2

Magento のカートに製品を追加するときに、見積もりアイテムの重量を更新したいと考えています。

私はこのコードを試しました:

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{
   $event = $observer->getEvent();
   $quote_item = $event->getQuoteItem();
   $myWeight = ($quote_item->getWeight()/100)*$number;
   $quote_item->setWeight($myWeight);
   $quote_item->save();
}

しかし、それは機能していません。同じ機能で見積もりアイテムの価格を更新しましたが、機能します。

新しい体重が適用されないのはなぜですか?

4

4 に答える 4

3

sales_quote_item_set_productイベントを使用します。このイベントは、見積もりアイテムが製品を設定した後、すべてのリクエストで呼び出されます。これは、価格と重量をカスタム値に変更する方法です。

/*
 * Called on every request, updates item after loaded from session
 */
public function updateItem(Varien_Event_Observer $observer) 
{
    $item = $observer->getQuoteItem();
    if ($item->getParentItem()) {
        $item = $item->getParentItem();
    }

    // set price
    $item->setCustomPrice($customPrice);
    $item->setOriginalCustomPrice($customPrice);
    $item->getProduct()->setIsSuperMode(true);

    // set weight
    $item->setWeight($customWeight);
}

それが役に立てば幸い :)

于 2014-05-27T12:11:04.183 に答える
1

あなたは正しい出来事を観察しています

checkout_cart_product_add_after

これを行う必要はありません。関数からこれを削除できます

$quote_item->save();

しかし、これを追加

$quote_item->getProduct()->setIsSuperMode(true);

これで、関数は次のようになります

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{  
   $event = $observer->getEvent();
   $quote_item = $event->getQuoteItem();
   $myWeight = ($quote_item->getWeight()/100)*$number;

   $quote_item->getProduct()->setIsSuperMode(true);

   $quote_item->setWeight($myWeight);
}

元の製品重量をテーブルに書き込まないようにするために行う必要がある主なことは、次の行 (v1.9 CE) 行番号 390からファイルsales_flat_quote_itemをコピーして削除することです。item.phpapp/code/core/Mage/Sales/Model/Quoteapp/code/local/Mage/Sales/Model/Quote

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

今では魅力のように機能するはずです。試行錯誤

于 2015-11-04T13:07:57.470 に答える
1

見積もりアイテムではなく、見積もりまたはカートを保存する必要があります。試す:

$quote_item->getQuote()->save();

また

Mage::getSingleton('checkout/cart')->save();
于 2012-08-03T20:50:29.320 に答える
0

ここで私のコードを試すことができます。sidebar.phtml で

<?php
$items = Mage::getSingleton('checkout/cart')->getQuote()
    ->getItemsCollection()->getItems();
$product = Mage::getModel('catalog/product');
$total_weight = 0;
foreach ($items as $item) {
    $product = $item->getProduct();
    $qty = $item->getQty();
    $weight = $item->getWeight();
    if ($product->isConfigurable()) {
        $total_weight += ($weight * ($qty - 1));
    } else {
        $total_weight += ($weight * $qty);
    };
}
?>


<div><strong>Total Weight</strong></div>
</div><strong><?php echo $total_weight;?> KG</strong></div>
于 2013-11-19T12:53:05.540 に答える