2

I have no experience in prestashop. I'm trying to get all products which matches to some value of its attribute.

[edit]

Now I get all products by using Product::getProducts. After that I filter result array by array_filter. I'm wondering about method to retrieve that filtered products directly, where filtering is doing in database. Eg. passing a function or string filter like Product::getProductsBy( 'product_id > 10')

4

2 回答 2

7

简短的回答是,没有任何内置的东西可以做到这一点,但是,如果我理解正确,那么你想要一个具有特定属性值的产品列表,以合适的格式输出,使用产品列表主题模板。

我假设您从组内属性值的下拉列表开始,因此我们可以完全忽略属性分组。例如,您可能有一个属性组“Size”( id_attribute_group = 1) 具有可选值“M”和“L”,后者对应于表中的条目XX_attribute

在 XX_attribute 表中,您将拥有

id_attribute | id_attribute_group | color
1            | 1                  | #000000
2            | 1                  | #000000

在 XX_attribute_lang 表中,您将拥有

id_attribute | id_lang            | name
1            | 1                  | M
2            | 1                  | L

假设您不关心类别等 - 要获得尺寸为“L”的所有产品,您可以使用简单的查询,例如:

SELECT pa.`id_product` FROM `XX_product_attribute` pa
LEFT JOIN `XX_product_attribute_combination` pac ON pa.`id_product_attribute` = pac.`id_product_attribute`
WHERE pac.`id_attribute` = '2'

然后,您必须基本复制 Prestashop 内部使用的功能,以检索上面获得的 product_ids 数组的产品详细信息,以传递给产品列表模板。在此阶段仅获取产品 ID 可能是最好的方法,因为无论如何您都需要对产品数据进行语言、货币、税收等后处理。

要从上面构建的 id 数组或类似的过滤查询(例如存储在名为 的数组中$product_ids)中获取详细的产品数据,您必须执行以下操作:

global $cookie;
$id_lang = $cookie->id_lang;
$product_list = array();

if (count($product_ids))
{
  $sql = '
  SELECT p.*, pa.`id_product_attribute`, pl.`description`, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, i.`id_image`, il.`legend`, m.`name` AS manufacturer_name, tl.`name` AS tax_name, t.`rate`, cl.`name` AS category_default, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new,
    (p.`price` * IF(t.`rate`,((100 + (t.`rate`))/100),1)) AS orderprice
  FROM `'._DB_PREFIX_.'category_product` cp
  LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = cp.`id_product`
  LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (p.`id_product` = pa.`id_product` AND default_on = 1)
  LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)($id_lang).')
  LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).')
  LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
  LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).')
  LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group`
                                             AND tr.`id_country` = '.(int)Country::getDefaultCountryId().'
                                               AND tr.`id_state` = 0)
    LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
  LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)($id_lang).')
  LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
  WHERE p.`active` = 1' : '').'
        '.($id_supplier ? 'AND p.id_supplier = '.(int)($id_supplier) : '').'
  AND p.`id_product` IN(' . implode(',', $product_ids) . ')';

  $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql);


  /* Modify SQL result */
  $product_list = Product::getProductsProperties($id_lang, $result);
}

显然,我使用 IN() 将结果过滤到我们的产品集,这对于大量产品来说会变得很棘手,但它给出了总体思路!另一个问题是,上面的查询几乎是从 Prestashop 其他地方的类似功能中提取出来的(如果我在任何地方错误地编辑了它,请道歉!)因此可能会在升级时中断......

于 2012-02-07T10:48:18.833 回答
0

正如保罗上面提到的,你的问题非常模糊,如果没有额外的信息可能很难正确回答。但是,我将尝试提供几个潜在的解决方案:

如果您想购买带有颜色和尺寸选项的特定产品,例如 iPod,您可以使用目录中的属性和组选项卡和组合生成器来完成此操作。

如果您希望根据价格、功能、颜色等参数缩小产品列表(例如,想想您在 Amazon.com 上的购物方式),您可以使用内置的分层配置导航模块。

如果这些都不能解决您的问题,请发布其他信息以帮助我们更好地了解您的问题。

来源:我是 PrestaShop 的社区经理

于 2012-02-06T16:20:21.990 回答