I created a custom module for applying custom coupon code programmatically in magento 2. For shopping cart rule.
I have only coupon code with block and model collection check below image.
How can do it ? Any suggestion for that ?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI created a custom module for applying custom coupon code programmatically in magento 2. For shopping cart rule.
I have only coupon code with block and model collection check below image.
How can do it ? Any suggestion for that ?
First you need to inject a few classes in your constructor:
protected $cart;
protected $quoteRepository;
public function __construct(
...
\Magento\Checkout\Model\Cart $cart,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
...
$this->cart= $cart;
$this->quoteRepository = $quoteRepository;
}
Then in your code you can do: Data.php in helper
public function getCouponCode(){
$cartQuote = $this->cart->getQuote();
return $cartQuote->getCouponCode();
}
Here use own create function replace $cartQuote->getCouponCode();
to $cartQuote->getOwnFuction();
And put code in getOwnFuction()
for apply custom couponCode
And then i put in my function: Vendor\Shipping\Model\Quote\Address\Rate.php
public function importShippingRate(\Magento\Quote\Model\Quote\Address\RateResult\AbstractResult $rate)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$helper = $objectManager->create('Vendor\Shipping\Helper\Data');
$couponCode = $helper->getCouponCode();
echo 'CouponsCode = '.$couponCode;
}
$couponCode
with your particular coupon code
Oct 10, 2016 at 9:18