0

I have a dynamic product which is based on user size input. On the basis of user size input I am calculating product price and weight.

I already set the product calculated price in custom price field of quote and set additional_options with all user input as custom option.

I also have to calculate the weight of the product based on user input and want to use it for shipping calculation.

I am trying to set product calculated weight in quote item as weight, but it get changed when quote refreshed or any extra activity on quote.

How do I set product custom weight in quote and used it in shipping method ?

1
  • Were you able to find a resolution to this? We have the same need. Commented Nov 9, 2017 at 0:25

2 Answers 2

0

Magento 2 : use Plugin for Type : Magento\Quote\Model\Quote\Item

after method setProduct() which is a public method. setWeight('123') and return the results.

public function afterSetProduct(\Magento\Quote\Model\Quote\Item $item, $results){


    $results->setWeight('80');

    return $results;

}
0

Vendor/Module/etc/events.xml

<?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="sales_quote_save_before">
        <observer name="vendor_module_quote_save_before" instance="Vendor\Module\Observer\CartSetProduct"/>
    </event>
</config>

Vendor/Module/Observer/CartSetProduct.php

<?php
declare (strict_types = 1);

namespace Vendor\Module\Observer;

class CartSetProduct implements \Magento\Framework\Event\ObserverInterface {

    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        // $cart = $observer->getCart();
        $quote = $observer->getQuote();
        $items = $quote->getAllVisibleItems();
        foreach ($items as $item) {
            $item->getProduct()->setWeight(0.2);
            $item->setWeight(0.2);
            // $item->calcRowTotal();
        }
    }
}
?>

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.