<?xml version="1.0"?>
<config>
<modules>
<MagePal_LimitCartProductByCategory>
<version>1.0.1</version>
</MagePal_LimitCartProductByCategory>
</modules>
<global>
<models>
<limitcartproductbycategory>
<class>MagePal_LimitCartProductByCategory_Model_Observer</class>
</limitcartproductbycategory>
</models>
<events>
<checkout_cart_product_add_after>
<observers>
<limitcartproductbycategory>
<type>singleton</type>
<class>MagePal_LimitCartProductByCategory_Model_Observer</class>
<method>cartlimit</method>
</limitcartproductbycategory>
</observers>
</checkout_cart_product_add_after>
</events>
</global>
</config>
tir app/etc/etc/modules/magepal_limitcartproductbybycategory.xml中的dir app/etc/eck in dir app/etc/eck中
<?xml version="1.0"?>
<config>
<modules>
<MagePal_LimitCartProductByCategory>
<active>true</active>
<codePool>local</codePool>
</MagePal_LimitCartProductByCategory>
</modules>
</config>
this是DIR APP/code/local/locagepal/limitCartProductByCategory/model/observer.php中的当前obser.php:
class MagePal_LimitCartProductByCategory_Model_Observer
{
public function cartlimit(Varien_Event_Observer $observer)
{
$category_ids = array();
$quote = Mage::getSingleton('checkout/session')->getQuote();
foreach($quote->getAllVisibleItems() as $item){
$product = Mage::getModel('catalog/product')->load($item->getId());
$product_category_ids = explode(",", $product->getCategoryIds());
//$product_category_ids = $product->getCategoryIds();
array_push($category_ids, $product_category_ids);
}
$justAdded = $observer->getQuoteItem();
$justAddedCategoryIds = explode(",", $product->getCategoryIds());
$justAddedId = in_array(58, $justAddedCategoryIds);
$productJustAdded = Mage::getModel('catalog/product')->load($justAdded->getId());
//total the catalogegory id in $category_ids
//if $productJustAdded->getCategoryIds exist in $category_ids,
//then check to see if category id count greater than 3
// if true then add error msg and try setting the qty to 0
$freesample = 58;
$tmp = array_count_values($category_ids);
$cnt = $tmp[$freesample];
echo $cnt;
if ($justAddedId == true && $cnt > 3) {
$quote->removeItem($justAdded->getId())->save();
Mage::app()->getLayout()->getMessagesBlock()->setMessages('You can only have 3 free samples. Please remove a sample to add another.');
Mage::app()->getLayout()->getMessagesBlock()->getGroupedHtml();
}
return $this;
}
}
创建事件的观察者
checkout_cart_product_add_after
查看我的示例 @ change magento的默认产品默认状态为如何在观察者上创建的帮助
创建:app/code/local/magepal/limitcartproductByCategory/model/observer.php
class MagePal_LimitCartProductByCategory_Model_Observer
{
public function cartlimit(Varien_Event_Observer $observer)
{
$category_ids = array();
$quote = Mage::getSingleton('checkout/session')->getQuote();
foreach($quote->getAllVisibleItems() as $item){
$product = Mage::getModel('catalog/product')->load($item->getId());
$product_category_ids = explode(",", $product->getCategoryIds());
//$product_category_ids = $product->getCategoryIds();
array_push($category_ids, $product_category_ids);
}
$justAdded = $observer->getQuoteItem();
$productJustAdded = Mage::getModel('catalog/product')->load($justAdded->getId());
//total the category id in $category_ids
//if $productJustAdded->getCategoryIds exist in $category_ids,
//then check to see if category id count greater than 3
// if true then add error msg and try setting the qty to 0
return $this;
}
}
您应该写一个观察者,该观察者观察购物车,并检查只有3个样本。
在这里一个例子 <checkout_cart_product_add_after>
<observers>
<tibdev_fancybox_cart_observer>
<type>singleton</type>
<class>Tibdev_Fancybox_Model_Cart_Observer</class>
<method>applyFancybox</method>
</tibdev_fancybox_cart_observer>
</observers>
</checkout_cart_product_add_after>
在这里您可以看到我的观察者观察事件COBLECOUT_CART_PRODUCT_ADD_AFTER.