Guillaume_KS Posted April 21, 2023 Share Posted April 21, 2023 Bonjour, je créer des règles paniers à la volée grâce à un csv et je souhaite ajouter des restrictions produits sur ces règles paniers. L'objet CartRule ne permet pas de le faire me semble-t-il, et je constate qu'en base de données, 3 tables doivent être alimentés : cart_rule_product_rule cart_rule_product_rule_group cart_rule_product_rule_value J'ai beau chercher je ne trouve pas de solution, est-ce que quelqu'un peut m'aider ? J'ai chercher à utiliser aussi l'AdminCartRulesController mais rien n'y fait, voici mon code (Prestashop 8.0) $date = strtotime($r['date']); $date_formated = date("Y-m-d", $date); $id_product = Product::getIdBy(['external_reference' => $r['code_produit'] ]); $id_group = Group::getIdBy(['external_reference' => $r['cat_tarifaire'] ]); $id_customer = Customer::getIdBy(['external_reference' => $r['code_client'] ]); $cartRule = new CartRule(); if($id_customer) { $cartRule->id_customer = $id_customer; } $cartRule->name = array($this->langId => "test_promoX+Y"); $cartRule->date_from = $date_formated; $cartRule->date_to = "2050-01-01"; $cartRule->active = true; $cartRule->group_restriction = true; $cartRule->product_restriction = true; if($id_product) { $cartRule->gift_product = $id_product; } /*$test = new AdminCartRulesControllerCore(); $test->postProcess(); $test->processAdd(); $test->processSave(); dump($test);*/ $cartRule->save(); $id_cart_rule = $cartRule->id; if($id_group > 0) { $groupRestriction = \Db::getInstance()->getValue( "INSERT INTO "._DB_PREFIX_."cart_rule_group (id_cart_rule, id_group) VALUES ($id_cart_rule, $id_group) " ); } if (is_array($ruleGroupArray = Tools::getValue('product_rule_group')) && count($ruleGroupArray)) { foreach ( $ruleGroupArray as $ruleGroupId ) { Db::getInstance()->execute( 'INSERT INTO `' . _DB_PREFIX_ . 'cart_rule_product_rule_group` (`id_cart_rule`, `quantity`) VALUES (' . (int)$id_cart_rule . ', ' . (int)Tools::getValue( 'product_rule_group_' . $ruleGroupId . '_quantity' ) . ')' ); $id_product_rule_group = Db::getInstance()->Insert_ID(); if ( is_array( $ruleArray = Tools::getValue( 'product_rule_' . $ruleGroupId ) ) && count( $ruleArray ) ) { foreach ( $ruleArray as $ruleId ) { Db::getInstance()->execute( 'INSERT INTO `' . _DB_PREFIX_ . 'cart_rule_product_rule` (`id_product_rule_group`, `type`) VALUES (' . (int)$id_product_rule_group . ', "' . pSQL( Tools::getValue( 'product_rule_' . $ruleGroupId . '_' . $ruleId . '_type' ) ) . '")' ); $id_product_rule = Db::getInstance()->Insert_ID(); $values = []; foreach ( Tools::getValue( 'product_rule_select_' . $ruleGroupId . '_' . $ruleId ) 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 ) ); } } } } } dump($ruleGroupArray); return true; } La fin à partir d' if($ruleGroupArray... est ue reprise du controller qui ne fonctionne pas. Link to comment Share on other sites More sharing options...
AfterGlow93 Posted April 23, 2023 Share Posted April 23, 2023 (edited) Bonjour, Avez-vous écrit une partie du code vous même ? Il y'a quelques erreurs de syntaxe, et surtout, certaines variables sortent de nulle part. Il y'a déjà un problème ici : Quote $groupRestriction = \Db::getInstance()->getValue( "INSERT INTO "._DB_PREFIX_."cart_rule_group (id_cart_rule, id_group) VALUES ($id_cart_rule, $id_group) " ); Dans un premier temps, on ne peut pas utiliser un INSERT INTO via getValue(), un execute() est nécessaire. Ensuite, la variable $groupRestriction n'est elle pas simplement liée à CartRule->group_restriction ? $ruleGroupArray ne semble définie nulle part ?? S'agit-il d'un override ? De quelle contrôleur ? Comment est appelé ce script ? Manuellement ? Via une action (Hook) ? Edited April 23, 2023 by AfterGlow93 (see edit history) Link to comment Share on other sites More sharing options...
Guillaume_KS Posted April 24, 2023 Author Share Posted April 24, 2023 Bonjour, je pense avoir fait un doublon de sujet malheureusement. C'est l'admin controller cart rule qui à fait cette erreur Link to comment Share on other sites More sharing options...
Guillaume_KS Posted April 24, 2023 Author Share Posted April 24, 2023 https://www.prestashop.com/forums/topic/1076027-créer-une-restriction-produit-pour-règle-panier-via-le-code-résolu/#comment-3442666 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