0

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

1 Answer 1

1

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.

2
  • Hi @Sohel Rana, I have update the question after trying with you suggestion, please check.
    – Ajwad Syed
    Nov 19, 2019 at 7:52
  • Yes, Now it is solved.
    – Ajwad Syed
    Nov 19, 2019 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.