skip to Main Content

I am creating automatic shopping cart rules on a PRESTASHOP site.

I can’t find the method to put a restriction on a product for the cart rule to be valid. I added the restriction on a specific Group via SQL by inserting the data $id_cart_rule and $id_group, however for a product restriction there are 3 tables that exist: ps_cart_rule_product_rule, ps_cart_rule_product_rule_group and ps_cart_rule_product_rule_value. This implies the creation of object with id for id_product_rule and id_product_rule_group.

I’m stuck, does anyone know how to do?

Here is my current code that creates a basket rule and adds a restriction per group.

$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;
    }

    $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)
            " );
    }


    return true;
}

I tried to search in all prestashop native classes related to the cart rule and products, as well as to find solutions in sql directly

2

Answers


  1. Chosen as BEST ANSWER

    I find the solution, if anybody needs it :

    $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)");
    

  2. You can do this by recalling the cart_rule object and setting the reduction_product property with the array of IDs to be restricted

    $product_ids = array(1, 2, 3); // product IDs to restrict the cart rule to
    $cart_rule->reduction_product = $product_ids;
    

    Then just call the save() or update() method on the cart_rule itself.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search