5

I have my custom code , which generates the new customer and add products to that customer's cart.This code is working fine for me.I want to add some discount to that customer programmatically. So how can I add some discount programmatically for the customer's cart using this controller file.

<?php
/**
 *
 * Copyright © 2015 Detailcommerce. All rights reserved.
 */
namespace Mp\Accountmanager\Controller\Accountmanager;

class Amsave extends \Magento\Framework\App\Action\Action
{
    protected $_messageManager;
    protected $cart;
    protected $product;
    protected $resultPageFactory;
    protected $customerRepositoryInterface;
    protected $quoteModel;
    protected $productRepository;
    protected $cartManagementInterface;
    protected $cartRepositoryInterface;
    protected $storeManager;

    public function __construct(
        \Magento\Framework\App\Action\Context $context, 
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
        \Magento\Quote\Model\Quote $quoteModel,
         \Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
         \Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Catalog\Model\Product $product,
        \Magento\Checkout\Model\Cart $cart) {
        parent::__construct($context);
        $this->_messageManager = $messageManager;
        $this->storeManager = $storeManager;
        $this->resultPageFactory = $resultPageFactory;
        $this->cart = $cart;
        $this->_customerRepositoryInterface = $customerRepositoryInterface;
        $this->cartManagementInterface = $cartManagementInterface;
        $this->quoteModel = $quoteModel;
        $this->cartRepositoryInterface = $cartRepositoryInterface;
        $this->productRepository = $productRepository;
        $this->product = $product;
    }
    public function execute()
    {
        $post = $this->getRequest()->getParams();

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
        $state = $objectManager->get('\Magento\Framework\App\State');

        $customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory');

        $websiteId = $storeManager->getWebsite()->getWebsiteId();

        $store = $storeManager->getStore();  // Get Store ID

        $storeId = $store->getStoreId();

        $customer = $objectManager->create('Mp\Accountmanager\Model\Accountmanager');

        $customer_new = $customerFactory->create();

                    $customer_new->setWebsiteId($websiteId);

                    $customer_new->setEmail($post['customer_email']);

                    $customer_new->setFirstname($post['customer_contact_person']);

                    $customer_new->setLastname($post['customer_contact_person']);

                    $customer_new->setPassword($post['customer_email']);

                   $customer_new->save();




                    $cartId = $this->cartManagementInterface->createEmptyCart(); //Create empty cart
                    $quote = $this->cartRepositoryInterface->get($cartId); // load empty cart quote
                    $quote->setStoreId($this->storeManager->getStore()->getId());
                    $customer= $this->_customerRepositoryInterface->getById($customer_new->getId());
                    $quote->setCurrency();
                    $quote->assignCustomer($customer);

                     if($customerSession->getProcuctIdsSm() != NULL)
                    {
                    $products_arraysm = $customerSession->getProcuctIdsSm();
                    $products_arraysm = explode(',', $products_arraysm);
                     foreach ($products_arraysm as $value) 
                    {
                        $_product = $this->productRepository->getById($value);    
                        $quote->addProduct($_product,1);
                    }
                    }
                    $quote->collectTotals()->save();


            $this->messageManager->addSuccessMessage(__("Product Added successfully"));

    $this->_redirect('checkout/cart');
}
}                     

Please help me!!! Your answer would be appreciated...Thanks

  • Did you ever get your answer? – vitoriodachef Sep 21 '18 at 13:42
2

Try following way:


$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->setCouponCode($couponCode)->collectTotals();
$this->cartRepositoryInterface->save($quote);
  • Do you have any idea about how can i apply custom discount using custom controller ? – anonymous Mar 19 '18 at 7:33
  • If i use the coupon code , then it is working fine. $quote->setCouponCode('9898989')->collectTotals()->save(); But i want to add custom discount price instead of coupon code. Thanks!!! – Sunny Rahevar Mar 19 '18 at 7:46

Your Answer

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

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