我注意到在PrestaShop 1.7中你可以标记产品。 是否可以通过编程方式按标签搜索产品?
例如,如果我想找到包含任何[' madcow,crazydog,happyant']标签的产品,我该如何处理?
$this->context->getProducts('tag', ['madcow, crazydog, happyant']) ?
或者
Product::getByAttribute('tag', ['madcow, crazydog, happyant'])
或类似的?
任何非常感谢的帮助
答案 0 :(得分:0)
您可以通过执行以下操作获取活动产品(ID和名称)foreach标记:
$tag = new Tag(null, "tag", $idlang);
$products = $tag->getProducts();
标签构造器也是如此,你的代码可能是:
$context = \Context::getContext();
$idlang = $context->language->id;
$tags = ['madcow', 'crazydog', 'happyant'];
$products = [];
foreach($tags as $t){
$tag = new Tag(null, $t, $idlang);
if($tag != null){
$tag_products = $tag->getProducts();
foreach($tag_products as $tg)
$products[$tg['id_product']] = $tg['name'];
}
}
答案 1 :(得分:0)
您可以使用此代码段:
$array_tags = implode(',',['one','two','three']);
$id_lang = $this->context->language->id;
$products = Db::getInstance()->executeS('
SELECT pl.id_product
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON p.id_product = pl.id_product'.Shop::addSqlRestrictionOnLang('pl').'
'.Shop::addSqlAssociation('product', 'p').'
WHERE pl.id_lang = '.(int) $id_lang.'
AND product_shop.active = 1
AND p.id_product IN (SELECT pt.id_product FROM `'._DB_PREFIX_.'product_tag` pt WHERE pt.name IN ('.pSQL($array_tags).'))
ORDER BY pl.name');