注意 $category->getProducts() :"Trying to get property of non-object"
Notice with $category->getProducts() : "Trying to get property of non-object"
我试图在 Prestashop 中获取一个类别中的产品总数,我得到了正确的数字,但有此通知:
"Trying to get property of non-object in Category.php".
这是我的代码:
<?php
define('PRESTASHOP_S_EXTERNAL_SCRIPT', true);
include(dirname(__FILE__).'\..\prestashop_1.6.1.0\prestashop\config\config.inc.php');
//returns 1
echo Configuration::get('PS_LANG_DEFAULT');
$id_category = 123;
$category = new Category($id_category, (int)Configuration::get('PS_LANG_DEFAULT'),(int)Configuration::get('PS_SHOP_DEFAULT'));
//works
echo $category->getName();
//returns 1
echo is_object($category);
//returns the correct number but with a PHP notice ("Trying to get property of non-object in Category.php on line 671")
echo $category->getProducts(1,1,1000,null,null,1,1);
抛出此通知是因为您没有在上下文中设置任何控制器。
如果您查看第 671 行的 Category.php:
public function getProducts($id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $random = false, $random_number_products = 1, $check_access = true, Context $context = null)
{
// [...]
$front = in_array($context->controller->controller_type, array('front', 'modulefront'));
// [...]
}
问题来自这段代码$context->controller->controller_type
。您的上下文中没有定义控制器,因为您不是 运行 来自 Prestashop 的脚本。
您可以尝试手动添加一个Controller到Context对象:
$context = Context::getContext();
$context->controller = new FrontController();
echo $category->getProducts(1,1,1000,null,null,1,1);
我试图在 Prestashop 中获取一个类别中的产品总数,我得到了正确的数字,但有此通知:
"Trying to get property of non-object in Category.php".
这是我的代码:
<?php
define('PRESTASHOP_S_EXTERNAL_SCRIPT', true);
include(dirname(__FILE__).'\..\prestashop_1.6.1.0\prestashop\config\config.inc.php');
//returns 1
echo Configuration::get('PS_LANG_DEFAULT');
$id_category = 123;
$category = new Category($id_category, (int)Configuration::get('PS_LANG_DEFAULT'),(int)Configuration::get('PS_SHOP_DEFAULT'));
//works
echo $category->getName();
//returns 1
echo is_object($category);
//returns the correct number but with a PHP notice ("Trying to get property of non-object in Category.php on line 671")
echo $category->getProducts(1,1,1000,null,null,1,1);
抛出此通知是因为您没有在上下文中设置任何控制器。
如果您查看第 671 行的 Category.php:
public function getProducts($id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $random = false, $random_number_products = 1, $check_access = true, Context $context = null)
{
// [...]
$front = in_array($context->controller->controller_type, array('front', 'modulefront'));
// [...]
}
问题来自这段代码$context->controller->controller_type
。您的上下文中没有定义控制器,因为您不是 运行 来自 Prestashop 的脚本。
您可以尝试手动添加一个Controller到Context对象:
$context = Context::getContext();
$context->controller = new FrontController();
echo $category->getProducts(1,1,1000,null,null,1,1);