我正在尝试在Magento中设置一个样本产品类别,以便人们可以为每次购买选择最多3个免费样品,但我如何才能限制每个订单中该类别的3个样品?
[编辑]
这是dir app / code / local / MagePal / LimitCartProductByCategory / etc / config.xml中的当前config.xml:
<?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>
目录app / etc / modules / MagePal_LimitCartProductByCategory.xml中的MagePal_EnableDuplicateProductStatus.xml:
<?xml version="1.0"?>
<config>
<modules>
<MagePal_LimitCartProductByCategory>
<active>true</active>
<codePool>local</codePool>
</MagePal_LimitCartProductByCategory>
</modules>
</config>
这是dir app / code / local / MagePal / LimitCartProductByCategory / Model / Observer.php中当前的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();
$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;
}
}
答案 0 :(得分:1)
为事件checkout_cart_product_add_after
请查看我的示例@ Change Magento default status for duplicated products,获取有关如何在观察者上创建
的帮助 <events>
<checkout_cart_product_add_after>
<observers>
<enableduplicateproductstatus>
<type>singleton</type>
<class>limitcartproductbycategory/observer</class>
<method>cartlimit</method>
</enableduplicateproductstatus>
</observers>
</checkout_cart_product_add_after>
</events>
创建: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;
}
}
答案 1 :(得分:0)
你应该写一个观察者观察购物车并检查只有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>
在这里你可以看到我的观察者观察事件checkout_cart_product_add_after。