按属性列出产品prestashop [英] listing products by Attributes prestashop
问题描述
我是prestashop 1.6开发的新手,我想创建一个模块,该模块允许根据产品的不同属性列出产品示例:我们有两种颜色(黑色和蓝色)和两种尺寸(L,XL)的牛仔裤,该页面显示四种产品(黑色L,黑色XL,蓝色L,蓝色XL),如图所示列出示例
I am new in the prestashop 1.6 development, I want to create a module that allows to list the products according to their different attributes example: we have jeans pants with two colors (black and blue) and two size (L, XL), the page displays four products (black L, black XL, blue L, blue XL), as shown in the picture listing example
您能指导我成功完成本模块吗
can you guide me to succeed this module
推荐答案
有一个付费模块可以完美实现这一目标: https://addons.prestashop.com/en/combinaisons-customization/31071-show-combinations-or-product-attributes-in-product-list.html
There's a paid module that's perfect to achieve this: https://addons.prestashop.com/en/combinaisons-customization/31071-show-combinations-or-product-attributes-in-product-list.html
或者,您也可以覆盖Category类中存在的getProducts()方法,并在Category控制器中对其进行调用.
Alternately, you can also override the getProducts() method present in the Category class an called in the Category controller.
以下是与PrestaShop 1.6.x版本兼容的基本示例:
Here's a basic example compatible with PrestaShop 1.6.x versions:
- 在/override/classes/ 中创建一个空的Category.php文件
- 删除文件/cache/class_index.php
- 在/override/classes/Category.php中添加getProducts()方法
- Create an empty Category.php file in /override/classes/
- Delete the file /cache/class_index.php
- Add the getProducts() method in /override/classes/Category.php
class Category extends CategoryCore
{
public function getProducts($id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $random = false, $random_number_products = 1, $check_access = true, Context $context = null)
{
if (!$context) {
$context = Context::getContext();
}
if ($check_access && !$this->checkAccess($context->customer->id)) {
return false;
}
$front = in_array($context->controller->controller_type, array('front', 'modulefront'));
$id_supplier = (int)Tools::getValue('id_supplier');
/** Return only the number of products */
if ($get_total) {
$sql = 'SELECT COUNT(cp.`id_product`) AS total
FROM `'._DB_PREFIX_.'product` p
'.Shop::addSqlAssociation('product', 'p').'
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product`
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON pa.`id_product` = p.`id_product`
WHERE cp.`id_category` = '.(int)$this->id.
($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '').
($active ? ' AND product_shop.`active` = 1' : '').
($id_supplier ? 'AND p.id_supplier = '.(int)$id_supplier : '');
return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
}
if ($p < 1) {
$p = 1;
}
/** Tools::strtolower is a fix for all modules which are now using lowercase values for 'orderBy' parameter */
$order_by = Validate::isOrderBy($order_by) ? Tools::strtolower($order_by) : 'position';
$order_way = Validate::isOrderWay($order_way) ? Tools::strtoupper($order_way) : 'ASC';
$order_by_prefix = false;
if ($order_by == 'id_product' || $order_by == 'date_add' || $order_by == 'date_upd') {
$order_by_prefix = 'p';
} elseif ($order_by == 'name') {
$order_by_prefix = 'pl';
} elseif ($order_by == 'manufacturer' || $order_by == 'manufacturer_name') {
$order_by_prefix = 'm';
$order_by = 'name';
} elseif ($order_by == 'position') {
$order_by_prefix = 'cp';
}
if ($order_by == 'price') {
$order_by = 'orderprice';
}
$nb_days_new_product = Configuration::get('PS_NB_DAYS_NEW_PRODUCT');
if (!Validate::isUnsignedInt($nb_days_new_product)) {
$nb_days_new_product = 20;
}
$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) AS quantity'.(Combination::isFeatureActive() ? ', IFNULL(product_attribute_shop.id_product_attribute, 0) AS id_product_attribute,
product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity' : '').', 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`, image_shop.`id_image` id_image,
il.`legend` as legend, m.`name` AS manufacturer_name, cl.`name` AS category_default,
DATEDIFF(product_shop.`date_add`, DATE_SUB("'.date('Y-m-d').' 00:00:00",
INTERVAL '.(int)$nb_days_new_product.' DAY)) > 0 AS new, product_shop.price AS orderprice, pa.id_product_attribute
FROM `'._DB_PREFIX_.'category_product` cp
LEFT JOIN `'._DB_PREFIX_.'product` p
ON p.`id_product` = cp.`id_product`
'.Shop::addSqlAssociation('product', 'p').
(Combination::isFeatureActive() ? ' LEFT JOIN `'._DB_PREFIX_.'product_attribute_shop` product_attribute_shop
ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop='.(int)$context->shop->id.')':'').'
'.Product::sqlStock('p', 0).'
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
ON (product_shop.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
ON (p.`id_product` = pl.`id_product`
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').')
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON pa.`id_product` = p.`id_product`
LEFT JOIN `'._DB_PREFIX_.'product_attribute_image` pai ON (pai.`id_product_attribute` = pa.`id_product_attribute`)
LEFT JOIN `'._DB_PREFIX_.'image_shop` image_shop ON (image_shop.`id_image` = pai.`id_image` AND image_shop.cover=1 AND image_shop.id_shop='.(int)$context->shop->id.')
LEFT JOIN `'._DB_PREFIX_.'image_lang` il
ON (image_shop.`id_image` = il.`id_image`
AND il.`id_lang` = '.(int)$id_lang.')
LEFT JOIN `'._DB_PREFIX_.'manufacturer` m
ON m.`id_manufacturer` = p.`id_manufacturer`
WHERE product_shop.`id_shop` = '.(int)$context->shop->id.'
AND cp.`id_category` = '.(int)$this->id
.($active ? ' AND product_shop.`active` = 1' : '')
.($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '')
.($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '');
if ($random === true) {
$sql .= ' ORDER BY RAND() LIMIT '.(int)$random_number_products;
} else {
$sql .= ' ORDER BY '.(!empty($order_by_prefix) ? $order_by_prefix.'.' : '').'`'.bqSQL($order_by).'` '.pSQL($order_way).'
LIMIT '.(((int)$p - 1) * (int)$n).','.(int)$n;
}
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql, true, false);
if (!$result) {
return array();
}
if ($order_by == 'orderprice') {
Tools::orderbyPrice($result, $order_way);
}
/* Extended name */
foreach ($result as &$p)
{
$attributes = Db::getInstance()->ExecuteS('
SELECT al.name
FROM '._DB_PREFIX_.'attribute_lang al
WHERE al.id_attribute IN (SELECT pac.id_attribute FROM '._DB_PREFIX_.'product_attribute_combination pac WHERE pac.id_product_attribute = '.(int)$p['id_product_attribute'].')
AND al.id_lang = '.(int)$id_lang);
$extended_name = $p['name'].' (';
foreach ($attributes as $a)
$extended_name .= $a['name'].', ';
$p['name'] = rtrim($extended_name, ', ').')';
}
/** Modify SQL result */
return Product::getProductsProperties($id_lang, $result);
}
}
这将显示每个产品可用的所有组合,并选择右封面图片和详细名称(例如我的衣服(红色,M)").这个示例并不是完美的,分页和库存也必须进行调整,但是您明白了.
This will display all the combinations available for each product and also select the right cover picture as well as a detailed name (e.g. "My dress (Red, M)"). This example isn't perfect though, pagination and stock also have to be tweaked but you get the point.
这篇关于按属性列出产品prestashop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!