使用 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');
}
我需要做的是访问当前产品的类别名称,但这很难做到。
我试过各种解决方案都没有成功。
挂钩 DisplayRightColumnProduct()
没有任何参数作为参数传递给它,因此要获取类别名称或任何其他信息,我们必须重新构建用户正在访问的产品的所有数据。
我们还必须了解一些产品的属性:
商品可以有多个分类,方便用户关注
进入产品页面的不同路径。
商品也可以直接访问,本例我们没有
有关应显示哪个类别名称的信息。
所以在 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;
此代码段将显示默认产品类别的名称。
$product = $this->context->controller->getProduct();
$category = new Category((int)$product->id_category_default, (int)$this->context->language->id);
echo $category->name;
我为 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');
}
我需要做的是访问当前产品的类别名称,但这很难做到。
我试过各种解决方案都没有成功。
挂钩 DisplayRightColumnProduct()
没有任何参数作为参数传递给它,因此要获取类别名称或任何其他信息,我们必须重新构建用户正在访问的产品的所有数据。
我们还必须了解一些产品的属性:
商品可以有多个分类,方便用户关注 进入产品页面的不同路径。
商品也可以直接访问,本例我们没有 有关应显示哪个类别名称的信息。
所以在 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;
此代码段将显示默认产品类别的名称。
$product = $this->context->controller->getProduct();
$category = new Category((int)$product->id_category_default, (int)$this->context->language->id);
echo $category->name;