我试图获取 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);
最佳答案
抛出此通知是因为您的上下文中没有设置任何 Controller 。
如果您查看 Category.php 第 671 行:
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 中运行脚本。
您可以尝试手动向 Context 对象添加 Controller:
$context = Context::getContext();
$context->controller = new FrontController();
echo $category->getProducts(1,1,1000,null,null,1,1);
关于php - 注意 $category->getProducts() : "Trying to get property of non-object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35462362/