Bugas Posted November 14, 2019 Share Posted November 14, 2019 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 More sharing options...
EvaF Posted November 14, 2019 Share Posted November 14, 2019 (edited) 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 Edited November 14, 2019 by EvaF (see edit history) Link to comment Share on other sites More sharing options...
Bugas Posted November 15, 2019 Author Share Posted November 15, 2019 Thank You! It's like i expected, but i thought, i would ask before i go into some serious coding i really don't like putting values directly into tables Link to comment Share on other sites More sharing options...
EvaF Posted November 15, 2019 Share Posted November 15, 2019 (edited) 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(); 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(); 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")'); Edited November 20, 2019 by EvaF (see edit history) 1 1 Link to comment Share on other sites More sharing options...
Bugas Posted November 15, 2019 Author Share Posted November 15, 2019 Wow, that's a great. great piece of advice! You almost done it for me! Much apprecieted!! Really great job, thank You! Link to comment Share on other sites More sharing options...
EvaF Posted November 15, 2019 Share Posted November 15, 2019 you're welcome.. This way I am learning to work with Prestashop Link to comment Share on other sites More sharing options...
Bugas Posted November 20, 2019 Author Share Posted November 20, 2019 Thanks to Your post it was almost copy-paste Just for the future use, if You could, please edit Your step 3) , there's a typo: manufactures = manufacturers Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now