0

After upgrading to Magento 2.3.2 (from 2.2.8) I am having a problem with a piece of custom code. I am running this as part of a block for displaying valid cart rules for a product.

public function getRules($product) {
    if (!$product) {
        $product = $this->registry->registry('product');
    }

    $_rules = $this->ruleFactory->create()->getCollection();

    foreach($_rules as $rule){
        $validate = $rule->getActions()->validate($product);
        if ($validate){
            // Do something
        }

    }

    // remaining omitted
}

The problematic part that I am running into is this line:

$validate = $rule->getActions()->validate($product);

It is throwing the following errors.

2019/07/25 12:22:26 [error] 5286#0: *7638021 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Call to a member function getTypeId() on null in /site.com/dev/vendor/magento/module-configurable-product/Plugin/SalesRule/Model/Rule/Condition/Product.php:59
Stack trace:
#0 /site.com/dev/vendor/magento/module-configurable-product/Plugin/SalesRule/Model/Rule/Condition/Product.php(30): Magento\ConfigurableProduct\Plugin\SalesRule\Model\Rule\Condition\Product->getProductToValidate(Object(Magento\AdvancedSalesRule\Model\Rule\Condition\Product\Interceptor), Object(Magento\Catalog\Model\Product\Interceptor))
#1 /site.com/dev/vendor/magento/framework/Interception/Interceptor.php(121): Magento\ConfigurableProduct\Plugin\SalesRule\Model\Rule\Condition\Product->beforeValidate(Object(Magento\AdvancedSalesRule\Model\Rule\Condition\Product\Interceptor), Object(Magento\Catalog\Model\Product\Interceptor))
#2 /site.com/dev/vendor/magento/framework/Interception/Interc" while reading response header from upstream

My store only has simple products. Any help would be greatly appreciated.

1 Answer 1

1

I figured it out... apparently I needed the entire product model loaded.

Assuming $this->product is Magento\Catalog\Model\Product;

$item = $this->product->setProduct($product);

$validate = $rule->getActions()->validate($product);

It now works

1
  • This help me. Thanks Adam! Commented Dec 13, 2021 at 19:58

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.