簡體   English   中英

使用 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM