Magento update item qty use observer events <checkout_cart_save_after>

127 篇文章 0 订阅

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);
      			 		}
      			 }
			 } 
   }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值