Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
1.4 kB
2
Indexable
Never
public function addProductToCartRuleRestriction($id_product, $id_cart_rule)
{

    $cartRuleProductRuleQuery = "SELECT crprg.id_product_rule_group, MAX(id_product_rule) AS last_id_product_rule
    FROM `" . _DB_PREFIX_ . "_cart_rule_product_rule_group` crprg
             INNER JOIN `" . _DB_PREFIX_ . "_cart_rule_product_rule` crpr ON crprg.id_product_rule_group = crpr.id_product_rule_group
    WHERE crprg.id_cart_rule = " . $id_cart_rule . "
    GROUP BY crprg.id_product_rule_group";

    $cartRuleProductRules = DB::getInstance()->executeS($cartRuleProductRuleQuery);

    $id_product = intval($id_product);
    $cartRuleProductRulesNumber = count($cartRuleProductRules);
    $errors = [];

    if ($cartRuleProductRulesNumber) {

        $cartRuleProductRuleValueQuery = "INSERT INTO `" . _DB_PREFIX_ . "cart_rule_product_rule_value` (`id_product_rule`, `id_item`) 
        VALUES ";

        foreach ($cartRuleProductRules as $key => $item) {

            $cartRuleProductRuleValueQuery .= "(" . (int)$item['last_id_product_rule'] . ", " . $id_product . ")";

            if ($key < ($cartRuleProductRulesNumber - 1)) {
                $cartRuleProductRuleValueQuery .= ", ";
            }
        }

        $queryResult = DB::getInstance()->execute($cartRuleProductRuleValueQuery);

        if (!$queryResult) {
            $errors[] = "Error location : cart_rule_product_rule_value";
        }
    }

    return $errors;
}