Question

I would like to add a custom promotion condition to Magento 1.7. It should be possible to apply a promotion based on a custom attribute in the shipping address of the customer. Its actually quite basic: if the attribute is filled, the promotion should apply, not more.

It should appear as a seperate condition point of the Cart Attributes like "Shipping Postcode" or "Shipping Region"

What would help as well: How are the standard Cart Promotion conditions are implemented or where to look for more information on this topic. I searched the internet for quite a while now, and I'm really stuck. Your help is highly appreciated!

Thx a lot

Was it helpful?

Solution

Ok, I found a more or less working solution I want to share here, in case sombody else is needing this:

I created a module, that is introducing a new Condition into the promotions. The missing peace in order to achieve this was to do it via an observer.

First the config <path to module>\etc\config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <HauteNature_FamilienkarteHessen>
            <version>1.0.0</version>
        </HauteNature_FamilienkarteHessen>
    </modules>
    <admin>
        <fieldsets>
            <customer_dataflow>
                <building>
                    <billing>1</billing>
                    <shipping>1</shipping>
                </building>
            </customer_dataflow>
        </fieldsets>
    </admin>

    <global>

        <models>
            <familienkartehessen>
                <class>HauteNature_FamilienkarteHessen_Model</class>
            </familienkartehessen>
        </models>
        <helpers>
            <familienkartehessen>
                <class>HauteNature_FamilienkarteHessen_Helper</class>
            </familienkartehessen>
        </helpers>
        <events>
         <salesrule_rule_condition_combine>
            <observers>
                <add_condition_to_sales_rule>
                    <class>familienkartehessen/observer</class>
                    <method>addConditionToSalesRule</method>
                </add_condition_to_sales_rule>
            </observers>
         </salesrule_rule_condition_combine>
        </events>

        <resources>
            <familienkartehessen_setup>
                <setup>
                    <module>HauteNature_FamilienkarteHessen</module>
                    <class>Mage_Eav_Model_Entity_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
           </familienkartehessen_setup>
        </resources>

        <fieldsets>
            <sales_copy_order_billing_address>
                <familiycarthessen><to_order>*</to_order></familiycarthessen>
            </sales_copy_order_billing_address>
            <sales_copy_order_shipping_address>
                <familiycarthessen><to_order>*</to_order></familiycarthessen>
            </sales_copy_order_shipping_address>
            <sales_convert_quote_address>
                <familiycarthessen><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></familiycarthessen>
            </sales_convert_quote_address>
            <sales_convert_order_address>
                <familiycarthessen><to_quote_address>*</to_quote_address></familiycarthessen>
            </sales_convert_order_address>
            <customer_address>
                 <familiycarthessen><to_quote_address>*</to_quote_address></familiycarthessen>
            </customer_address>
            <checkout_onepage_billing>
                <familiycarthessen><to_customer>*</to_customer></familiycarthessen>
            </checkout_onepage_billing>
        </fieldsets>
    </global>
</config>

I think this part is hoocking it to the magento Interface:

<salesrule_rule_condition_combine>
    <observers>
        <add_condition_to_sales_rule>
        <class>familienkartehessen/observer</class>
            <method>addConditionToSalesRule</method>
        </add_condition_to_sales_rule>
    </observers>
 </salesrule_rule_condition_combine>

Second the observer, quite simple, just adding the menu point to the UI and linking it to the condition: <path to module>\Model\Observer.php:

class HauteNature_FamilienkarteHessen_Model_Observer extends Mage_Core_Model_Abstract {

    /**
     * Event: salesrule_rule_condition_combine
     *
     * @param $observer
     */
    public function addConditionToSalesRule($observer) {

        $additional = $observer->getAdditional();
        $conditions = (array) $additional->getConditions();

        $conditions = array_merge_recursive($conditions, array(
            array('label'=>Mage::helper('familienkartehessen')->__('Familienkarte Hessen'), 'value'=>'familienkartehessen/condition_familienkarte'),
        ));

        $additional->setConditions($conditions);
        $observer->setAdditional($additional);

        return $observer;
    }
}

And finally the custom Condition Class that actually does the work <path to module>\Model\Condition/Familienkarte.php:

class HauteNature_FamilienkarteHessen_Model_Condition_Familienkarte extends Mage_Rule_Model_Condition_Abstract {

    /**
     * @TODO for whatever this it, check it and afterwards document it!
     *
     * @return Hackathon_DiscountForATweet_Model_Condition_Tweet
     */
    public function loadAttributeOptions() {
        $attributes = array(
            'fkhContent' => Mage::helper('familienkartehessen')->__('Familienkarte Hessen')
        );

        $this->setAttributeOption($attributes);

        return $this;
    }

    /**
     * @TODO for whatever this it, check it and afterwards document it!
     *
     * @return mixed
     */
    public function getAttributeElement() {
        $element = parent::getAttributeElement();
        $element->setShowAsText(true);
        return $element;
    }

    /**
     * @TODO for whatever this it, check it and afterwards document it!
     *
     * @return string
     */
    public function getInputType() {

        switch ($this->getAttribute()) {
            case 'fkhContent':
                return 'boolean';
        }
        return 'string';
    }

    /**
     * @TODO for whatever this it, check it and afterwards document it!
     * @return string
     */
    public function getValueElementType() {
        return 'text';
    }

    /**
     * Validate FamiliencarteHessen Rule Condition
     *
     * @param Varien_Object $object
     *
     * @return bool
     */
    public function validate(Varien_Object $object) {

        /* here should be something meaningful */
        $address = $object;
        if (!$address instanceof Mage_Sales_Model_Quote_Address) {
            if ($object->getQuote()->isVirtual()) {
                $address = $object->getQuote()->getBillingAddress();
            }
            else {
                $address = $object->getQuote()->getShippingAddress();
            }
        }

        return $this->validateAttribute(trim($address->getFamiliycarthessen()));
    }

}

If anyone of you has comments on this code, I would be greatfull to hear them.

Best wishes
Andreas

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top