我想在Prestashop中获得一个类别的产品总数,我得到了正确的数字但是有这个通知:
"试图在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);
答案 0 :(得分:2)
抛出此通知是因为您的上下文中没有设置任何Controller。
如果你看一下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
。在您的上下文中没有定义Controller,因为您没有在Prestashop中运行脚本。
您可以尝试手动将Controller添加到Context对象:
$context = Context::getContext();
$context->controller = new FrontController();
echo $category->getProducts(1,1,1000,null,null,1,1);