Php 使用Prestashop 1.6在模块中获取产品类别名称
我为prestashop创建了自己的模块(目前非常基本) 我想为产品添加一些定制(类似于属性向导Pro) 最终目标:我想让我的模块在产品页面上显示一个小表单,具体取决于产品所属的类别(每个类别的表单略有不同)——并且该表单的结果将在购买产品时保存 我想把表单放在RightColumnProduct中——我可以通过在这个钩子中访问它并调用我创建的TPL来实现这一点Php 使用Prestashop 1.6在模块中获取产品类别名称,php,module,prestashop,prestashop-1.6,Php,Module,Prestashop,Prestashop 1.6,我为prestashop创建了自己的模块(目前非常基本) 我想为产品添加一些定制(类似于属性向导Pro) 最终目标:我想让我的模块在产品页面上显示一个小表单,具体取决于产品所属的类别(每个类别的表单略有不同)——并且该表单的结果将在购买产品时保存 我想把表单放在RightColumnProduct中——我可以通过在这个钩子中访问它并调用我创建的TPL来实现这一点 public function hookDisplayRightColumnProduct() { /* Place your
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');
}
我需要做的是访问当前产品的类别名称,但这很难做到
我尝试了各种解决方案,但都没有成功。hook
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;
//从$\u GET中检索产品id,并实例化该对象,使其为我们必须创建的任何功能做好准备。
$product=新产品(工具::getValue('id\u product'),false,$this->context->cookie->id\u lang);
//通过模拟ProductController所做的工作,我们将得到产品的类别
$id_category=false;
if(isset($\u SERVER['HTTP\u REFERER'])和&$\u SERVER['HTTP\u REFERER']==Tools::securereferer($\u SERVER['HTTP\u REFERER'])//请确保上一页是其中一页
&&preg_match('~^.*('context->shop)| |!Product::idIsOnCategoryId((int)$Product->id,数组('0'=>array('id_category'=>id_category)))
$id\u category=(int)$product->id\u category\u默认值;
$category=新类别((int)$id\u category,(int)$this->context->cookie->id\u lang);
//现在,我们可以使用category对象来获取名称,只需引用属性:
返回$category->name;
此代码段将显示默认产品类别的名称
$product=$this->context->controller->getProduct();
$category=新类别((int)$product->id\u category\u默认值,(int)$this->context->language->id);
echo$category->name;
这是可行的,但最终的echo应该是:echo$category->name;谢谢!