3

I reported the following issue as Magento Issue #9976 but I'd like to know if a fix for this is in the wild already and I'm just missing it.


I'm working on a Magento ver. 1.9.2.4 shop. I must create an additional catalogrule discount on products that have special_from_date attribute set. This is the interesting part of my rule:

enter image description here

My problem is: this rule doesn't work in a reliable way: sometime it does, sometime it doesn't.

I think I tracked down the issue in a apple-to-orange comparison in shop/app/code/core/Mage/Rule/Model/Condition/Abstract.php#614.

As you can see in this debugging session, the function Mage_CatalogRule_Model_Rule_Condition_Product::validateAttribute() is comparing $validatedValue <= $value, where the former ia a unix timestamp (1504310400) and the latter is a plain date 2018-01-18.

The former is coming from the special_from_date attribute, the latter is the date set in the rule

enter image description here

The main issue is that shop/app/code/core/Mage/CatalogRule/Model/Rule/Condition/Product.php#118 converts the product attribute into a unix timestamp, while no conversion is applied to the conditions of the rule.

1 Answer 1

3

Confirmed in 1.9.3.7

Possible fix:

Change Mage_CatalogRule_Model_Rule_Condition_Product::_prepareDatetimeValue() to

protected function _prepareDatetimeValue($value, $object)
{
    $attribute = $object->getResource()->getAttribute($this->getAttribute());
    if ($attribute && $attribute->getBackendType() == 'datetime') {
        $this->setValue(strtotime($this->getValue())); # added line
        $value = strtotime($value);
    }
    return $value;
}

Edit: this brings up next bug

W/o this change comparison is always true for products without "Special Price From" and false if a date is set. Result, price rules are

  • added for products w/o "Special Price From"
  • not added for products with valid "Special Price From"

Seems to be completly wrong. The change from above fixes just the comparison, if a "Special Price From" is set. If not set comparison is still wrong:

Mage_Rule_Model_Condition_Abstract#L610

        case '<=': case '>':
            if (!is_scalar($validatedValue)) {
                return false;
            } else {
                $result = $validatedValue <= $value;
            }
            break;

Problems:

  1. $value = strtotime($value); - strtotime return false for null or empty value
  2. is_scalar($validatedValue) - returns true if $validatedValue is false
  3. false is less the some value, so it returns wrong $result

Think (tested) this should work:

protected function _prepareDatetimeValue($value, $object)
{
    $attribute = $object->getResource()->getAttribute($this->getAttribute());
    if ($attribute && $attribute->getBackendType() == 'datetime') {
        /**
         * seems this was still wrong for empty values
        $this->setValue(strtotime($this->getValue()));
        if (is_scalar($value)) {
            $value = strtotime($value);
        }
        */
        if (!$value) {
            return null;
        }
        $this->setValue(strtotime($this->getValue()));
        $value = strtotime($value);
    }
    return $value;
}
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.