the Requirement:
there is a visual service item in cart,
every time change relation product , the service item will be change qty
so add a Magento Observer model to approach it.
here is some content in the config.xml :
<global>
<helpers>
<mycheckout>
<class>Bysoft_Mycheckout_Helper</class>
</mycheckout>
</helpers>
<models>
<mycheckout>
<class>Bysoft_Mycheckout_Model</class>
</mycheckout>
</models>
<resources>
<bysoft_mycheckout_setup>
<setup>
<module>Bysoft_Mycheckout</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
</bysoft_mycheckout_setup>
</resources>
<events>
<checkout_cart_save_before> <!-- identifier of the event we want to catch -->
<observers>
<checkout_cart_save_before_handler> <!-- identifier of the event handler -->
<type>model</type> <!-- class method call type; valid are model, object and singleton -->
<class>mycheckout/observer</class> <!-- observers class alias -->
<method>changeServiceItemQty</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</checkout_cart_save_before_handler>
</observers>
</checkout_cart_save_before>
</events>
</global>
PHP Model Class:
<?php
class Bysoft_Mycheckout_Model_Observer
{
public function changeServiceItemQty(Varien_Event_Observer $observer)
{
$event = $observer->getEvent();
$cart = $event->getCart();
//检测安心厨房商品总价
$semi_total = 0;
$items= $cart->getQuote()->getAllItems();
foreach ($items as $item) {
$productId = $item->getProduct()->getId();
$item_product = Mage::getModel('catalog/product')->load($productId);
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($item_product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
if ($attributeSetName == 'Semi Cooked' && $item->getData('product_type') == 'bundle') {
$semi_total += $item->getRowTotalInclTax();
}
}
//检测服务item是否存在
$item_exists = false;
$item_exists_id = false;
$item_exists_sku = false;
$cartHelper = Mage::helper('checkout/cart');
foreach ($items as $item) {
$productId = $item->getProduct()->getId();
$item_product = Mage::getModel('catalog/product')->load($productId);
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($item_product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
if ($attributeSetName == 'Chef Catering Services') {
$item_exists = true;
$item_exists_id = $item->getId();
$item_exists_sku = $item_product->getSku();
//更新已存在服务数量
$item->setQty(ceil(0.3*$semi_total));
break;
}
}
//如果semi cooked总金额小于1000. 将服务item 删除
if ($semi_total < 1000 && $item_exists) {
foreach ($items as $item) {
$productId = $item->getProduct()->getId();
$item_product = Mage::getModel('catalog/product')->load($productId);
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($item_product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
if ($attributeSetName == 'Chef Catering Services') {
$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId);
}
}
}
}
}

本文介绍如何在Magento中通过Observer模型实现购物车服务项数量的动态调整,特别是当改变关联产品时,通过配置config.xml和实现PHP Model类来完成这一任务。
1091

被折叠的 条评论
为什么被折叠?



