mysql 在购物车规则上添加产品限制(prestashop 8.0)

ezykj2lf  于 2023-04-29  发布在  Mysql
关注(0)|答案(2)|浏览(176)

我正在创建一个PRESTASHOP网站自动购物车规则。
我找不到对产品设置限制以使购物车规则有效的方法。我通过插入数据$id_cart_rule和$id_group通过SQL添加了对特定组的限制,但是对于产品限制,存在3个表:ps_cart_rule_product_rule、ps_cart_rule_product_rule_group和ps_cart_rule_product_rule_value。这意味着使用id_product_rule和id_product_rule_group的id创建对象。
我被卡住了,有人知道该怎么做吗?
下面是我当前的代码,它创建了一个篮子规则,并为每个组添加了一个限制。
$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;
}

我试图在所有与购物车规则和产品相关的Prestashop原生类中搜索,以及直接在SQL中寻找解决方案

pdsfdshx

pdsfdshx1#

您可以通过调用cart_rule对象并使用要限制的ID数组设置reduction_product属性来实现此操作

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

然后调用cart_rule本身的**保存()update()**方法即可。

sqxo8psd

sqxo8psd2#

我找到了解决办法,如果有人需要的话:

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

相关问题