Prestashop:如何获取产品配件 [英] Prestashop: How to get accessories for product

查看:39
本文介绍了Prestashop:如何获取产品配件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得每个产品的配件?在循环中:

how can I get accessories for each product? In a loop:

{foreach $products as $product}

{foreach $products as $product}

//获取配件

{/foreach}

推荐答案

问题是 HomeFeatured 模块不检索产品的配件,因此它们在模板中不可用.

The problem is that the module HomeFeatured doesn't retrieve accessories for products so they aren't available in the template.

你有选择:

  • 编辑模块的PHP代码:简单但不升级证明
  • 将模块复制到 myhomefeatured :也很容易但更少,并且升级证明

我更喜欢第 2 个,更具未来证明性 &如果需要,您可以在之后添加越来越多的逻辑.

I prefer the 2nd, more future proof & you can add more and more logic after if you need.

无论您选择什么,这里都是修改 hookDisplayHome 的代码,以创建一个按产品 ID 索引的 Smarty 变量 $accessories :

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');
}

这篇关于Prestashop:如何获取产品配件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆