Jump to content

Créer une restriction produit pour règle panier via le code (RÉSOLU)


Recommended Posts

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.

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

J'ai trouver si jamais quelqu'un passe par la et à la même question

 

            $type = 1;

            Db::getInstance()->execute(
                "INSERT INTO " . _DB_PREFIX_ . "cart_rule_product_rule_group
                 (id_cart_rule, quantity)
				VALUES ($id_cart_rule, 1)");
            $id_product_rule_group = \Db::getInstance()->Insert_ID();

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

            Db::getInstance()->execute(
                "INSERT INTO " . _DB_PREFIX_ . "cart_rule_product_rule_value
                    (id_product_rule, id_item)
                    VALUES ($id_product_rule, $id_product)");

 

Link to comment
Share on other sites

  • Guillaume_KS changed the title to Créer une restriction produit pour règle panier via le code (RÉSOLU)

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...