1

We're using Magento CE v2.2.3 and we're selling a single product (with multiple options) which at the moment only has two prices.

I need to set a promotion where a customer can get a discount of 10% of their first purchase but only to one product even if there are multiple products in cart. I also need to exclude gift cards from discounts.

On my first attempt, I have set a rule that applies a discount to entire cart, which does not work as desired:

Coupon: No Coupon
Uses per Customer: 1
Conditions:
  - If ALL of these conditions are TRUE:
    - If an item is NOT FOUND in the cart with ANY of these conditions true:
      - SKU IS Gift Card
Actions:
  - Apply Percent of product price discount
  - Discount Amount 10
  - Maximum Qty Discount is Applied To 1
  - Discount Qty Step 0
  - Apply to Shipping Amount No
  - Discard subsequent rules Yes

My second attempt was in hope that I could set two rules that would apply either one or second to entire cart, but when adding two products with different prices to cart, both rules are applied, which again is not correct:

Rule 1:

Coupon: No Coupon
Uses per Customer: 1
Conditions:
  - If ALL of these conditions are TRUE:
    - If an item is NOT FOUND in the cart with ANY of these conditions true:
      - SKU IS Gift Card
Actions:
  - Apply Fixed amount discount for whole cart
  - Discount Amount 36.5
  - Maximum Qty Discount is Applied To 1
  - Discount Qty Step 0
  - Apply to Shipping Amount No
  - Discard subsequent rules Yes
Apply the rule:
  - If ALL of these conditions are TRUE:
    - Price in cart is 365.00

Rule 2:

Coupon: No Coupon
Uses per Customer: 1
Conditions:
  - If ALL of these conditions are TRUE:
    - If an item is NOT FOUND in the cart with ANY of these conditions true:
      - SKU IS Gift Card
Actions:
  - Apply Fixed amount discount for whole cart
  - Discount Amount 42.5
  - Maximum Qty Discount is Applied To 1
  - Discount Qty Step 0
  - Apply to Shipping Amount No
  - Discard subsequent rules Yes
Apply the rule:
  - If ALL of these conditions are TRUE:
    - Price in cart is 425.00

Is it even possible to do the above with default Magento or do we need an extension for that?

  • Sir did you solve this problem??? If yes can you please share the code with me? – Asad Khan Apr 4 at 11:11
  • Nope, sorry didn't solve this one ... – Kristjan O. Apr 4 at 20:32
  • I can give you the code if you still need it... I just solved it last night – Asad Khan Apr 5 at 9:13
  • Would appreciate it. – Kristjan O. Apr 5 at 13:14
0

So I fix it for my specific data you can fix it for yours

public function setCoupon()
    {
        // // $this->_state->setAreaCode('adminhtml');  
        // // $this->_state->setAreaCode('frontend'); 


        $coupon['name'] = 'Sheikh The Power';
        $coupon['desc'] = 'Discount for vip signup coupon.';
        $coupon['start'] = date('Y-m-d');
        $coupon['end'] = '';
        $coupon['max_redemptions'] = 1;
        $coupon['discount_type'] ='by_fixed';
        $coupon['discount_amount'] = 15;
        $coupon['flag_is_free_shipping'] = 'no';
        $coupon['redemptions'] = 1;
        // $coupon['produtIds'] = array(1 ,2,3);
        $coupon['produtIds'] = 1;
        $coupon['code'] ='Code-'.uniqid(); //this code will normally be autogenerated but i am hard coding for testing purposes  

        $this->_rule->setName($coupon['name'])
                ->setDescription($coupon['desc'])
                ->setFromDate($coupon['start'])
                ->setToDate($coupon['end'])
                ->setUsesPerCustomer($coupon['max_redemptions'])
                ->setCustomerGroupIds(array('0','1','2','3',))
                ->setIsActive(1)
                ->setSimpleAction($coupon['discount_type'])
                ->setDiscountAmount($coupon['discount_amount'])
                ->setDiscountQty(1)
                ->setApplyToShipping($coupon['flag_is_free_shipping'])
                ->setTimesUsed($coupon['redemptions'])
                ->setWebsiteIds(array('1'))
                ->setCouponType(2)
                // ->setProductIds($this->getSKUU())
                ->setProductIds(NULL)
                ->setCouponCode($coupon['code'])
                ->setUsesPerCoupon(NULL);

            $conditions = array();
            $conditions["1"] = array
                (
                "type" => "Magento\SalesRule\Model\Rule\Condition\Combine",
                "aggregator" => "all",
                "attribute" => null,
                "operator" => null,
                "value" => 1,
                "is_value_processed" => null,
            );
            $conditions["1--1"] = array
                (
                "type" => "Magento\SalesRule\Model\Rule\Condition\Product\Found",
                "attribute" => null,
                "operator" => null,
                "value" => 1,
                "is_value_processed" => null,
                "aggregator" => "all",
            );
            $conditions["1--1--1"] = array
                (
                "type" => "Magento\SalesRule\Model\Rule\Condition\Product",
                "attribute" => "sku",
                "operator" => "==",
                // "operator" => "()",
                // "value" => $couponData['general']['product_id']
                "value" => $this->getSKUU(),
            );
            $conditions["1--1--1-1"] = array
                (
                "type" => "Magento\SalesRule\Model\Rule\Condition\Product",
                // "attribute" => "quote_item_row_total",
                "attribute" => "quote_item_qty",
                "operator" => "==",
                "value" => 1
            );

            $actions = array(
                "1" => array(
                        "type"          => "Magento\SalesRule\Model\Rule\Condition\Product",
                        "aggregator"    => "all",
                        "value"         => "1",
                        "new_child"     => false
                ),
                "1--1" => array(
                        "type"          => "Magento\SalesRule\Model\Rule\Condition\Product",
                        "attribute"     => "sku",
                        'operator' => '==',
                        'value' => $this->getSKUU()
                )
            );

                $this->_rule->setData('conditions', $conditions);
                $this->_rule->setData("actions",$actions);

                $validateResult = $this->_rule->validateData(new \Magento\Framework\DataObject($this->_rule->getData()));
                if ($validateResult !== true) {
                    foreach ($validateResult as $errorMessage) {
                        echo $errorMessage;
                    }
                    return;
                }

                try {
                    $this->_rule->loadPost($this->_rule->getData());
                    $this->_rule->save();

                    // $ruleJob = $obj->get('Magento\CatalogRule\Model\Rule\Job');
                    // $ruleJob->applyAll();
                   // echo "rule created";
                } catch (Exception $e) {
                    echo $e->getMessage();
                }

                $this->_rule->save();
    }

where my di is

public function __construct(

        \Magento\SalesRule\Model\Rule $rule,
        \Magento\Framework\App\State $state,
     ){
        $this->_state = $state;
        $this->_rule = $rule;
     }

I wish next time this code will help you

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.