我们希望在比较当前产品的产品页面上进行快速比较,然后再比较该类别中的两个畅销产品。我有所有的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是否已经有内置函数来提取产品功能
答案 0 :(得分:1)
使用MySQL,您可以尝试使用可能有助于此查询的GROUP_CONCAT()。但是,我们编写了一个快速代码段,为您提供了可以执行的操作的概述。
此代码:
以下是最终结果:
代码:
<?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(而不是直接在控制器中显示这些代码)。