3

I'm trying to port over tens of thousands of coupons from a custom e-commerce website into Magento 2.0.8 and I'm having a hard time getting it to work. These are simple coupons either fixed $ or % off of the carts subtotal. I have been unable to get it to save from the command line.

I have a command called coupons:migrate that I'm running this code in. This is what I have so far in my createCoupon method that accepts the current coupons row (looping over a CSV file):

protected function createCoupon($coupon) {
        $shoppingCartPriceRule = $this->objectManager->create('Magento\SalesRule\Model\Rule');

        $shoppingCartPriceRule->setName($coupon['name'])
            ->setDescription($coupon['name'])
            ->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')
            ->setCouponCode($coupon['code'])
            ->setUsesPerCoupon(NULL);

    return $shoppingCartPriceRule->save();

}

I'm currently getting this error from my database:

[Zend_Db_Statement_Exception]
  SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction, query was: INSERT INTO `magento_reward_salesrule` (`rule_id`,`points_delta`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `points_delta` = VALUES
  (`points_delta`)

  [PDOException]
  SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction

Any help would be greatly appreciated. I'm basically stuck now.

  • How about your current issue? – Khoa TruongDinh Aug 26 '16 at 14:25
  • @B.Mackswell, did the answer help you to solve the problem? If so, please accept the answer. If not, please inform us. – 7ochem Oct 10 '16 at 7:03
0

I don't try to test your code, however, seem that your code lines need to improve the performance. After saving the object model, we need to unset the data object:

$shoppingCartPriceRule->save()
$shoppingCartPriceRule->unsetData(); 
0

You can try below code:

protected $ruleFactory

public function __construct(\Magento\SalesRule\Model\RuleFactory $ruleFactory) {
    $this->rulesFactory = $ruleFactory }


$ruleData = [
            "name" => "Buy 3 tee shirts and get the 4th free",
            "description" => "Buy 3 tee shirts and get the 4th free",
            "from_date" => null,
            "to_date" => null,
            "uses_per_customer" => "0",
            "is_active" => "1",
            "stop_rules_processing" => "0",
            "is_advanced" => "1",
            "product_ids" => null,
            "sort_order" => "0",
            "simple_action" => "buy_x_get_y",
            "discount_amount" => "1.0000",
            "discount_qty" => null,
            "discount_step" => "3",
            "apply_to_shipping" => "0",
            "times_used" => "0",
            "is_rss" => "1",
            "coupon_type" => "NO_COUPON",
            "use_auto_generation" => "0",
            "uses_per_coupon" => "0",
            "simple_free_shipping" => "0",
            "customer_group_ids" => [0, 1, 2, 3],
            "website_ids" => [1],
            "coupon_code" => null,
            "store_labels" => [],
            "conditions_serialized" => '',
            "actions_serialized" => ''
        ];

$ruleModel = $this->ruleFactory->create();
$ruleModel->setData($ruleData);
$ruleModel->save();
0

Try code below

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');  

$coupon['name'] = 'test';
$coupon['desc'] = 'Discount for Test';
$coupon['start'] = date('Y-m-d');
$coupon['end'] = '';
$coupon['max_redemptions'] = 1;
$coupon['discount_type'] = 'by_percent';
$coupon['discount_amount'] = 15;
$coupon['flag_is_free_shipping'] = 'no';
$coupon['redemptions'] = 1;
$coupon['code'] = 'test-1234'; //this code will normally be autogenetated but i am hard coding for testing purposes

$shoppingCartPriceRule = $objectManager->create('Magento\SalesRule\Model\Rule');
$shoppingCartPriceRule->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)
    ->setCouponCode($coupon['code'])
    ->setUsesPerCoupon(NULL);
$shoppingCartPriceRule->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.