马根托 |在报价循环中更新项目价格
Magento | Update Item Price in Quote Loop
我需要为订单中的每个产品执行一些自定义定价,这会改变项目的最终价格。
我在 sales_quote_item_qty_set_after
事件中使用观察器来确保每次重新配置产品时价格都是 运行,因为我无法从其他活动。
我 运行 遇到的问题是在使用 ->getAllItems()
.
遍历报价时无法更改报价中的产品价格
我知道可以通过以下产品添加事件覆盖价格。
$_item = $obs->getQuoteItem();
$_item = ( $_item->getParentItem() ? $_item->getParentItem() : $_item );
$new_price = 1000;
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->getProduct()->setIsSuperMode(true);
但是,当 运行浏览购物车中的报价项目时,我无法找到执行此操作的方法。这可能吗,或者定价必须在不同的观察者中完成,如果是这样,哪个?
完整代码如下...
public function adminAddProduct(Varien_Event_Observer $obs) {
$quote = $this->_getSession()->getQuote();
foreach($quote->getAllItems() as $_item){
$options = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
$_product = $_item->getProduct();
$sku = $options['simple_sku'];
if ($sku) {
$tierPrices = $_product->getTierPrice();
$price = $_item->getPrice();
$qty = $options['info_buyRequest']['qty'];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$runspeed = $product->getResource()->getAttribute('runspeed')->getFrontend()->getValue($product);
$prodOpts = [];
foreach($options['options'] as $option) {
$prodOpts[$option['label']] = $option['value'];
}
$index = 0;
foreach($tierPrices as $tierPrice) {
if($tierPrice['price_qty'] <= $qty && $tierPrices[$index + 1]['price_qty'] > $qty) {
$price = $tierPrice['price'];
}
//$prodTiers[$tierPrice['price_qty']] = $tierPrice['price'];
}
if($prodOpts['Quantity'] == 'Fixed Quantity' && array_key_exists('Quantity Select', $prodOpts)){
$qty = str_replace(',', '', explode(" ", $prodOpts['Quantity Select'])[0]);
} else {
$qty = 1;
}
$_item->setQty($qty);
$_item->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice);
$new_price = 999;
$_item->setPrice($new_price);
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->save();
Mage::log($price);
Mage::log($prodOpts);
}
}
}
感谢任何帮助。
您可以在函数结束时尝试 $quote->save()
。
我需要为订单中的每个产品执行一些自定义定价,这会改变项目的最终价格。
我在 sales_quote_item_qty_set_after
事件中使用观察器来确保每次重新配置产品时价格都是 运行,因为我无法从其他活动。
我 运行 遇到的问题是在使用 ->getAllItems()
.
我知道可以通过以下产品添加事件覆盖价格。
$_item = $obs->getQuoteItem();
$_item = ( $_item->getParentItem() ? $_item->getParentItem() : $_item );
$new_price = 1000;
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->getProduct()->setIsSuperMode(true);
但是,当 运行浏览购物车中的报价项目时,我无法找到执行此操作的方法。这可能吗,或者定价必须在不同的观察者中完成,如果是这样,哪个?
完整代码如下...
public function adminAddProduct(Varien_Event_Observer $obs) {
$quote = $this->_getSession()->getQuote();
foreach($quote->getAllItems() as $_item){
$options = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
$_product = $_item->getProduct();
$sku = $options['simple_sku'];
if ($sku) {
$tierPrices = $_product->getTierPrice();
$price = $_item->getPrice();
$qty = $options['info_buyRequest']['qty'];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$runspeed = $product->getResource()->getAttribute('runspeed')->getFrontend()->getValue($product);
$prodOpts = [];
foreach($options['options'] as $option) {
$prodOpts[$option['label']] = $option['value'];
}
$index = 0;
foreach($tierPrices as $tierPrice) {
if($tierPrice['price_qty'] <= $qty && $tierPrices[$index + 1]['price_qty'] > $qty) {
$price = $tierPrice['price'];
}
//$prodTiers[$tierPrice['price_qty']] = $tierPrice['price'];
}
if($prodOpts['Quantity'] == 'Fixed Quantity' && array_key_exists('Quantity Select', $prodOpts)){
$qty = str_replace(',', '', explode(" ", $prodOpts['Quantity Select'])[0]);
} else {
$qty = 1;
}
$_item->setQty($qty);
$_item->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice);
$new_price = 999;
$_item->setPrice($new_price);
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->save();
Mage::log($price);
Mage::log($prodOpts);
}
}
}
感谢任何帮助。
您可以在函数结束时尝试 $quote->save()
。