Reputation: 389
I created a code to create new cart rule, it works great, but there's one issue, the restriction by product is not working... Hoping someone can help, I tried looking the PrestaShop classes and controllers, and I tried to replicate, but this is what I got to and didn't work.
$coupon = new Discount();
$coupon->quantity = 1;
$coupon->quantity_per_user = 1;
$coupon->id_discount_type = 2;// reduction amount
$coupon->value = '10';
$coupon->id_customer = 1;
$coupon->minimum_amount = 0;
$coupon->minimum_amount_currency = 1;
$coupon->minimum_amount_tax = 0;
$coupon->minimum_amount_shipping = 0;
$coupon->quantity = 1;
$coupon->quantity_per_user = 1;
$coupon->product_restriction = 1;
$coupon->product_rule_group[] = 1;
$coupon->product_rule_group_1_quantity = 1;
$coupon->product_rule_1[] = 1;
$coupon->product_rule_1_1_type = 'products';
$coupon->product_rule_select_1_1[] = 9;
$coupon->reduction_percent = 100;
$coupon->reduction_amount = 0;
$coupon->reduction_currency = 1;
$coupon->reduction_tax = 0;
$coupon->apply_discount_to = 'specific';
$coupon->reductionProductFilter = '191072 Air Freshener Refill';
$coupon->reduction_product = 6;
$coupon->free_gift = 0;
$start_date = date('Y-m-d H:i:s');
$coupon->date_from = $start_date;
$end_date = date('Y-m-d H:i:s'); //some end date
$coupon->date_to = $end_date;
$gen_pass = strtoupper(Tools::passwdGen(8));
$vouchercode = 'somecode';
$name_v = $vouchercode.'-'.$gen_pass;
$namelang = array();
$namelang[1] = $name_v;
$namelang[2] = $name_v;;
//Add Name array
$coupon->name = $namelang;
$current_language = 1;
$coupon->id_customer = 1;
// fixed bug for currency
$coupon->reduction_currency = 1;
$coupon->minimum_amount_currency = 1;
$code_v = $vouchercode.'-'.$gen_pass;
$coupon->code = $code_v;
//$coupon->minimal = $coupon->value;
$coupon->active = 1;
//$coupon->cart_display = 1;
//$coupon->cart_rule_restriction = 0;
$coupon->description = '';
$coupon->highlight = 1;
$coupon->add();
Upvotes: 2
Views: 3160
Reputation: 1447
If you don't want to engage in complex codes, this method may help you:
create Sample disabled coupon with your restrictions on admin panel
create object of your Sample coupon on your php file
clone object to newObject
change (code and active and etc) then add new coupon from it
Upvotes: 0
Reputation: 501
i know it doesn't matter now but, anyways, if you're looking for a way to tie or add products to a specific cart rule, this might help
$products = [1 => "32232", 2 => "23232", 3 => "45343"];
So, after you $cartrule->add()
you get, $cartRuleId = $cartrule->id
$coupon->product_restriction = 1; //enable product restriction
$coupon->reduction_product = -2; // write this only if you want the discount to be applied on the products in the cart and not the whole cart
(new Cart( $cart->id) )->addCartRule( (int) $cartRuleId); // apply the cart rule to this existing cart
# first, save `id_cart_rule` & `quantity` and get id_product_rule_group
Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cart_rule_product_rule_group` (`id_cart_rule`, `quantity`)
VALUES ('.(int)$cartRuleId.', "'.(int)$qty.'")');
$id_product_rule_group = Db::getInstance()->Insert_ID();
# second, save `id_product_rule_group` & `type` and get id_product_rule
Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cart_rule_product_rule` (`id_product_rule_group`, `type`)
VALUES ('.(int)$id_product_rule_group.', "'.$type.'")');
$id_product_rule = Db::getInstance()->Insert_ID();
# finally, using id_product_rule assign products
foreach ($products as $id) {
$values[] = '('.(int)$id_product_rule.','.(int)$id.')';
}
$values = array_unique($values);
if (count($values)) {
Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cart_rule_product_rule_value` (`id_product_rule`, `id_item`) VALUES '.implode(',', $values));
}
Upvotes: 0
Reputation: 1317
We recommend you to use following code to create a cart rule.
Db::getInstance()->execute('INSERT INTO ' . _DB_PREFIX_ . 'cart_rule_shop
set id_cart_rule = ' . (int) $cart_rule_id . ', id_shop = ' . (int) $this->context->shop->id);
Db::getInstance()->execute('INSERT INTO ' . _DB_PREFIX_ . 'cart_rule_lang
set id_cart_rule = ' . (int) $cart_rule_id . ', id_lang = ' . (int) $this->context->language->id . ',
name = "' . strip_tags($coupon_name) . '"');
Db::getInstance()->execute('INSERT INTO ' . _DB_PREFIX_ . 'cart_rule_product_rule_group
set id_product_rule_group = NULL, id_cart_rule = ' . (int) $cart_rule_id . ',
quantity = 1');
$product_rule_group_id = Db::getInstance()->Insert_ID();
Db::getInstance()->execute('INSERT INTO ' . _DB_PREFIX_ . 'cart_rule_product_rule
set id_product_rule = NULL, id_product_rule_group = ' . (int) $product_rule_group_id . ',
type = "products"');
$product_rule_id = Db::getInstance()->Insert_ID();
Db::getInstance()->execute('INSERT INTO ' . _DB_PREFIX_ . 'cart_rule_product_rule_value
set id_product_rule =' . (int) $product_rule_id . ', id_item = ' . (int) $id_product . '');
Upvotes: 1