1

我想在 Magento 1.7 中添加自定义促销条件。应该可以根据客户送货地址中的自定义属性应用促销。它实际上非常基本:如果属性被填充,则应该应用促销,而不是更多。

它应该显示为购物车属性的单独条件点,例如“送货邮政编码”或“送货地区”

还有什么帮助:标准的购物车促销条件是如何实施的,或者在哪里可以找到有关此主题的更多信息。我现在在互联网上搜索了很长时间,我真的被卡住了。非常感谢您的帮助!

多谢

4

1 回答 1

4

好的,我找到了一个或多或少的工作解决方案,我想在这里分享,以防其他人需要这个:

我创建了一个模块,在促销中引入了一个新的条件。为了实现这一点而缺少的和平是通过观察者来实现的。

首先是配置<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>

我认为这部分将其连接到 magento 接口:

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

其次是观察者,非常简单,只需将菜单点添加到 UI 并将其链接到条件<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;
    }
}

最后是实际完成工作的自定义条件类<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()));
    }

}

如果你们中的任何人对此代码有意见,我会很高兴听到他们的意见。

最好的祝愿
安德烈亚斯

于 2013-10-08T10:05:55.067 回答