问题
how can I get accessories for each product? In a loop:
{foreach $products as $product}
// get accessories
{/foreach}
回答1:
The problem is that the module HomeFeatured doesn't retrieve accessories for products so they aren't available in the template.
You have the choices :
- edit the PHP code of the module : simple but not upgrade proof
- duplicate the module to myhomefeatured : easy too but less, and upgrade proof
I prefer the 2nd, more future proof & you can add more and more logic after if you need.
Whatever you choose, here the modified code of hookDisplayHome to ahve a Smarty variable $accessories indexed by product's id :
public function hookDisplayHome($params) {
$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
$nb = (int)(Configuration::get('HOME_FEATURED_NBR'));
$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10));
// -- begin -->
$accessories = array();
foreach ($products as $product) {
$p = new Product($product['id_product'], false, (int)Context::getContext()->language->id);
$accessories[$product['id_product']] = $p->getAccessories((int)Context::getContext()->language->id);
}
// <-- end --
$this->smarty->assign(array(
'products' => $products,
'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
'accessories' => $accessories // <-- added --
));
return $this->display(__FILE__, 'homefeatured.tpl');
}
回答2:
Fast example to get Attribute Combinations:
$product = new Product($id_product);
$comb = $product->getAttributeCombinations($this->context->cookie->id_lang);
if (sizeof($comb)){
foreach($comb AS $combination){
//do some stuff here
}
}
I think it will helps to for another
回答3:
I'm not sure how it will look inside .tpl
file but for PHP it is:
$product->getAccessories(intval($cookie->id_lang))
referring to your example it might look like this:
{foreach $products as $product}
{assign var='accessories' value=$product->getAccessories(intval($cookie->id_lang))}
{$accessories|@var_dump}
{foreach from=$accessories item=accessory}
{$accessory|@var_dump}
{/foreach}
{/foreach}
来源:https://stackoverflow.com/questions/14600882/prestashop-how-to-get-accessories-for-product