2

I want to generate the coupon for the specific products like I want to generate the coupon for the product with id= 20 whose price is let suppose 30$ and I want to sell it on 15$. How can I generate this programmatically? thanks in Advance

7
  • Refer this link magento.stackexchange.com/questions/207311/…. Go to comments of answer, where you can find this line $shoppingCartPriceRule->setProductIds(array(1,2,3)); for specific product's coupon code. Feb 26 '19 at 11:05
  • @DharaBhatti I'm not getting where is this line $shoppingCartPriceRule->setProductIds(array(1,2,3)); and what about the price limit like I want to fix the price for specific product I dont want to set the price by % I want to set the price fix like one product price is 30$ and I want to set this product price to 20$ fix how can I do this?
    – Asad Khan
    Feb 26 '19 at 11:27
  • Then you have to use $coupon['discount_type'] ='by_fixed'; instead of $coupon['discount_type'] ='by_percent'; And for this line $shoppingCartPriceRule->setProductIds(array(1,2,3));, click on "show 6 more comments" in the comments area of answer. Feb 26 '19 at 11:34
  • @DharaBhatti can you please help me how to get this coupon number in other file actually I want to send this coupon number in email to customer.
    – Asad Khan
    Feb 28 '19 at 9:14
  • First get rule_id using Magento\SalesRule\Model\Coupon class by your coupon code and then load that rule using Magento\SalesRule\Model\Rule class by rule_id. You will get details about your coupon code that you added. Mar 1 '19 at 9:08
1

So at Last I solve this mystery we cannot apply the coupon on the specific product by using getProductIds() to apply the coupon on the specific product we have to add few conditions after the basic coupon code

use Magento\SalesRule\Model\Rule;
 public function __construct(
    \Magento\Framework\App\Helper\Context $helpercontext,
    Rule $rule,
 ){
    $this->_rule = $rule;
    parent::__construct($helpercontext);
 }
 public function setCoupon()
{
    // $this->_state->setAreaCode('adminhtml');  
    // $this->_state->setAreaCode('frontend');  


    $coupon['name'] = 'Offer_asad2';
    $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['code'] ='NL04-1234'; //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(array(1,2,3))
            // ->setProductIds(array('1','2','3'))
            ->setProductIds(1)
            ->setCouponCode($coupon['code'])
            ->setUsesPerCoupon(NULL);
           $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->save();
}

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.