自定义模块获取折扣商品报错:Trying to get property of non-object
Let's break down what's causing this notice and how to fix it step by step.
Why This Happens
The error at Product.php line 2376 means somewhere in the getPricesDrop() method, the code is trying to access a property on a variable that isn't an object (it's probably null or a scalar value). In your case, this is almost certainly tied to how you're handling the language ID or the state of $this->context in your module's getContent() method.
Step-by-Step Solutions
1. Verify $this->context->language is a Valid Object
First, confirm if the language object in your module's context is actually initialized. Add a quick debug check at the start of your getContent() method:
public function getContent() { // Quick debug to check language context if (!is_object($this->context->language)) { die('Language context is missing!'); } // Your existing code $products = Product::getPricesDrop((int)$this->context->language->id, 0, 10); // ... rest of your code }
If this triggers the die message, it means $this->context->language isn't available in the current context—this is common in module admin pages, where the system uses the employee's language instead of the frontend language.
2. Use the Correct Language ID for the Context
Since getContent() is typically used for module admin settings, switch to using the logged-in employee's language ID instead:
// For admin module pages $id_lang = (int)$this->context->employee->id_lang; $products = Product::getPricesDrop($id_lang, 0, 10);
Alternatively, explicitly fetch the global context to ensure you have access to a valid language object:
$context = Context::getContext(); $id_lang = (int)$context->language->id; $products = Product::getPricesDrop($id_lang, 0, 10);
3. Ensure Your Module Properly Initializes the Context
Double-check that your module's constructor calls the parent Module constructor—this is critical for populating $this->context:
class YourCustomModule extends Module { public function __construct() { parent::__construct(); // Don't skip this! $this->name = 'yourcustommodule'; $this->tab = 'front_office_features'; // ... other module setup settings } public function getContent() { // Your code here } }
Without parent::__construct(), $this->context won't be properly initialized, leading to missing objects like language.
4. Handle Empty Results Gracefully
If the error persists, it might mean getPricesDrop() is returning no products, and the core code is trying to access properties on a non-object result. Add a check in your module to avoid this:
$products = Product::getPricesDrop($id_lang, 0, 10); if (!empty($products)) { // Process and display your discounted products } else { // Show a message like "No discounted products available" }
Final Notes
In most cases, this notice stems from a missing or uninitialized language object in your module's context. Using Context::getContext() explicitly or switching to the employee's language ID for admin pages should resolve the issue quickly.
内容的提问来源于stack exchange,提问作者pawel1993

