1

I created the custom discount plugin.

When I use the the following to get the grandtotal using api, /rest/V1/carts/mine/totals:

My buymore plugin is not firing the collect method via Api. Why is so? Should Api also fire the collect method and not just the fetch method? Just because of that, I have to put all my codes from collect to fetch. Can someone help me to advice why collect method is not called via Api? I need to use it for my mobile development and I hope that someone can help. I have added a log in the collect method and it is not logged at all.

/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Woxzy\BuyMore\Model\Quote;

class BuyMore extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
    /**
     * Discount calculation object
     *
     * @var \Magento\SalesRule\Model\Validator
     */
    protected $calculator;
    /**
     * Core event manager proxy
     *
     * @var \Magento\Framework\Event\ManagerInterface
     */
    protected $eventManager = null;
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;
    /**
     * @var \Magento\Framework\Pricing\PriceCurrencyInterface
     */
    protected $priceCurrency;
    /**
     * @param \Magento\Framework\Event\ManagerInterface $eventManager
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\SalesRule\Model\Validator $validator
     * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
     */

    const XML_PATH = "buymore/buymore/";

    public function __construct(
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\SalesRule\Model\Validator $validator,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
    ) {
        $this->setCode('buymore');
        $this->eventManager = $eventManager;
        $this->calculator = $validator;
        $this->storeManager = $storeManager;
        $this->priceCurrency = $priceCurrency;
    }



    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);

         $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
         $logger = $objectManager->get('\Psr\Log\LoggerInterface');
         $logger->debug("Collect hello");

         return $this;

    }

    /**
     * Add discount total information to address
     *
     * @param \Magento\Quote\Model\Quote $quote
     * @param \Magento\Quote\Model\Quote\Address\Total $total
     * @return array|null
     */
    public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
    {
        $result = null;

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $logger = $objectManager->get('\Psr\Log\LoggerInterface');

        $address=$quote->getAddress();

        $scopeConfig = $objectManager->get('\Magento\Framework\App\Config\ScopeConfigInterface');

        $buyMore = $objectManager->get('Woxzy\BuyMore\Model\BuyMore');

        $websiteScope=\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE;

        $buyMore->setCurrencyTo($this->storeManager->getStore()->getCurrentCurrencyCode());
        //$buyMore->setCurrencyTo("EUR");
        $buyMore->setCurrencyFrom($scopeConfig->getValue(SELF::XML_PATH."currency_fr",$websiteScope));
        $buyMore->setQty($scopeConfig->getValue(SELF::XML_PATH."qty",$websiteScope));
        $buyMore->setDiscountType($scopeConfig->getValue(SELF::XML_PATH."discount_type",$websiteScope));
        $buyMore->setBaseInitialAmt($scopeConfig->getValue(SELF::XML_PATH."base_initial_amt",$websiteScope));
        $buyMore->setBaseSubsequentAmt($scopeConfig->getValue(SELF::XML_PATH."base_subsequent_amt",$websiteScope));
        $buyMore->setIsActive($scopeConfig->getValue(SELF::XML_PATH."active",$websiteScope));

        //$logger->debug("getBaseSubsequentAmt >> ".$buyMore->getBaseSubsequentAmt());

        $result=[];

        if($buyMore->getIsActive()==true){
            $store = $this->storeManager->getStore();

            $currencyFactory = $objectManager->get('\Magento\Directory\Model\CurrencyFactory');

            if (is_string($buyMore->getCurrencyFrom())) {

              $rateToBase = $currencyFactory->create()->load($buyMore->getCurrencyFrom())->getAnyRate($buyMore->getCurrencyTo());

            } elseif ($buyMore->getCurrencyFrom() instanceof \Magento\Directory\Model\Currency) {

                  $rateToBase = $buyMore->getCurrencyFrom()->getAnyRate($buyMore->getCurrencyTo());
            }

            $buyMore->calculateByQty($quote->getItemsQty(),$rateToBase);

            $description="Buy More Save More";

            $baseSubtotalWithDiscount=(float)$quote->getBaseSubtotal()+$buyMore->getBaseDiscountAmount();
            $subtotalWithDiscount=(float)$quote->getSubtotal()+$buyMore->getDiscountAmount();

            $quote->setBaseBuyMoreAmount((float)$buyMore->getBaseDiscountAmount());
            $quote->setBuyMoreAmount((float)$buyMore->getDiscountAmount());
            $quote->setBaseSubtotalWithDiscount($baseSubtotalWithDiscount);
            $quote->setSubtotalWithDiscount($subtotalWithDiscount);
            $quote->setGrandTotal($subtotalWithDiscount);
            $quote->setBaseGrandTotal($baseSubtotalWithDiscount);
            $quote->save();

            $quote->getShippingAddress()->setBaseBuyMoreAmount((float)$buyMore->getBaseDiscountAmount());
            $quote->getShippingAddress()->setBuyMoreAmount((float)$buyMore->getDiscountAmount());
            $quote->getShippingAddress()->setBaseSubtotalWithDiscount($baseSubtotalWithDiscount);
            $quote->getShippingAddress()->setSubtotalWithDiscount($subtotalWithDiscount);
            $quote->getShippingAddress()->setGrandTotal($subtotalWithDiscount);
            $quote->getShippingAddress()->setBaseGrandTotal($baseSubtotalWithDiscount);
            $quote->getShippingAddress()->save();

            $total->setBaseBuyMoreAmount((float)$buyMoreSave->getBaseDiscountAmount());
            $total->setBuyMoreAmount((float)$buyMoreSave->getDiscountAmount());
            $total->setBaseSubtotalWithDiscount($baseSubtotalWithDiscount);
            $total->setSubtotalWithDiscount($subtotalWithDiscount);
            $total->setGrandTotal($subtotalWithDiscount);
            $total->setBaseGrandTotal($baseSubtotalWithDiscount);

            $result = [
                ['code' => $this->getCode(),
                'title' => strlen($description) ? __($description) : __('Discount'),
                'value' => $buyMore->getDiscountAmount()
                ]
            ];
        }//if buy more save more is active            

        return $result;

    }
}
2
  • Show your configuration xml – Sohel Rana Feb 3 '17 at 11:46
  • At the moment I don't have access to the codes till monday. Do you have any reference instead? – madi Feb 3 '17 at 11:57

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.