在深入研究了Magento的核心代码之后,我发现你需要使用才能制作和工作。$item->getProduct()->setIsSuperMode(true)
$item->setCustomPrice()
$item->setOriginalPrice()
下面是一些示例代码,您可以在侦听 或 事件的观察器中使用。该代码在逻辑上是相同的,只是只为一个项目调用,并且为购物车中的所有项目调用。此代码仅作为示例分为/复制到 2 个方法。checkout_cart_product_add_after
checkout_cart_update_items_after
checkout_cart_product_add_after
checkout_cart_update_items_after
事件: checkout_cart_product_add_after
/**
* @param Varien_Event_Observer $observer
*/
public function applyDiscount(Varien_Event_Observer $observer)
{
/* @var $item Mage_Sales_Model_Quote_Item */
$item = $observer->getQuoteItem();
if ($item->getParentItem()) {
$item = $item->getParentItem();
}
// Discounted 25% off
$percentDiscount = 0.25;
// This makes sure the discount isn't applied over and over when refreshing
$specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);
// Make sure we don't have a negative
if ($specialPrice > 0) {
$item->setCustomPrice($specialPrice);
$item->setOriginalCustomPrice($specialPrice);
$item->getProduct()->setIsSuperMode(true);
}
}
事件: checkout_cart_update_items_after
/**
* @param Varien_Event_Observer $observer
*/
public function applyDiscounts(Varien_Event_Observer $observer)
{
foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item /* @var $item Mage_Sales_Model_Quote_Item */) {
if ($item->getParentItem()) {
$item = $item->getParentItem();
}
// Discounted 25% off
$percentDiscount = 0.25;
// This makes sure the discount isn't applied over and over when refreshing
$specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);
// Make sure we don't have a negative
if ($specialPrice > 0) {
$item->setCustomPrice($specialPrice);
$item->setOriginalCustomPrice($specialPrice);
$item->getProduct()->setIsSuperMode(true);
}
}
}