Jump to content

Cart Rule with Manufacturers


Bugas

Recommended Posts

How to make a coupon via php with manufacturers restriction (so that it works for only one manufacturers products)?

My code at the moment:

$coupon = new Discount();
  $coupon->quantity = 1;
  $coupon->quantity_per_user = 1;
  @$coupon->id_discount_type = 1;
  @$coupon->value = 2;
  $coupon->reduction_tax=1;
  $coupon->partial_use = 0;

  $start_date = date('Y-m-d H:i:s');
  $coupon->date_from = $start_date;
  

  $coupon->date_to = '2020-01-01 00:01:00';

  $gen_pass = strtoupper(Tools::passwdGen(8));
  $vouchercode = 'TT_'.$gen_pass;
  $name_v = 'TEST COUPON';
  $coupon->name = array('1'=>$name_v);
  $coupon->code = $vouchercode;

  $current_language = (int)$cookie->id_lang;

  $coupon->id_customer = '';

	 $coupon->add();

 

I know there's an object, but i don't understand how to add manufacturers restriction?

public static $definition = array(
 'table' => 'cart_rule',
 'primary' => 'id_cart_rule',
 'multilang' => true,
 'fields' => array(
  'id_customer' =>    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
  'date_from' =>	 array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'required' => true),
  'date_to' =>	 array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'required' => true),
  'description' =>    array('type' => self::TYPE_STRING, 'validate' => 'isCleanHtml', 'size' => 65534),
  'quantity' =>	 array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
  'quantity_per_user' =>   array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
  'priority' =>	 array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
  'partial_use' =>    array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'code' =>	  array('type' => self::TYPE_STRING, 'validate' => 'isCleanHtml', 'size' => 254),
  'minimum_amount' =>   array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'),
  'minimum_amount_tax' =>  array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'minimum_amount_currency' =>array('type' => self::TYPE_INT, 'validate' => 'isInt'),
  'minimum_amount_shipping' =>array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'country_restriction' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'carrier_restriction' =>  array('type' => self::TYP
E_BOOL, 'validate' => 'isBool'),
  'group_restriction' =>   array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'cart_rule_restriction' =>  array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'product_restriction' =>  array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'shop_restriction' =>   array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'free_shipping' =>    array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'reduction_percent' =>   array('type' => self::TYPE_FLOAT, 'validate' => 'isPercentage'),
  'reduction_amount' =>   array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'),
  'reduction_tax' =>    array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'reduction_currency' =>  array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
  'reduction_product' =>   array('type' => self::TYPE_INT, 'validate' => 'isInt'),
  'gift_product' =>    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
  'gift_product_attribute' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
  'highlight' =>	 array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'active' =>	 array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'date_add' =>	 array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
  'date_upd' =>	 array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
  // Lang fields
  'name' =>	  array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCleanHtml', 'required' => true, 'size' => 254),
 ),
);
Link to comment
Share on other sites

there are no objects for productRules, productRuleGroups and productRuleValues   in Prestashop ( I can be wrong - of course) - productRuleGroups, productRules and productRuleValues are realized via SQL.
inspire yoursef in controllers/admin/AdminCartRulesController.php function

protected function afterAdd($currentObject) {
...
}



the part   // Add product rule restrictions
 

 

 

PS.png

Edited by EvaF (see edit history)
Link to comment
Share on other sites

no problem, all three tables(ps_cart_rule_product_rule...) are very simple (I am working with Prestashop 1.7.5.1, but I do not expect any changes compared to PS 1.6 in this area)
steps:
1) set cart_rule->product_restriction

$coupon->cart_rule->product_restriction = 1;     // Do not forget to activate product_restriction!!! ( according your $coupon structure)

2) insert into ps_cart_rule_product_rule_group table, got  id_product_rule_group of inserted row ( at the picture bellow id_product_rule_group = 14)

$quantity = 1;  // set yourself
$id_cart_rule = $coupon->cart_rule->id;  // set yourself according your code

Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'cart_rule_product_rule_group` (`id_cart_rule`, `quantity`)
				VALUES (' . (int) $id_cart_rule . ', ' . (int) $quantity . ')');
                $id_product_rule_group = Db::getInstance()->Insert_ID();

PS2.png.aed532543e83b453b32a5002bbc8af01.png

3)  insert into ps_cart_rule_product_rule table, got  id_product_rule of inserted row ( at the picture bellow id_product_rule = 13), you can set type "manufacturers" by hand

Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'cart_rule_product_rule` (`id_product_rule_group`, `type`)
						VALUES (' . (int) $id_product_rule_group . ', "manufacturers")');
                        $id_product_rule = Db::getInstance()->Insert_ID();

PS1.png.92a140286b77ee9fe13fc2022777db48.png

4) insert into ps_cart_rule_product_value table the id(s) of manufacturers

 Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'cart_rule_product_rule_value` (`id_product_rule`, `id_item`) SELECT ' .
                            $id_product_rule . ',id_manufacturer FROM `' . _DB_PREFIX_ . 'manufacturer` where name in ("LG","DMECG")');

PS3.png.0a64416693f2a70365406f6d56ff413a.png

PS4.png.7dee878c1544bb7862ca1d0f8f07cc0f.png

Edited by EvaF (see edit history)
  • Like 1
  • Thanks 1
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...