使用Prestashop 1.6在模块中获取产品类别名称

时间:2015-12-17 03:41:31

标签: php module prestashop prestashop-1.6

我为prestashop创建了自己的模块(目前非常基本)。

我想为产品添加一些自定义功能(类似于属性向导专业版) 终极目标:我希望我的模块在产品页面上显示一个小表格,具体取决于产品所在的类别(每个类别的表格略有不同) - 并且在购买产品时会保存该表格的结果。

我想把表单放在RightColumnProduct中 - 我可以通过在这个钩子中访问它并调用我创建的TPL来做到这一点。

public function hookDisplayRightColumnProduct()
{
    /* Place your code here. */
    /*Get the Current Category name to see which TPL to show*/


    return $this->display(__FILE__,'views/hooks/mytpl.tpl');
}

我需要做的是访问当前产品的类别名称,但这很难做到。

我尝试了各种解决方案但没有成功。

2 个答案:

答案 0 :(得分:5)

此代码段会显示默认产品类别的名称。

$product = $this->context->controller->getProduct(); $category = new Category((int)$product->id_category_default, (int)$this->context->language->id); echo $category->name;

答案 1 :(得分:1)

钩子DisplayRightColumnProduct()没有任何参数作为参数传递给它,因此要获取类别名称或任何其他信息,我们必须再次重建用户正在访问的产品的所有数据。 我们还必须了解某些产品的属性:

  1. 该产品可以有多个类别,因此用户可以关注 获取产品页面的不同途径。

  2. 产品也可以直接访问,在这种情况下我们没有     有关应显示哪个类别名称的信息。

  3. 因此,在DisplayRightColumnProduct函数中,我将执行以下步骤:

    //retrieve the product id from the $_GET, and instanciate the object to have it ready for any functionality we have to create.
        $product = new Product(Tools::getValue('id_product'), false, $this->context->cookie->id_lang);
    
    //by simulating what the ProductController does we are going to get the category of the product
                        $id_category = false;
    
                        if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == Tools::secureReferrer($_SERVER['HTTP_REFERER']) // Assure us the previous page was one of the shop
                            && preg_match('~^.*(?<!\/content)\/([0-9]+)\-(.*[^\.])|(.*)id_(category|product)=([0-9]+)(.*)$~', $_SERVER['HTTP_REFERER'], $regs))
                        {
                            // If the previous page was a category and is a parent category of the product use this category as parent category
                            $id_object = false;
                            if (isset($regs[1]) && is_numeric($regs[1]))
                                $id_object = (int)$regs[2];
                            elseif (isset($regs[5]) && is_numeric($regs[5]))
                                $id_object = (int)$regs[6];
                            if ($id_object)
                            {
                                $referers = array($_SERVER['HTTP_REFERER'],urldecode($_SERVER['HTTP_REFERER']));
                                if (in_array($this->context->link->getCategoryLink($id_object), $referers)) 
                                    $id_category = (int)$id_object;
                                elseif (isset($this->context->cookie->last_visited_category) && (int)$this->context->cookie->last_visited_category && in_array($this->context->link->getProductLink($id_object), $referers))
                                    $id_category = (int)$this->context->cookie->last_visited_category;
                            }
    
                        }
    //else if we have accessed the product page directly, we have just one way to get the category and it's to retrieve the default from the product object.
                        if (!$id_category || !Category::inShopStatic($id_category, $this->context->shop) || !Product::idIsOnCategoryId((int)$product->id, array('0' => array('id_category' => $id_category))))
                            $id_category = (int)$product->id_category_default;
                        $category = new Category((int)$id_category, (int)$this->context->cookie->id_lang);
    
    //now we have the category object at our disposal so to get the name, we can simply refer to the property:
    return $category->name;
    
相关问题