gpt4 book ai didi

php - Prestashop 产品特性 PHP SQL

转载 作者:行者123 更新时间:2023-11-29 00:29:47 25 4
gpt4 key购买 nike

我们想在产品页面上进行快速比较,比较当前产品,然后比较该类别中的两个畅销产品。我有所有的 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 是否已经内置了拉取产品功能的功能

最佳答案

对于 MySQL,您可以尝试使用有助于此查询的 GROUP_CONCAT()。不过,我们编写了一个快速代码片段,让您大致了解您可以做什么。

这段代码:

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

最终结果如下:

Product comparison

代码:

    <?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(而不是直接在 Controller 中显示这些)。

关于php - Prestashop 产品特性 PHP SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17151576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com