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


Get Product Category Name In Module using Prestashop 1.6

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

我想为产品添加一些自定义功能(类似于Attribute Wizard Pro)最终目标:我希望我的模块根据产品所属的类别在产品页面上显示一个小表格(每个类别的表格略有不同),并且在购买产品时会保存该表格的结果。

我想把表单放在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');
}

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

我尝试过各种解决方案,但都没有成功。

这个片段将显示默认产品类别的名称。

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

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

  1. 产品可以有多个类别,因此用户可以关注进入产品页面的不同路径。

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

因此,在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;