简体   繁体   中英

Get Product Category Name In Module using Prestashop 1.6

I created my own module for prestashop (very basic currently).

I want to add some customization to products (similar to Attribute Wizard Pro ) Ultimate Goal: I would like for my module to display a little form on the product page depending on which category the product is in (slightly varied forms for each category) - and the results of that form would be saved when the product is purchased.

I want to put the form in the RightColumnProduct - I can do that by accessing it in this hook and calling the TPL I created.

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

What I need to do is access the category name for the current product, but that is proving difficult to do.

I've tried various solutions with no success.

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

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

The hook DisplayRightColumnProduct() doesn't have any parameter passed to it as argument, so to get the category name or any other information we have to rebuild again all the data of the product the user is visiting. We also have to be aware of some product's properties:

  1. the product can have more then one category, so the user can follow different path to get on the product page.

  2. the product can be also visited directly, in this case we have no information about which category name should be shown.

So in the DisplayRightColumnProduct function I would perform the following steps:

//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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM