0

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?

[EDIT]

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>

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>

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

2 回答 2

1

为事件创建观察者checkout_cart_product_add_after

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

     <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;
    }
}
于 2013-03-12T13:49:17.780 回答
0

You should write an observer that observes the cart and checks that only 3 samples are put to it.

Here an example

        <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>

Here you can see my observer that observes the event checkout_cart_product_add_after.

于 2013-03-11T21:17:59.520 回答