Jump to content

Add Customer Group Restriction to CartRule - PHP


bombis

Recommended Posts

Hello!

Does someone know how to add group user restriction to vouchers?

Quote

 $voucher = new CartRule();
        $voucher->id_customer = (int)$id_customer;
        $voucher->code = $code;
        $voucher->name[Configuration::get('PS_LANG_DEFAULT')] = $code;
        $voucher->quantity = 1;
        $voucher->quantity_per_user = 1;
        $voucher->active = true;
        $voucher->shop_restriction = true;
        $voucher->highlight = false;
        $voucher->cart_rule_restriction = true;
        $now = time();
        $voucher->date_from = date('Y-m-d H:i:s', $now);
        $voucher->date_to = date('Y-m-d H:i:s', $now + (3600 * 24 * $validity));

 

I guess i should use  'group_restriction' but it is boolean and i want to restric customer group with id = 4 for example.

 

            'group_restriction' =>        array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
 

Link to comment
Share on other sites

it is analogic  question like this:

https://www.prestashop.com/forums/topic/1008564-cart-rule-with-manufacturers/

instead of "Product selection" is only "Customer group selection"

the query  you have to execute is simplier:

$voucher->cart_rule->group_restriction = 1; 
$id_cart_rule = $voucher->cart_rule->id;  // set yourself according your code

//and add into ps_cart_rule_group

// group by id (f.e. fo id_group=4)
Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'cart_rule_group` (`id_cart_rule`, `id_group`) 
VALUES(' . (int) $id_cart_rule . ', 4));
//or 
// Group by name f.e. for group name = 'Customer'
// Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'cart_rule_group` (`id_cart_rule`, `id_group`)  SELECT ' . (int) $id_cart_rule . ', id_group  FROM `' . _DB_PREFIX_ . 'group_lang` WHERE id_lang=1 and name ='Customer');

 

Link to comment
Share on other sites

14 hours ago, EvaF said:

 


$voucher->group_restriction = 1; 

// group by id (f.e. fo id_group=4)
Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'cart_rule_group` (`id_cart_rule`, `id_group`) 
VALUES(' . (int)$voucher->id . ', 4)');

 

Was looking for a function but i guess we have to use sQL.

Works like  a champ, thanks EvaF!!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...