João Rafael
João Rafael

Reputation: 1

Same Products Different Product Lines - Prestashop 1.7.6.7

I've been battling this problem the entire week and I am lost all of hope... We have a Prestashop 1.7.6.7 store and we need to add 2x Services to each product in a specific category.

My problem comes when I try to add 2products with the same service.

I get: Product X Product Y 2x Installation Product X 2x Installation Product y

When the desire behaviour is

Product X Product Y Installation Product X Installation Product Y

I've tried an override in Cart.php addProductToCart()

public function getProducts($refresh = false, $id_product = false, $id_country = null, $fullInfos = true){
    // Get all products from the parent method.
    $parentProducts = parent::getProducts($refresh, $id_product, $id_country, $fullInfos);
    $filteredProducts = array();$serviceTemplate = null;
    
    // Filter out any service product (ID 12565) from the parent results,
    // and save one instance as a template.
    foreach ($parentProducts as $prod) {
        if ((int)$prod['id_product'] !== 12565) {
            $filteredProducts[] = $prod;
        } else {
            if ($serviceTemplate === null) {
                $serviceTemplate = $prod;
            }
        }
    }

    // Retrieve all associations for service product 12565 in this cart.
    $associations = Db::getInstance()->executeS(
        'SELECT base_product_name 
         FROM ' . _DB_PREFIX_ . 'r_myservice_association 
         WHERE id_cart = ' . (int)$this->id . ' 
           AND id_product_service = 12565'
    );

    // Group associations by a normalized version of the base product name
    // so that duplicates collapse (e.g. ignoring case and extra spaces).
    $uniqueAssociations = array();
    if ($associations) {
        foreach ($associations as $assoc) {
            $key = strtolower(trim($assoc['base_product_name']));
            $uniqueAssociations[$key] = $assoc;
        }
    }

    // For each unique association, add exactly one service product line with quantity 1.
    if (!empty($uniqueAssociations) && $serviceTemplate !== null) {
        foreach ($uniqueAssociations as $assoc) {
            $newServiceLine = $serviceTemplate;  // clone the template
            $newServiceLine['cart_quantity'] = 1;   // force quantity to 1
            $newServiceLine['name'] = 'Installation for ' . trim($assoc['base_product_name']);
            $filteredProducts[] = $newServiceLine;
        }
    }

    return $filteredProducts;
}

public function hookActionCartSave($params) { $cart = $params['cart']; if (!$cart) { return; }    
if (Tools::getIsset("id_base_product") && Tools::getIsset('base_product_name')) {
        $idBaseProduct = (int)Tools::getValue('id_base_product');
        $baseName = pSQL(Tools::getValue('base_product_name'));

        $idProductService = (int)Tools::getValue("id_product");
        $sql = 'INSERT INTO `' . _DB_PREFIX_ . 'r_myservice_association`
            (id_cart, id_product_service, id_product_base, base_product_name, date_add, date_upd)
            VALUES (' . (int)$cart->id . ', ' . $idProductService . ', ' . $idBaseProduct . ', "' . $baseName . '", NOW(), NOW())';
        Db::getInstance()->execute($sql);
    }
}`

Upvotes: 0

Views: 21

Answers (0)

Related Questions