Skip to content

Instantly share code, notes, and snippets.

@ihor-sviziev
Last active November 3, 2015 13:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihor-sviziev/1dfc3b546d86ea490d8f to your computer and use it in GitHub Desktop.
Save ihor-sviziev/1dfc3b546d86ea490d8f to your computer and use it in GitHub Desktop.
Mage_SalesRule_Model_Rule_Condition_Product_Combine 1.9.1 fix
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category Mage
* @package Mage_SalesRule
* @copyright Copyright (c) 2006-2014 X.commerce, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Mage_SalesRule_Model_Rule_Condition_Product_Combine extends Mage_Rule_Model_Condition_Combine
{
/**
* Quote item conditions group
*/
const PRODUCT_ATTRIBUTES_TYPE_QUOTE_ITEM = 'quote_item';
/**
* "Product attribute match a value" conditions group
*/
const PRODUCT_ATTRIBUTES_TYPE_PRODUCT = 'product_attribute_match';
/**
* "Product attribute is set" conditions group
*/
const PRODUCT_ATTRIBUTES_TYPE_ISSET = 'product_attribute_isset';
/**
* Products attributes info
* @var array
*/
protected $_productAttributesInfo = null;
/**
* Initialize and retrieve a helper instance
* @return Mage_Catalog_Helper_Data
*/
protected function _getHelper()
{
return Mage::helper('catalog');
}
/**
* Check whether the attribute is a quote item attribute
* @param $attributeCode
*
* @return bool
*/
protected function _getIsQuoteItemAttribute($attributeCode)
{
return strpos($attributeCode, 'quote_item_') === 0;
}
/**
* Add an attribute condition to the conditions group
* @param $conditionType
* @param $conditionModel
* @param $attributeCode
* @param $attributeLabel
*
* @return $this
*/
protected function _addAttributeToConditionGroup($conditionType, $conditionModel, $attributeCode, $attributeLabel)
{
if (!array_key_exists($conditionType, $this->_productAttributesInfo)) {
$this->_productAttributesInfo[$conditionType] = array();
}
$conditionKey = sprintf('%s|%s', $conditionModel, $attributeCode);
$this->_productAttributesInfo[$conditionType][$conditionKey] = array(
'label' => $attributeLabel,
'value' => $conditionKey
);
return $this;
}
/**
* Retrieve a conditions by group_id
* @param $conditionsGroup
*
* @return array
*/
protected function _getAttributeConditions($conditionsGroup)
{
$this->_initializeProductAttributesInfo();
return array_key_exists($conditionsGroup, $this->_productAttributesInfo)
? $this->_productAttributesInfo[$conditionsGroup]
: array();
}
/**
* CHeck whether the product attribute information exists and initialize it if missing
* @return $this
*/
protected function _initializeProductAttributesInfo()
{
if (is_null($this->_productAttributesInfo)) {
$this->_productAttributesInfo = array();
$productAttributes = Mage::getModel('salesrule/rule_condition_product')
->loadAttributeOptions()
->getAttributeOption();
foreach ($productAttributes as $attributeCode => $attributeLabel) {
if ($this->_getIsQuoteItemAttribute($attributeCode)) {
$this->_addAttributeToConditionGroup(
self::PRODUCT_ATTRIBUTES_TYPE_QUOTE_ITEM,
'salesrule/rule_condition_product',
$attributeCode,
$attributeLabel
);
} else {
$this->_addAttributeToConditionGroup(
self::PRODUCT_ATTRIBUTES_TYPE_PRODUCT,
'salesrule/rule_condition_product',
$attributeCode,
$attributeLabel
)->_addAttributeToConditionGroup(
self::PRODUCT_ATTRIBUTES_TYPE_ISSET,
'salesrule/rule_condition_product_attribute_assigned',
$attributeCode,
$attributeLabel
);
}
}
}
return $this;
}
/**
* Initialize a rule condition
*/
public function __construct()
{
parent::__construct();
$this->setType('salesrule/rule_condition_product_combine');
}
/**
* Generate a conditions data
* @return array
*/
public function getNewChildSelectOptions()
{
$conditions = parent::getNewChildSelectOptions();
$conditions = array_merge_recursive(
$conditions,
array(
array(
'label' => Mage::helper('catalog')->__('Conditions Combination'),
'value' => 'salesrule/rule_condition_product_combine'
),
array(
'label' => Mage::helper('catalog')->__('Cart Item Attribute'),
'value' => $this->_getAttributeConditions(self::PRODUCT_ATTRIBUTES_TYPE_QUOTE_ITEM)
),
array(
'label' => Mage::helper('catalog')->__('Product Attribute'),
'value' => $this->_getAttributeConditions(self::PRODUCT_ATTRIBUTES_TYPE_PRODUCT),
),
array(
'label' => $this->_getHelper()->__('Product Attribute Assigned'),
'value' => $this->_getAttributeConditions(self::PRODUCT_ATTRIBUTES_TYPE_ISSET)
)
)
);
return $conditions;
}
/**
* Collect all validated attributes
* @param $productCollection
*
* @return $this
*/
public function collectValidatedAttributes($productCollection)
{
foreach ($this->getConditions() as $condition) {
$condition->collectValidatedAttributes($productCollection);
}
return $this;
}
/**
* Validate a condition with the checking of the child value
* @param Varien_Object $object
*
* @return bool
*/
public function validate(Varien_Object $object)
{
/** @var Mage_Catalog_Model_Product $product */
$product = $object->getProduct();
if (!($product instanceof Mage_Catalog_Model_Product)) {
$product = Mage::getModel('catalog/product')->load($object->getProductId());
}
$valid = parent::validate($object);
if (!$valid && $product->getTypeId() == Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE) {
$children = $object->getChildren();
$valid = $children && self::validate($children[0]);
}
return $valid;
}
}
@ktastrophy
Copy link

I have to ask. How did you find that? I've been debugging this for longer than I'd like to admit.

@ihor-sviziev
Copy link
Author

@ktastrophy I've just got this issue, enabled debug and fixed it =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment