Question

I have a custom_field on the product details page( Like a hidden field ). Which is not a product attribute. I can get this field value using checkout_cart_product_add_after event using an observer like this.

<event name="checkout_cart_product_add_after">
    <observer name="CartProductAddAfter" instance="Ayakil\ProductsSubscription\Observer\Checkout\CartProductAddAfter"/>
</event>

Observer file

class CartProductAddAfter implements \Magento\Framework\Event\ObserverInterface
{
public function __construct(
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\App\RequestInterface $request
) {
    $this->_logger = $logger;
    $this->_request = $request;
}public function execute(
    \Magento\Framework\Event\Observer $observer
) {               
    
    $postData = $this->_request->getParams();       
    $frequencies = $postData['frequencies'];
}

}

My question is I have a custom field( subcribe_frequency ) in quote_item, How can I set this $frequencies quote items after adding the products to the cart?

Please someone help me in this?

Was it helpful?

Solution

Simply use the following code:

public function execute(
    \Magento\Framework\Event\Observer $observer
) {

    $postData = $this->_request->getParams();
    $frequencies = $postData['frequencies'];
    $item = $observer->getQuoteItem();
    $item->setSubcribeFrequency($frequencies);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top