1

I have customer attributes added using a module and I need to add add them to Promotions, especially for Shopping Cart Price Rules. I found this tutorial, but looks like I'm doing something wrong, because it doesn't work (the attributes are not added). I also can't contact the author for help. I don't have any experience in creating modules, so please bear with me. What I did:

  1. in app/etc/modules I put XYZ_AddPromoCA.xml with the following content
<?xml version="1.0"?>
<config>
    <modules>
        <XYZ_AddPromoCA>
        <active>false</active>
        <codePool>local</codePool>
        </XYZ_AddPromoCA>
    </modules>
</config>
  1. in app/local I created the following directory structure enter image description here

  2. in XYZ/AddPromoCA/etc I created config.xml containing the code

    <?xml version="1.0"?>
    <config>
        <modules>
            <XYZ_AddPromoCA>
                <version>1.0.0</version>
            </XYZ_AddPromoCA>
        </modules>
        <global>
          <models>
                <xyz_addpromoca>
                    <class>XYZ_AddPromoCA_Model</class>
                </xyz_addpromoca>
            </models>
            <events>
                <salesrule_rule_condition_combine>
                    <observers>
                        <xyz_addpromoca_add_customer_to_salesrules_conditions>
                            <class>XYZ_AddPromoCA_Model_Observer</class>
                            <method>updateSalesRulesConditionsForCustomerAttributes</method>
                        </xyz_addpromoca_add_customer_to_salesrules_conditions>
                    </observers>
                </salesrule_rule_condition_combine>            
            </events>
        </global>
    </config>
  1. in XYZ/AddPromoCA/Model put an Observer.php containing the code
    <?php
    class XYZ_AddPromoCA_Model_Observer extends Varien_Event_Observer 
    {
        public function updateSalesRulesConditionsForCustomerAttributes(Varien_Event_Observer $observer)
        {
            $customerCondition = Mage::getModel('company/rule_condition_customer');
            $customerAttributes = $customerCondition->loadAttributeOptions()->getAttributeOption();
            $attributes = array();
            foreach ($customerAttributes as $code=>$label) {
                $attributes[] = array('value'=>'company/rule_condition_customer|'.$code, 'label'=>$label);
            }
            $additional = $observer->getAdditional();
            $conditions = array(
                array('label'=>'Customer Attribute', 'value'=>$attributes)
            );
            $additional->setConditions($conditions);
            return $additional;
        }
    }
  1. and finally in XYZ/AddPromoCA/Model/Rule/Condition i put a Customer.php having the code
<?php
    class XYZ_AddPromoCA_Model_Rule_Condition_Customer extends Mage_Rule_Model_Condition_Abstract
    {
        /**
         * @return $this
         * These are customer attributes we want to use as condition rules for
         */
        public function loadAttributeOptions()
        {
            $attributes = array(
                'account_type' => 'Account Type',
                'allowed_units' => 'Allowed Units',
            );
            $this->setAttributeOption($attributes);
            return $this;
        }
        /**
         * Validate Customer Rule Condition
         *
         * @param Varien_Object $object
         * @return bool
         */
        public function validate(Varien_Object $object)
        {
            $customer = ($object instanceof Mage_Customer_Model_Customer) ? $object : $object->getQuote()->getCustomer();
            $customer
                ->setAccountType($customer->getAccountType())
                ->setAllowedUnits($customer->getAllowedUnits())
            return parent::validate($customer);
        }
    }

I am sure there's something wrong with calling the customer attribute and getting the values, because the attributes are created using a commercial extension and I don't know what part of the code to modify to call them.

The customer attribute codes and labels are account_type / Account Type and allowed_units / Allowed Units The database tables created by the customer attributes extension are enter image description here

If there's anyone who can help me to get this fixed, I'd really appreciate it.

Your Answer

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

Browse other questions tagged or ask your own question.