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



我需要一个适用于SKU以两个字母开头的产品的目录价格规则。这样说:"喜欢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/TaitDefaultOperator.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/CoatalogRule/Rule/Cocondition/


<?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;
}
}

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

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/TaitDefaultOperator.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/CoatalogRule/Rule/Cocondition/


<?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;
}
}

最新更新