2

I have setting the custom price of the product on adding to cart which I am doing through some observer .

Everything is working fine except I am not getting the item id in the observer.I tried different ways but not able to get the item id of the added product .

Below is the code for reference. If anybody has some idea please revert.

class CustomPrice implements ObserverInterface
{
    protected $_request;
    protected $_helper;
    protected $cart;
    public function __construct(\Magento\Framework\App\RequestInterface $request,\TW\Product\Helper\Data $helper,\Magento\Checkout\Model\Cart $cart)
    {
        $this->_request = $request;
        $this->_helper = $helper;
        $this->cart=  $cart;
    }

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

        $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/my.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        $reqeustParams = $this->_request->getParams();
        $custom_price = $reqeustParams['custom_price'];

        $item = $observer->getEvent()->getData('quote_item');   


        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );

        $item_id = $item->getId(); **// I get nothing in this**     

        $item->setCustomPrice($custom_price);
        $item->setOriginalCustomPrice($custom_price);
        $item->getProduct()->setIsSuperMode(true);
        $pid = $item->getProductId();
        $logger->info("item id=====pid ".$item_id." ==== ".$pid);
        $this->_helper->updateIteminSession($pid,$item_id);

        //$logger->info("success !!!!");
    }

}

The response which i get on debug is below .. I don't see item id there :

[store_id] => 1
[product (Magento\Catalog\Model\Product\Interceptor)] => Array
    (
        [store_id] => 1
        [row_id] => 1
        [entity_id] => 1
        [created_in] => 1
        [updated_in] => 2147483647
        [attribute_set_id] => 4
        [type_id] => simple
        [sku] => 1
        [has_options] => 0
        [required_options] => 0
        [created_at] => 2017-06-12 08:00:07
        [updated_at] => 2017-06-12 08:00:07
        [name] => Retro Eyeglasses
        [meta_title] => Retro Eyeglasses
        [meta_description] => Retro Eyeglasses 
        [image] => /1/1/118179.jpg
        [small_image] => /1/1/118179.jpg
        [thumbnail] => /1/1/118179.jpg
        [options_container] => container2
        [country_of_manufacture] => US
        [url_key] => retro-eyeglasses
        [gift_message_available] => 2
        [gift_wrapping_available] => 2
        [is_returnable] => 2
        [swatch_image] => /1/1/118179.jpg
        [meta_keyword] => Retro Eyeglasses
        [price] => 12.9500
        [weight] => 1.0000
        [status] => 1
        [visibility] => 4
        [quantity_and_stock_status] => Array
            (
                [is_in_stock] => 1
                [qty] => 999997
            )

        [tax_class_id] => 2
        [options] => Array
            (
            )

        [media_gallery] => Array
            (
                [images] => Array
                    (
                        [1] => Array
                            (
                                [value_id] => 1
                                [file] => /1/1/118179.jpg
                                [media_type] => image
                                [row_id] => 1
                                [position] => 1
                                [disabled] => 0
                                [position_default] => 1
                                [disabled_default] => 0
                            )

                    )

                [values] => Array
                    (
                    )

            )

        [category_ids] => Array
            (
                [0] => 3
            )

        [tier_price] => Array
            (
            )

        [tier_price_changed] => 0
        [is_salable] => 1
        [website_ids] => Array
            (
                [0] => 1
            )

        [cart_qty] => 1
        [qty] => 1
        [event] => 
    )

[product_id] => 1
[product_type] => simple
[sku] => 1
[name] => Retro Eyeglasses
[weight] => 1.0000
[tax_class_id] => 2
[is_qty_decimal] => 
[qty_to_add] => 1
[qty] => 1
[qty_options] => Array
    (
    )
0

You need to do like

$id = $item->getItemId()

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

        $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/my.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        $reqeustParams = $this->_request->getParams();
        $custom_price = $reqeustParams['custom_price'];

        $item = $observer->getEvent()->getData('quote_item');   


        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );

        $item_id = $item->getItemId(); **// I get nothing in this**     

        $item->setCustomPrice($custom_price);
        $item->setOriginalCustomPrice($custom_price);
        $item->getProduct()->setIsSuperMode(true);
        $pid = $item->getProductId();
        $logger->info("item id=====pid ".$item_id." ==== ".$pid);
        $this->_helper->updateIteminSession($pid,$item_id);

        //$logger->info("success !!!!");
    }
  • Thanks Rakesh .. but after trying that also I am not getting the item id .. I did the same in the Magento1.x and it worked .. but not sure why not happening in M2. – ssnegi Sep 18 '17 at 13:34
  • just try with echo "<pre>";print_r($item->debug()); and check id is found or not? – Rakesh Jesadiya Sep 18 '17 at 13:44
  • Please check the result above on debug..I don't see item id there . – ssnegi Sep 18 '17 at 13:55
  • I noticed that when I add a product first time it did not show item id .. but when i add same product again ..it shows the item id – ssnegi Sep 19 '17 at 1:23

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.