2

我们希望在产品页面上进行快速比较,比较当前产品,然后再比较该类别中的两个最畅销产品。我拥有所有 SQL 来获取当前产品和最畅销的两个产品,但是在向这些产品添加功能时遇到了麻烦。这就是我所面临的。

这只是一个例子。此 SQL 将在一个类别中提取产品功能。

SELECT ps_product.id_product, ps_feature_lang.name, ps_feature_value_lang.value FROM ps_product LEFT JOIN ps_feature_product ON ps_feature_product.id_product = ps_product.id_product LEFT JOIN ps_feature_lang ON ps_feature_lang.id_feature = ps_feature_product.id_feature LEFT JOIN ps_feature_value_lang ON ps_feature_value_lang.id_feature_value = ps_feature_product.id_feature_value WHERE ps_product.id_category_default = '6' AND ps_feature_lang.id_feature IS NOT NULL

它返回

id_product | name | value
 9          Height  5
 9          Width   5
 9          Depth   5
 9          Weight  5
10          Height  10

我尝试按 id_product 进行分组,但当然这只会显示该产品功能列表中的值。我也尝试将它们按产品放入数组中,但无法让它们在前端正确排列。

我不确定 prestashop 是否已经有内置功能来提取产品功能

4

1 回答 1

2

使用 MySQL,您可以尝试使用 GROUP_CONCAT() 来帮助查询。但是,我们编写了一个快速代码片段,让您大致了解您可以做什么。

这段代码:

  • 检索当前产品类别的两个畅销书
  • 检索 3 个产品的功能列表(当前产品 + 2 个畅销产品)
  • 将它们分组并显示到 HTML 表格中

这是最终结果:

产品对比

代码:

    <?php

include(dirname(__FILE__).'/config/config.inc.php');
include(dirname(__FILE__).'/init.php');
ini_set('display_errors', 'On');

$id_product = 1; /* Current Product ID */
$id_category = 2; /* Current Category ID */
$id_lang = 1; /* Current Language ID */

/* Retrieve the two Best sellers for this category, excluding the current product */
$best_sellers = Db::getInstance()->ExecuteS('
SELECT ps.id_product, pl.name product_name
FROM '._DB_PREFIX_.'product_sale ps
LEFT JOIN '._DB_PREFIX_.'category_product cp ON (cp.id_product = ps.id_product)
LEFT JOIN '._DB_PREFIX_.'product p ON (p.id_product = cp.id_product)
LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = p.id_product)
WHERE cp.id_category = '.(int)$id_category.' AND cp.id_product != '.(int)$id_product.' AND p.active = 1 AND pl.id_lang = '.(int)$id_lang.'
ORDER BY ps.sale_nbr DESC
LIMIT 2');

if (count($best_sellers))
{
    $products_to_compare = array();
    $products_names = array();
    foreach ($best_sellers as $best_seller)
    {
        $products_to_compare[] = (int)$best_seller['id_product'];
        $products_names[(int)$best_seller['id_product']] = $best_seller['product_name'];
    }
    $products_to_compare[] = (int)$id_product;
    $products_names[(int)$id_product] = 'Current product'; /* Replace by the current product name */

    $features = Db::getInstance()->ExecuteS('
    SELECT fp.id_product, fp.id_feature, fl.name feature_name, fvl.value feature_value
    FROM '._DB_PREFIX_.'feature_product fp
    LEFT JOIN '._DB_PREFIX_.'feature_lang fl ON (fl.id_feature = fp.id_feature)
    LEFT JOIN '._DB_PREFIX_.'feature_value_lang fvl ON (fvl.id_feature_value = fp.id_feature_value)
    WHERE fp.id_product IN ('.pSQL(implode(',', $products_to_compare)).')');

    $features_to_display = array();
    foreach ($features as $feature)
    {
        if (!isset($features_to_display[(int)$feature['id_feature']]))
        {
            $features_to_display[(int)$feature['id_feature']] = array();
            $features_to_display[(int)$feature['id_feature']]['name'] = $feature['feature_name'];
        }

        $features_to_display[(int)$feature['id_feature']][(int)$feature['id_product']] = $feature['feature_value'];
    }

    echo '
    <table cellpadding="5" cellspacing="0" border="1">
        <tr>
            <td></td>';

    foreach ($products_to_compare as $product_to_compare)
            echo '<td>'.($product_to_compare == $id_product ? '<b>' : '').Tools::safeOutput($products_names[(int)$product_to_compare]).($product_to_compare == $id_product ? '</b>' : '').'</td>';

    echo '</tr>';

    foreach ($features_to_display as $feature_to_display)
    {
        echo '
        <tr>
            <td>'.Tools::safeOutput($feature_to_display['name']).'</td>';

        foreach ($products_to_compare as $product_to_compare)
            echo '<td>'.($product_to_compare == $id_product ? '<b>' : '').Tools::safeOutput($feature_to_display[(int)$product_to_compare]).($product_to_compare == $id_product ? '</b>' : '').'</td>';

        echo '
        </tr>';
    }

    echo '
    </table>';

}
else
    die('Sorry, no best sellers for this category');

可以改进此代码以节省内存并将结果传输到 Smarty(而不是直接在控制器中显示这些)。

于 2013-06-18T15:28:38.523 回答