2

I am working with Magento 2.0.10, I have created an observer to set the product price and weight posted from the products details page using a custom attribute. In the below example I am trying to set a static price and weight. For the price attribute its working correctly, but I am not able to set the weight. The quote item is giving the actual product weight while calculating shipping. Even if I change the observer event to sales_quote_item_set_product, it is not working at all for changing weight.

And even now I have tested with Magento 2.1 as well, but it isn't working at all

In events.xml and I am creating an observer like below

<event name="checkout_cart_product_add_after">
    <observer name="checkout_cart_product_add_after_customprice" instance="Test\Slider\Observer\Customprice" />
</event>

And script that i have used in my observer is working like this

$item = $observer->getQuoteItem();
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
$item->setCustomPrice(10);
$item->setOriginalCustomPrice(10);
$item->setWeight(10);
$item->getProduct()->setIsSuperMode(true);
  • It would be saving under field row_weight, not sure why but if you can adjust to use it at required places by using $item->getRowWeight() in your calculations.. – Himmat Paliwal Nov 2 '18 at 6:35
0

You could achive this using a plugin:

app/code/Product/SetWeight/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Product_SetWeight',
    __DIR__
);


app/code/Product/SetWeight/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Product_SetWeight"/>
</config>


app/code/Product/SetWeight/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\Quote\Item">
    <plugin name="product_set_weight" type="Product\SetWeight\Plugin\ItemWeight"/>
</type>
</config>

app/code/Product/SetWeight/Plugin/ItemWeight.php

<?php

namespace Product\SetWeight\Plugin;

class ItemWeight
{
    public function afterSetProduct(\Magento\Quote\Model\Quote\Item $subject, $result)
    {
        $weight = "3";
        $result->setWeight($weight);
        return $result;
    }
}

This solution is of course missing custom logic to determine which products should be changed and how!

Hope I've been able to help people revisting this thread.

| improve this answer | |
-1

Finally , i have found the solution. May be it's not the best solution , but it works

I have just commented line number 422 with script

->setWeight($this->getProduct()->getWeight()) 

coming inside setProduct function in the below file

/vendor/magento/module-quote/Model/Quote/Item.php

And its working for me.

| improve this answer | |
  • You know that changing code in a file that's in vendor will be overwritten as soon as a newer version of that file/module is available and installed. I have to be honest, I don't know the answer right away, but I don't think this will be the solution you're looking for. – Arjen Miedema Feb 27 '18 at 6:24

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.