Question

I am creating giftcard code programmatically. For this in vendor GiftCardAccount Module, the code is:

$model = $this->giftCAFactory->create()->setStatus(
            \Magento\GiftCardAccount\Model\Giftcardaccount::STATUS_ENABLED
        )->setWebsiteId(
            $data->getWebsiteId()
        )->setBalance(
            $data->getAmount()
        )->setLifetime(
            $data->getLifetime()
        )->setIsRedeemable(
            $data->getIsRedeemable()
        )->setOrder(
            $order
        )->save();

So in custom module I am creating as:

$model = $this->giftCAFactory->create()->setStatus(
                    \Magento\GiftCardAccount\Model\Giftcardaccount::STATUS_DISABLED
                )->setWebsiteId(
                    $this->_storeManager->getStore()->getId()
                )->setBalance(
                    $amount
                )->setLifetime(
                    $lifetime
                )->setIsRedeemable(
                    TRUE
                )->save();

Only Lifetime (ExpiryDate) is not saving correctly.

If I get LifeTime in timestamp as:

$lifetime = strtotime(date('Y-m-d').'+12 month'); //1605657600

then the saved date is any random date before 1999

If I get value for LifeTime as

$lifetime = date('18/11/2020');

Then ExpiryDate is not set and is shown as no expiry for that gift code.

How can I save correct date, I want to set expiry date, 1 year after the current date.

Edit:

As mentioned by @Sohel Rana, I created function to get date in vendor way which I tested by printing output and it works as:

 public function getLiftTime(){

    $currentDate = new \DateTime('now', new \DateTimeZone($this->_localeDate->getConfigTimezone()));
    $expirydays = 365;
    if (is_numeric($expirydays) && $expirydays > 0) {
        return $this->_localeDate->date(clone $currentDate)
            ->modify('+' . $expirydays . ' days')
            ->format('Y-m-d');
}
}

and called this function to setLiftTime as:

$model = $this->giftCAFactory->create()->setStatus(
                    \Magento\GiftCardAccount\Model\Giftcardaccount::STATUS_DISABLED
                )->setWebsiteId(
                    $this->_storeManager->getStore()->getId()
                )->setBalance(
                    $amount
                )->setLifetime(
                    $this->getLiftTime()
                )->setIsRedeemable(
                    TRUE
                )->save();

But with this way, the gift card is still creating without any expiry date. What is missing here ?

enter image description here

Was it helpful?

Solution

If you open the following class:

vendor/magento/module-gift-card-account/Model/Giftcardaccount.php

The following code is responsible for modifying Lifetime.

if (is_numeric($this->getLifetime()) && $this->getLifetime() > 0) {
    $this->setDateExpires(
        $this->_localeDate->date(clone $currentDate)
            ->modify('+' . $this->getLifetime() . ' days')
            ->format('Y-m-d')
    );
}

So pass Lifetime as a day, not timestamp.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top