Fadlan Posted August 28, 2023 Share Posted August 28, 2023 I'm out of options, With some help I made the code that adds my variable inside ProductController override: /*Dodatkowa cena SMEG*/ public function initContent() { parent::initContent(); if ($this->product) { // Load your custom variable from the database based on $this->product->id $smegOriginalPrice = $this->loadSmegOriginalPriceFromDb($this->product->id); // Assign the custom variable to Smarty $this->context->smarty->assign(array( 'smegOriginalPrice' => $smegOriginalPrice, )); } } private function loadSmegOriginalPriceFromDb($productId) { $db = \Db::getInstance(); $smegPrice = $db->getRow('SELECT original_price FROM `'._DB_PREFIX_.'product_smeg_prices` WHERE id_product = '.(int)$productId); if($smegPrice){ $smegPrice = floatval($smegPrice['original_price']); $formatted = Context::getContext()->getCurrentLocale()->formatPrice($smegPrice, 'PLN'); return $formatted; } else { return NULL; } } This works properly for product page tpl and {$smegOriginalPrice} returns correct value. I tried to use same tactics for CategoryController override but it failed, and returns only empty array for {$product.smegOriginalPrice} public function initContent() { parent::initContent(); // Assuming you have access to the products list, e.g., $this->cat_products $products = $this->category->getProducts($this->context->language->id, 1, 1000); foreach ($products as &$product) { $smegOriginalPrice = $this->loadSmegOriginalPriceFromDb($product['id_product']); $product['smegOriginalPrice'] = $smegOriginalPrice; } // Assign the custom variable to Smarty $this->context->smarty->assign(array( 'products' => $products, )); } private function loadSmegOriginalPriceFromDb($productId) { $db = \Db::getInstance(); $smegPrice = $db->getRow('SELECT original_price FROM `'._DB_PREFIX_.'product_smeg_prices` WHERE id_product = '.(int)$productId); if($smegPrice){ $smegPrice = floatval($smegPrice['original_price']); $formatted = Context::getContext()->getCurrentLocale()->formatPrice($smegPrice, 'PLN'); return $formatted; } else { return NULL; } } Is there a unified way to pass variable to product object? I have no idea how to progress, please help. Link to comment Share on other sites More sharing options...
byhero41 Posted September 4, 2023 Share Posted September 4, 2023 foreach ($products as &$product) { $smegOriginalPrice = $this->loadSmegOriginalPriceFromDb($product['id_product']); array_push($product['smegOriginalPrice'],$smegOriginalPrice); } Maybe this code can work Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now