Magento:每个订单限制3种产品的分类 [英] Magento: limit 3 products from category per order

查看:75
本文介绍了Magento:每个订单限制3种产品的分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Magento中设置样本产品类别,以允许人们每次购买最多选择3个免费样本,但是我如何才能限制每个订单中该类别的3个样本?

I'm trying to set up a sample products category in Magento to allow people to select up to 3 free samples for every purchase, but how can I limit only 3 samples from that category per order?

这是目录app/code/local/MagePal/LimitCartProductByCategory/etc/config.xml中的当前config.xml:

This is the current config.xml inside the dir app/code/local/MagePal/LimitCartProductByCategory/etc/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>

dir app/etc/modules/MagePal_LimitCartProductByCategory.xml中的

MagePal_EnableDuplicateProductStatus.xml:

MagePal_EnableDuplicateProductStatus.xml in dir app/etc/modules/MagePal_LimitCartProductByCategory.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:

This is the current Observer.php inside the dir 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();
    $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

看看我的示例@ 更改重复产品的Magento默认状态获取有关如何在观察者上创建的帮助

Take a look at my example @ Change Magento default status for duplicated products for help on how to create on observer

     <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

Create: 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;
    }
}

这篇关于Magento:每个订单限制3种产品的分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆