Prestashop: How to get accessories for product

允我心安 提交于 2019-12-12 10:08:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!