0

Im changing by checkout_cart_product_add_after the product Price and also want to change the Product weight.

here is the code: Magento 1.9.3

 public function checkoutCartProductAddAfter(Varien_Event_Observer $observer){
....

  // Get the quote item
  $item = $observer->getEvent()->getQuoteItem();

  // Ensure we have the parent item, if it has one
  $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
  // Enable super mode on the product.
  $item->getProduct()->setIsSuperMode(true)

  $weight = /.some calculation based on custom configuration../
  $item->setWeight($weight); // not working
...
}

so the weight is still overwritten by

app/code/core/Mage/Sales/Model/Quote/item.php

 //line # 390
...
->setWeight($this->getProduct()->getWeight())
...

Have you any solution about this. Without changing the item.php file.

Thank you

Usman

My IDEA

So my Idea is to set here a custom Attribute like:

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer){
....

  // Get the quote item
  $item = $observer->getEvent()->getQuoteItem();

  // Ensure we have the parent item, if it has one
  $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
  // Enable super mode on the product.
  $item->getProduct()->setIsSuperMode(true)

  $weight = /.some calculation based on custom configuration../
  $item->setWeight($weight); // not working

  $item->setCustomWeight($weight); // here we set a custom attribute custom_weight and store the calculated weight
...
} 

and then set a second observer sales_quote_item_set_product

public function salesQuoteItemSetProduct(Varien_Event_Observer $observer){
    $_event     = $observer->getEvent();
    $_quoteItem = $_event->getQuoteItem();
    $_product   = $_quoteItem->getProduct();

    if ($weight = $_product->getData('custom_weight'))
        $_quoteItem->setWeight($weight);// set the new weight
}

but its still not working, because the Value of the Custom Attribute

custom_weight

is empty.

1 Answer 1

1

try:

after your code

$item->setWeight($weight);

add

$item->getProduct()->setWeight($weight);

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.