1

In cart page I have added one option for customer to add gift package charge for his order. I had successfully display option on cart page.

enter image description here

Whenever customer check the checkbox, I am adding some additional amount to order[As you can see I had added $20]. But it label doesn't display in total section(Under shipping rate) In checkout page it display in correct manner . [For that I have added required files as this great answer suggested ]. After debugging I found that in fetch method I am getting null amount[that's why it didn't display]

My giftwrapfee.php is below

<?php

namespace NAME_SPACE\PACKAGE_NAME\Model\Total\Quote;

class Giftwrapfee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal {

    protected $quoteValidator = null;
      /**
     * @var scopeConfig
     */
    protected $scopeConfig;
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $_checkoutSession;
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;
    /**
     * Discount calculation object
     *
     * @var \Magento\SalesRule\Model\Validator
     */
    protected $calculator;

    /**
     * @var \Magento\Framework\Pricing\PriceCurrencyInterface
     */
    protected $priceCurrency;
    /**
     * Core event manager proxy
     *
     * @var \Magento\Framework\Event\ManagerInterface
     */
    protected $eventManager = null;
    /**
     * @param \Magento\Quote\Model\QuoteValidator $quoteValidator
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Magento\Checkout\Model\Session $checkoutSession     
     */
    public function __construct(
    \Magento\Quote\Model\QuoteValidator $quoteValidator, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\SalesRule\Model\Validator $validator,\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,\Magento\Framework\Event\ManagerInterface $eventManager
    ) {
        $this->setCode('my_giftfee');
        $this->eventManager = $eventManager;
        $this->quoteValidator = $quoteValidator;
        $this->scopeConfig = $scopeConfig;
        $this->_checkoutSession = $checkoutSession;
        $this->storeManager = $storeManager;
        $this->calculator = $validator;
        $this->priceCurrency = $priceCurrency;
    }

    /**
     * Collect address discount amount
     *
     * @param \Magento\Quote\Model\Quote $quote
     * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
     * @param \Magento\Quote\Model\Quote\Address\Total $total
     * @return $this
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    public function collect(
    \Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total
    ) {
        parent::collect($quote, $shippingAssignment, $total);
        $items = $shippingAssignment->getItems();
        if (!count($items)) {
            return $this;
        }                
            $store = $this->storeManager->getStore($quote->getStoreId());
            $address = $shippingAssignment->getShipping()->getAddress();
            $this->calculator->reset($address);

            $giftamount = 20; // my gift wrap amount            
            if ($giftamount > 0) {                                
                $total->addTotalAmount('my_giftfee', $giftamount);
                $total->setTotalAmount('my_giftfee', $giftamount);                
                $total->setGiftFee($fee);
                $total->setBaseGiftFee($fee);
                $address->setGiftFee($fee);
                $address->setBaseGiftFee($fee);
                $address->setGrandTotal($address->getGrandTotal() + $giftamount);
                $address->setBaseGrandTotal($address->getBaseGrandTotal() + $giftamount);
                $quote->setGiftFee($giftamount);
                $quote->setBaseGiftFee($giftamount);
                $total->setGrandTotal($total->getGrandTotal() + $giftamount);
                $total->setBaseGrandTotal($total->getBaseGrandTotal() + $giftamount);
                $quote->setGrandTotal($quote->getGrandTotal() + $giftamount);
                $quote->setBaseGrandTotal($quote->getBaseGrandTotal() + $giftamount);                
            }
        return $this;
    }

    /**
     * @param \Magento\Quote\Model\Quote $quote
     * @param Address\Total $total
     * @return array|null
     */
    public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total) {
        $amount = $total->getGiftFee();        
        if ($amount != 0) {
            $result = [
            'code' => 'my_giftfee',
            'title' => __("Gift Grap"),
            'value' => $amount
            ];
        }
        return $result;
    }

}

Can any one help on this weird issue ?

Any help will be appreciated .

Many thanks

  • In your fetch method, you try $amount = $quote->getGiftFee(); . There's a payment fee module, you can use it as a sample: github.com/mrkhoa99/Boolfly_Payment_Fee – Khoa TruongDinh Jun 19 '16 at 13:14
  • @KhoaTruongDinh I have used this method but its not working. It might be case issue because I am using checkbox to add fee. (In checkout page it display correct manner) – pjones Jun 19 '16 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.