如何在 Magento 1 中应用我的自定义目录价格规则条件?

Posted

技术标签:

【中文标题】如何在 Magento 1 中应用我的自定义目录价格规则条件?【英文标题】:How can i apply my custom catalog price rule condition in Magento 1? 【发布时间】:2021-03-25 02:19:22 【问题描述】:

我需要适用于 SKU以两个字母开头的产品的目录价格规则。所以像这样:`LIKE AB%'。目前这是不可能的(有一个 %value% 选项,但这不是我需要的)。

我可以在目录规则创建选项卡中显示我的新操作员。不幸的是,我无法找到实际应用此新条件的特定部分或要覆盖的部分。跟踪并尝试分配了几个小时,但找不到。那么接下来是什么,我应该覆盖什么来应用我的新运算符?

Peer1979/Modulename/etc/local.xml

<config>    
    <global> 
    <models>
            <catalogrule>
                <rewrite>
                    <rule_condition_combine>Peer1979_Modulename_Model_CatalogRule_Model_Rule_Condition_Combine</rule_condition_combine>    
                    <rule_condition_product>Peer1979_Modulename_Model_CatalogRule_Model_Rule_Condition_Product</rule_condition_product>
                </rewrite>                
            </catalogrule>
        </models> 
        </global>
</config>

Peer1979/Modulename/Model/CatalogRule/Model/Rule/Condition/TraitDefaultOperator.php


<?php

trait TraitDefaultOperator

    /*
     * @var string  Operator starts with (f.e. LIKE ST%)
     */

    private $operator_starts_with = '^[]';

    /*
     * Get default operator input by type, extended with new operator '^[]'.
     */

    private function getDefaultAndCustomOperatorInputByType()
    
        return [
            'string' => array('==', '!=', '>=', '>', '<=', '<', '', '!', '()', '!()', $this->operator_starts_with),
            'numeric' => array('==', '!=', '>=', '>', '<=', '<', '()', '!()'),
            'date' => array('==', '>=', '<='),
            'datetime' => array('==', '>=', '<='),
            'select' => array('==', '!='),
            'boolean' => array('==', '!='),
            'multiselect' => array('[]', '![]', '()', '!()'),
            'grid' => array('()', '!()'),
        ];
    

    /**
     * Default operator options getter
     * Provides all possible operator options
     *
     * @return array
     */
    public function getDefaultOperatorOptions()
    
        if (null === $this->_defaultOperatorOptions) 
            $this->_defaultOperatorOptions = $this->getDefaultAndCustomOperatorOptions();
        
        return $this->_defaultOperatorOptions;
    
    /*
     * Get default operator  options, extended with new operator '^[]'.
     */

    private function >getDefaultAndCustomOperatorOptions()
    
        return [
            //default magento operators
            '==' => Mage::helper('rule')->__('is'),
            '!=' => Mage::helper('rule')->__('is not'),
            '>=' => Mage::helper('rule')->__('equals or greater than'),
            '<=' => Mage::helper('rule')->__('equals or less than'),
            '>' => Mage::helper('rule')->__('greater than'),
            '<' => Mage::helper('rule')->__('less than'),
            '' => Mage::helper('rule')->__('contains'),
            '!' => Mage::helper('rule')->__('does not contain'),
            '[]' => Mage::helper('rule')->__('contains'),
            '![]' => Mage::helper('rule')->__('does not contain'),
            '()' => Mage::helper('rule')->__('is one of'),
            '!()' => Mage::helper('rule')->__('is not one of'),

            //custom magento operators
            $this->operator_starts_with => Mage::helper('rule')->__('[custom] starts with'),
        ];
    

Peer1979/Modulename/Model/CatalogRule/Model/Rule/Condition/Combine.php


<?php

require_once( dirname(__FILE__) . '/TraitDefaultOperator.php');

class Peer1979_Modulename_Model_CatalogRule_Model_Rule_Condition_Combine extends Mage_CatalogRule_Model_Rule_Condition_Combine

    use TraitDefaultOperator;

    /**
     * Default operator input by type map getter
     *
     * @return array
     */
    public function getDefaultOperatorInputByType()
    
        if (null === $this->_defaultOperatorInputByType) 
            $this->_defaultOperatorInputByType = $this->getDefaultAndCustomOperatorInputByType();
            $this->_arrayInputTypes = array('multiselect', 'grid');
        
        return $this->_defaultOperatorInputByType;
    



Peer1979/Modulename/Model/CatalogRule/Rule/Condition/


<?php

require_once( dirname(__FILE__) . '/TraitDefaultOperator.php');

class Peer1979_Modulename_Model_CatalogRule_Model_Rule_Condition_Product extends Mage_CatalogRule_Model_Rule_Condition_Product

    use TraitDefaultOperator;

    /**
     * Default operator input by type map getter
     *
     * @return array
     */
    public function getDefaultOperatorInputByType()
    
        if (null === $this->_defaultOperatorInputByType) 
            $this->_defaultOperatorInputByType = $this->getDefaultAndCustomOperatorInputByType();
            $this->_arrayInputTypes = array('multiselect', 'grid');

            //additional mangento default overrides
            $this->_defaultOperatorInputByType['category'] = array('==', '!=', '', '!', '()', '!()');
            $this->_arrayInputTypes[] = 'category';
        
        return $this->_defaultOperatorInputByType;
    


【问题讨论】:

【参考方案1】:

已修复!使用此代码,您可以创建自定义价格规则条件。请注意,这仅适用于字符串值。供将来参考或任何有兴趣的人:

Peer1979/Modulename/etc/local.xml

<config>    
    <global> 
    <models>
            <catalogrule>
                <rewrite>
                    <rule_condition_combine>Peer1979_Modulename_Model_CatalogRule_Model_Rule_Condition_Combine</rule_condition_combine>    
                    <rule_condition_product>Peer1979_Modulename_Model_CatalogRule_Model_Rule_Condition_Product</rule_condition_product>
                </rewrite>                
            </catalogrule>
        </models> 
        </global>
</config>

Peer1979/Modulename/Model/CatalogRule/Model/Rule/Condition/TraitDefaultOperator.php


<?php

trait TraitDefaultOperator

    /*
     * @var string  Operator starts with (f.e. LIKE ST%)
     */

    private $operator_starts_with = '^[]';

    /*
     * Get default operator input by type, extended with new operator '^[]'.
     */

    private function getDefaultAndCustomOperatorInputByType()
    
        return [
            'string' => array('==', '!=', '>=', '>', '<=', '<', '', '!', '()', '!()', $this->operator_starts_with),
            'numeric' => array('==', '!=', '>=', '>', '<=', '<', '()', '!()'),
            'date' => array('==', '>=', '<='),
            'datetime' => array('==', '>=', '<='),
            'select' => array('==', '!='),
            'boolean' => array('==', '!='),
            'multiselect' => array('[]', '![]', '()', '!()'),
            'grid' => array('()', '!()'),
        ];
    

    /**
     * Default operator options getter
     * Provides all possible operator options
     *
     * @return array
     */
    public function getDefaultOperatorOptions()
    
        if (null === $this->_defaultOperatorOptions) 
            $this->_defaultOperatorOptions = $this->getDefaultAndCustomOperatorOptions();
        
        return $this->_defaultOperatorOptions;
    
    /*
     * Get default operator  options, extended with new operator '^[]'.
     */

    private function >getDefaultAndCustomOperatorOptions()
    
        return [
            //default magento operators
            '==' => Mage::helper('rule')->__('is'),
            '!=' => Mage::helper('rule')->__('is not'),
            '>=' => Mage::helper('rule')->__('equals or greater than'),
            '<=' => Mage::helper('rule')->__('equals or less than'),
            '>' => Mage::helper('rule')->__('greater than'),
            '<' => Mage::helper('rule')->__('less than'),
            '' => Mage::helper('rule')->__('contains'),
            '!' => Mage::helper('rule')->__('does not contain'),
            '[]' => Mage::helper('rule')->__('contains'),
            '![]' => Mage::helper('rule')->__('does not contain'),
            '()' => Mage::helper('rule')->__('is one of'),
            '!()' => Mage::helper('rule')->__('is not one of'),

            //custom magento operators
            $this->operator_starts_with => Mage::helper('rule')->__('[custom] starts with'),
        ];
    

Peer1979/Modulename/Model/CatalogRule/Model/Rule/Condition/Combine.php


<?php

require_once( dirname(__FILE__) . '/TraitDefaultOperator.php');

class Peer1979_Modulename_Model_CatalogRule_Model_Rule_Condition_Combine extends Mage_CatalogRule_Model_Rule_Condition_Combine

    use TraitDefaultOperator;

    /**
     * Default operator input by type map getter
     *
     * @return array
     */
    public function getDefaultOperatorInputByType()
    
        if (null === $this->_defaultOperatorInputByType) 
            $this->_defaultOperatorInputByType = $this->getDefaultAndCustomOperatorInputByType();
            $this->_arrayInputTypes = array('multiselect', 'grid');
        
        return $this->_defaultOperatorInputByType;
    



Peer1979/Modulename/Model/CatalogRule/Rule/Condition/


<?php

require_once( dirname(__FILE__) . '/TraitDefaultOperator.php');

class Peer1979_Modulename_Model_CatalogRule_Model_Rule_Condition_Product extends Mage_CatalogRule_Model_Rule_Condition_Product

    use TraitDefaultOperator;

    /**
     * Default operator input by type map getter
     *
     * @return array
     */
    public function getDefaultOperatorInputByType()
    
        if (null === $this->_defaultOperatorInputByType) 
            $this->_defaultOperatorInputByType = $this->getDefaultAndCustomOperatorInputByType();
            $this->_arrayInputTypes = array('multiselect', 'grid');

            //additional mangento default overrides
            $this->_defaultOperatorInputByType['category'] = array('==', '!=', '', '!', '()', '!()');
            $this->_arrayInputTypes[] = 'category';
        
        return $this->_defaultOperatorInputByType;
    

  /**
     * Validate product attribute value for condition
     * Note: code is much like Mage_Rule_Model_Condition_Abstract > validate_attribute, with adjustments for new operator(s).
     *
     * @param   mixed $validatedValue product attribute value
     * @return  bool
     */
    public function validateAttribute($validatedValue)
    
        /* Code equal to original code */

        if (is_object($validatedValue)) 
            return false;
        

        $value = $this->getValueParsed();       //Condition value
        $op = $this->getOperatorForValidate();  //Condition operator

        // if operator requires array and it is not, or on opposite, return false
        if ($this->isArrayOperatorType() xor is_array($value)) 
            return false;
        

        $result = false;

        /* END Code equal to original code */

        switch ($op) 
            case $this->operator_starts_with:   //ok
                $value = (array)$value;
                foreach ($value as $item) 
                    //only support for strings!
                    //validate when one item starts with
                    if (strpos($validatedValue, $item) === 0)
                    
                        $result = true;
                        break;
                    
                

                break;
            default:    //ok
                $result = Mage_Rule_Model_Condition_Abstract::validateAttribute($validatedValue);
        

        return $result;
    


【讨论】:

以上是关于如何在 Magento 1 中应用我的自定义目录价格规则条件?的主要内容,如果未能解决你的问题,请参考以下文章

history.phtml 上 Magento 中的自定义状态

麦进斗:magento如何安装子主题

麦进斗:magento如何安装子主题

Magento 中的自定义表单

从 magento 的自定义模块中添加 sales_flat_order_item 表中的新字段

如何在 Magento 中添加自定义字段来订购?