3

My problem is to set custom option price as product price while adding to cart. Usually in Magento custom option price will add to the product price but I want to assign custom option price as product price.

For example product price is 10 and custom option price is 20. When product is added, it will assume total price as 10+20 = 30. But I want product price to be 20 only.

I need a suggestion, can we keep product price as "0" so that product price will become the custom option price when added? Please let me know if there is any better approach or it is already answered somewhere.

Note: Removed answer from question and posted it as answer

4

1 Answer 1

1

Here is the solution you can apply. It will work even if you have multiple custom options for a product.

create events.xml in the folder 'Namespace/Module/etc/frontend' and use the event 'checkout_cart_product_add_after'

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_product_add_after">
        <observer name="customproductprice" instance="Namespace\Module\Observer\CustomProductPrice" />
    </event>
</config>

Create CustomProductPrice.php in Observer folder.

<?php
    namespace Namespace\Module\Observer;

    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\RequestInterface;

    class CustomProductPrice implements ObserverInterface
    {
        protected $_objectManager;
        public function __construct(
                \Magento\Framework\ObjectManagerInterface $objectManager
            ) {
                $this->_objectManager = $objectManager;
            }
        public function execute(\Magento\Framework\Event\Observer $observer) {
            $item = $observer->getEvent()->getData('quote_item');         
            $product=$observer->getEvent()->getData('product');
            $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
            $_customOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($product); 
            $totalPrice = 0;
            foreach ($_customOptions['options'] as $key => $value) {
                $customOptions = $this->_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);
                foreach ($customOptions as $o) {
                    $values = $o->getValues();
                    foreach ($values as $v) {
                        if ($value['option_value'] == $v->getOptionTypeId()) {
                            $totalPrice += $v->getprice(); /* get price of custom option*/
                        }
                     }
                }
            }
            $item->setCustomPrice($totalPrice);
            $item->setOriginalCustomPrice($totalPrice);
            $item->getProduct()->setIsSuperMode(true);
        }

    }
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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