<?php

namespace Magedelight\Giftwrapper\Controller\Giftwrapper;

use Magento\Store\Model\StoreManagerInterface;

class AddNewItem extends \Magento\Framework\App\Action\Action
{
    /**
    * @var \Magento\Framework\View\Result\PageFactory
    */
    protected $resultPageFactory;

    /**
    * @var storeManager
    */
    protected $_storeManager;

    /**
    * @var additional options
    */
    protected $additionalOptions;

    /**
    * @var Core Registry
    */
    protected $_coreRegistry = null;

    /**
    * @var \Magento\Checkout\Model\Cart
    */
    protected $_cart;

    /**
    * @var \Magento\Catalog\Model\Product
    */
    protected $_productRepository;

    /**
    * @var \Magento\Catalog\Model\Product
    */

    protected $helper;
    protected $_productFactory;

    protected $_quoteItem;


    /**
    * @param \Magento\Framework\Registry $coreRegistry
    * @param \Magento\Store\Model\StoreManagerInterface $storeManager
    * @param \Magento\Framework\App\Action\Context $context
    * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
    * @param \Magento\Checkout\Model\Cart $cart
    * @param \Magento\Catalog\Model\Product $product
    * @param \Magento\Catalog\Model\productRepositorysitory $productRepository
    * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
    * @param \Magento\Store\Model\System\Store $systemStore
    * @param $instanceName = 'Magento\Catalog\Model\Product'
    */
    public function __construct(
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Catalog\Model\ProductRepository $productRepository,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Magento\Store\Model\System\Store $systemStore,        
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magedelight\Giftwrapper\Helper\Data $helper,
        \Magento\Quote\Model\Quote\Item $quoteItem,
        $instanceName = 'Magento\Catalog\Model\Product'
    )
    {
        $this->_productFactory = $productFactory;
        $this->_cart = $cart;
        $this->_productRepository = $productRepository;
        $this->_storeManager = $storeManager;
        $this->instanceName = $instanceName;
        $this->resultPageFactory = $resultPageFactory;
        $this->resultJsonFactory = $resultJsonFactory;
        $this->helper = $helper;
        $this->_quoteItem = $quoteItem;
        parent::__construct($context);
    }
    /**
    * Default customer account page
    *
    * @return void
    */
    public function execute()
    {
        $store = $this->_storeManager->getStore();
        $data = $this->getRequest()->getParams();

        $params1 = array();

        $existingAdditionalOptions = array();
        $addExistingOptions = array();

        $result = $this->resultJsonFactory->create();        
        $item = $this->_cart->getQuote()->getItemById($data['item_id']);
        $base_price = $item->getPrice();
        $qty = $item->getQty();
        $productOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());

        if($item->getOptionByCode('additional_options')){
            $existingAdditionalOptions = $this->helper->getJsonUnserialized($item->getOptionByCode('additional_options')->getValue());  
        }

        $addExistingOptions = $existingAdditionalOptions;


        if(isset($existingAdditionalOptions) && count($existingAdditionalOptions) > 0){
            foreach($existingAdditionalOptions as $option){
                if(!isset($option['code'])){
                    $addExistingOptions[] = [
                            'label' => isset($option['label']) ? $option['label'] : '',
                            'value'=> isset($option['value']) ? $option['value'] : '',
                        ];
                }
            }
        }

        $additionOptionsSerialized = '';

        if(isset($addExistingOptions) && !empty($addExistingOptions)) {
            $additionOptionsSerialized = $this->helper->getJsonSerialized($addExistingOptions);
        }



        $storeId = $this->_storeManager->getStore()->getId();

        //$_product = $this->product->load($item->getProduct()->getId());
        $_product = $this->_productFactory->create()->setStoreId($storeId)->load($item->getProduct()->getId());
        $productName = $_product->getName();
        $productId = $_product->getId();




        if($_product){
                $params1 = array(
                    'product' => $productId,
                    'qty' => 1, 
                    'base_price' => $base_price,
                    'selected_configurable_option' => isset($productOptions['info_buyRequest']['selected_configurable_option']) ? $productOptions['info_buyRequest']['selected_configurable_option'] : '',
                    'related_product' => isset($productOptions['info_buyRequest']['related_product']) ? $productOptions['info_buyRequest']['related_product'] : '',
                    'super_attribute' => isset($productOptions['info_buyRequest']['super_attribute']) ? $productOptions['info_buyRequest']['super_attribute'] : '',
                    'options' => isset($productOptions['info_buyRequest']['options']) ? $productOptions['info_buyRequest']['options'] : '',
                    'additional_options' => isset($additionOptionsSerialized) ? $additionOptionsSerialized : ''                  
                );
}


        try {
            $cartresult = $this->_cart->addProduct($_product, $params1);
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addException(
                $e,
                __('%1', $e->getMessage())
            );
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('error.'));
        }

        $this->_cart->save();

        $result->setData(['success' => true]);        
        return $result;
    }
}  

If the product that I am adding has no additional options to it and if the same product already exists in the cart with additional options to it, then on adding this product a new item entry is made into quote_item table having price as zero. I need to remove the other product having additional options & add both the previous product with additional options and new product without additional options so that the zero price issue is resolved. Can anyone please help me with this issue ?

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.