0

I have added custom option for 'Apply' in Cart price rule. But when trying add product to cart I get following error place_x_receive_y_free is unknown type {"exception":"[object] (InvalidArgumentException(code: 0): place_x_receive_y_free is unknown type at /app/vendor/magento/module-sales-rule/Model/Rule/Action/Discount/CalculatorFactory.php:51)"}

place_x_receive_y_free -> this is my custom option i have added in Apply. What is wrong?How can I fix this?

1

1 Answer 1

0

you need to properly override Magento sales rules

  1. di.xml
 <?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\SalesRule\Model\Rule\Metadata\ValueProvider">
  <plugin name="Yourmodule-salesrule-plugin" type="YourModule\YourFile\Plugin\Rule\Metadata\ValueProvider" sortOrder="1" />
</type>
<type name="Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory">
    <arguments>
        <argument name="discountRules" xsi:type="array">
            <item name="place-x-receive-y-free." xsi:type="string">YourModule\YourFile\Model\Rule\Action\Discount\PlaceXReceiveYFree </item>                               
        </argument>
    </arguments>
</type>
  1. Events.xml file

     <!-- language: lang-xml -->
     <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
         <event name="salesrule_validator_process">
            <observer name="your_custom_rules" instance="YourModule\YourFile\Observer\PlaceXReceiveYFree" />
        </event>
    </config>
    
  2. Observerfile PlaceXReceiveYFree.php and then write your logic in this observer file to properly override

2
  • How can implement item qty level discount in fronted. I have try this plugin <type name="Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory"> but not working. magento.stackexchange.com/questions/365294/… Mar 14, 2023 at 7:40
  • have you tried printing object from observer file $address = $observer->getEvent()->getAddress(); then foreach ($address->getAllItems() as $q): $validate = $rule->getActions()->validate($q); if ($validate): you can write your custom logic with frontend added cart items
    – Ishak Ali
    Mar 15, 2023 at 13:50

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.