4

I want to filter cart rules list in my account page. I've list cart rules in My Account page "Dashboard". But I want to filter cart rules list by condition.

Cart Rules List

I've configure the conditions in cart rules by date of birth. I just want to show the rules if the customer date of birth is same with my conditions.

I got the Data from \Magento\Customer\Block\Account\Dashboard\Info. but I dont know how to filter cart rules list.

$datax = $helper->getSalesRule();
echo '<pre>';
print_r($datax->getData());
echo '</pre>';

and the result

[  
    conditions_serialized
]=>{  
   "type":"Magento\\SalesRule\\Model\\Rule\\Condition\\Combine",
   "attribute":null,
   "operator":null,
   "value":"1",
   "is_value_processed":null,
   "aggregator":"all",
   "conditions":[  
   {  
"type":"Magento\\CustomerSegment\\Model\\Segment\\Condition\\Segment",
     "attribute":false,
     "operator":"==",
     "value":"138,110",
     "is_value_processed":false
   }
 ]
}

1 Answer 1

1

First, you have to get the collection of all rules, then validate those by using validate() function

$rules = $this->ruleFactory->create()->getCollection()

foreach ($rules as $rule) {
            $rule->afterLoad();
            if ($quote->isVirtual()) {
                $address = $quote->getBillingAddress();
            } else {
                $address = $quote->getShippingAddress();
            }
            foreach ($address->getAllItems() as $item) {// total_qty - allowed only in total collection process
                $address->setTotalQty($address->getTotalQty() + $item->getQty());
            }
            if ($rule->validate($address)) {
                $subtotal = 0;
                foreach ($quote->getItemsCollection() as $item) {
                    if ($item->getParentItemId()) {
                        continue;
                    }
                    if ($rule->getConditions()->validate($item)) {
                        $subtotal += $item->getBasePrice() * $item->getQty();
                    }
                }
                if ($subtotal > 0) {
                    return $rule;
                }
            }
        }

Your Answer

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

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