0

As per my requirement am creating the cart rule programmatically, for that i am reviewed the following code and implemented in my local

public function createRule()
{
    $price = 100;
    $sku = '24-WG085';
    $discount = ($price - (($price + 1) / 2));
    $shoppingCartPriceRule = $this->ruleFactory->create();

    $shoppingCartPriceRule->setName('Add a second for $1 - ' . $sku)
        ->setDescription('Buy one item at regular price, and receive a second item for just $1.00 more!')
        ->setFromDate('2000-01-01')
        ->setToDate(NULL)
        ->setUsesPerCustomer('0')
        ->setCustomerGroupIds(array('0','1','2','3',))
        ->setIsActive('1')
        ->setStopRulesProcessing('0')
        ->setIsAdvanced('1')
        ->setProductIds(NULL)
        ->setSortOrder('1')
        ->setSimpleAction('by_fixed')
        ->setDiscountAmount($discount)
        ->setDiscountQty(NULL)
        ->setDiscountStep('0')
        ->setSimpleFreeShipping('0')
        ->setApplyToShipping('0')
        ->setTimesUsed('0')
        ->setIsRss('0')
        ->setWebsiteIds(array('1',))
        ->setCouponType('1')
        ->setCouponCode(NULL)
        ->setUsesPerCoupon(NULL);

    $item_found = $this->foundProductRuleFactory->create()
        ->setType('Magento\SalesRule\Model\Rule\Condition\Product\Found')
        ->setValue(1) // 1 == FOUND
        ->setAggregator('all'); // match ALL conditions
    $shoppingCartPriceRule->getConditions()->addCondition($item_found);

    $conditions = $this->productRuleFactory->create()
        ->setType('Magento\SalesRule\Model\Rule\Condition\Product')
        ->setAttribute('sku')
        ->setOperator('==')
        ->setValue($sku);
    $item_found->addCondition($conditions);

    $actions = $this->productRuleFactory->create()
        ->setType('Magento\SalesRule\Model\Rule\Condition\Product')
        ->setAttribute('sku')
        ->setOperator('==')
        ->setValue($sku);
    $shoppingCartPriceRule->getActions()->addCondition($actions);

    $this->ruleResource->save($shoppingCartPriceRule);
}

But, when i execute the code i am getting the 'Uncaught Error: Call to a member function setOperator() on null.

I am not sure what is the exact problem in that setOperator, the following operators are available in core

$this->setOperatorOption(
            [
                '==' => __('is'),
                '!=' => __('is not'),
                '>=' => __('equals or greater than'),
                '<=' => __('equals or less than'),
                '>' => __('greater than'),
                '<' => __('less than'),
                '()' => __('is one of'),
                '!()' => __('is not one of'),
            ]
        );

Thanks in advance for your support and suggestion

0

I got the answer for my question, instead of setAttribute i tried to setData('attribute','sku') it's working fine.

$conditions = $this->_productRuleFactory->create()
                    ->setType('Magento\SalesRule\Model\Rule\Condition\Product')
                    ->setData('attribute','sku')
                    ->setData('operator','()')
                    ->setValue($requestedParameters['SKU']);
$item_found->addCondition($conditions);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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