0

I'm trying to create a shopping cart rule where free shipping will be given to certain products provided they have an inventory qty greater than 1.

Quantity (qty) is not a visible attribute in the attributes manager so I cant set it to use for promotion rules.

How do you enable the attribute inventory qty to be available for shopping cart rules?

1

Well if it's not too necessary for you to have a custom attribute for checking inventory of product(in catalog not cart) in Magento then, you can also achieve this using an event-observer.

You just have to observe event salesrule_validator_process this will fire just before the rule is applied, and with this you can check if the product's inventory meets your criteria(greater than 1) than only the rule will be validated to true.

Just create an observer like below: app/etc/modules/Company_Module.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Module>
            <codePool>local</codePool>
            <active>true</active>
        </Company_Module>
    </modules>
</config>

app/code/local/Company/Module/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Module>
            <version>1.0.0</version>
        </Company_Module>
    </modules>
    <global>
        <models>
            <module>
                <class>Company_Module_Model</class>
            </module>
        </models>
        <events>
            <salesrule_validator_process>
                <observers>
                    <company_module_model_observer>
                        <type>singleton</type>
                        <class>Company_Module_Model_Observer</class>
                        <method>checkInventory</method>
                    </company_module_model_observer>
                </observers>
            </salesrule_validator_process>
        </events>
    </global>
</config>

app/code/local/Company/Module/Model/Observer.php like below:

<?php
class Company_Module_Model_Observer extends Varien_Event_Observer {
    public function checkInventory($observer) {
        $quoteItems = $observer->getEvent()->getQuote()->getAllItems();
        foreach($quoteItems as $item):
            $productId = $item->getProductId();
            $_product = Mage::getModel('catalog/product')->load($productId);
            $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
            if(//Your product id or category condition) {
                if($stock->getQty() > 1.0000) {
                    //Do you stuff here
                }
            }
        endforeach;
    }
}

Refer below links for more info.

What observer need to use for NO-COUPON code apply option in magento?

http://www.webguys.de/magento/turchen-21-kostenlose-produkte-uber-warenkorb-preisregeln/

Hope this helps you out.

  • If you still didn't got a way to do this, I have updated my answer with code example, check it. – Vicky Dev Jan 8 '16 at 17:57
  • The module is a nice touch but ultimately your comment about custom attribute is what gave me the idea to do just that and have the stock replicated there, Thank you! – SR_Magento Jan 8 '16 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.