5

I have a custom quote item attribute (v_code) and I want to use this when creating a cart sales rule.

Scenario: If a cart item has a v_code, it's included/excluded from the discount.

I've managed to add it to the Conditions section but I am unable to add it to the Action conditions.

I would like to add it here.

enter image description here

Any help or direction would be appreciated.

1 Answer 1

1

here's the answer.

Create di.xml file:

....

    <type name="Magento\SalesRule\Model\Rule\Condition\Product\Combine">
        <plugin name="pluginnamehere"
                type="Vendor\ModuleName\Plugin\SalesRule\Condition\FileNameCondition"/>
    </type>
....

file: Vendor\ModuleName\Plugin\SalesRule\Condition\FileNameCondition.php

namespace Vendor\ModuleName\Plugin\SalesRule\Condition;

use Magento\SalesRule\Model\Rule\Condition\Product\Combine;
use Magento\SalesRule\Model\Rule\Action\FileNameCondition;

class FileNameCondition
{
    /**
     * @param Combine $subject
     * @param array $result
     *
     * @return array
     */
    public function afterGetNewChildSelectOptions(
        Combine $subject,
        array $result
    ): array
    {
        $groupLabel = __('Cart Item Attribute');
        $conditionAdded = false;

        foreach ($result as &$condition) {
            if (isset($condition['value'], $condition['label'])
                && is_array($condition['value'])
                && $condition['label']->getText() === $groupLabel->getText()
            ) {
                $condition['value'][] = $this->getCondition();
                $conditionAdded = true;
                break;
            }
        }

        if (!$conditionAdded) {
            // if group of "Cart Item Attribute" not founded then add condition separately
            $result[] = $this->getCondition();
        }

        return $result;
    }

    /**
     * @return array
     */
    private function getCondition(): array
    {
        return [
            'value' => \Vendor\ModuleName\Model\Rule\Condition\NewActionCondition.php::class,
            'label' => __('My new action condition')
        ];
    }
}
...

file: Vendor\ModuleName\Model\Rule\Condition\NewActionCondition.php

namespace Vendor\ModuleName\Model\Rule\Action;

use Magento\Config\Model\Config\Source\Yesno;
use Magento\Framework\Model\AbstractModel;
use Magento\Rule\Model\Condition\AbstractCondition;
use Magento\Rule\Model\Condition\Context;

class NewActionCondition extends AbstractCondition
{
    /**
     * @var Yesno
     */
    protected $sourceYesno;

    /**
     * @param Context $context
     * @param Yesno $sourceYesno
     * @param array $data
     */
    public function __construct(
        Context $context,
        Yesno $sourceYesno,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->sourceYesno = $sourceYesno;
    }

    /**
     * Load attribute options
     * @return $this
     */
    public function loadAttributeOptions()
    {
        $this->setAttributeOption([
            'new_action_condition' => __('My new action condition')
        ]);
        return $this;
    }

    /**
     * Get input type
     * @return string
     */
    public function getInputType()
    {
        return 'select';
    }

    /**
     * Get value element type
     * @return string
     */
    public function getValueElementType()
    {
        return 'select';
    }

    /**
     * Get value select options
     * @return array|mixed
     */
    public function getValueSelectOptions()
    {
        if (!$this->hasData('value_select_options')) {
            $this->setData(
                'value_select_options',
                $this->sourceYesno->toOptionArray()
            );
        }
        return $this->getData('value_select_options');
    }

    /**
     * @param AbstractModel $model
     * @return bool
     */
    public function validate(AbstractModel $model)
    {
        //insert here your validation code
        return true;
    }
}
...

Cheers :D

Sorry for the delay

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.