Jump to content

Custom product variable does not exists in product listing pages


Recommended Posts

I have create a new variable ($show_discount) overriding product class.

Product.php class override

<?php 
Class Product extends ProductCore
{
    public $show_discount = 1;
    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
    {
        self::$definition['fields']['show_discount'] = array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'size' => 1);
        parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
    }
}
?>

and i am changing the value from the controller (override).

The variable exists in product page (var dump), but does not exists in product listing (category, new product, search  etc.)

Can anyone help?

Link to comment
Share on other sites

33 minutes ago, Migration Pro said:

Hi,

You need to access the variable on tpl for display it in the product page.
you can modify tpl files in root\themes\your_theme\templates\catalog directory for modifying display products

In product page the variable $show_discount exists and working properly.  I have edit the tpl and i have access to the variable from the product page.

But not from product listing pages (category, search, new products etc.). When i var dump $product in listing pages the variable $show_discount does not exists.

Link to comment
Share on other sites

There is nothing to do with the theme. 

Has as say when I var dump $product in listing products the $product.show_discount does not exists. In product page the $product.show_discount exists and working as normal. 

I think my problem is in some controller. Maybe is stripping unnecessary variables for the listing products. 

Link to comment
Share on other sites

  • 5 months later...
  • 5 months later...
  • 4 weeks later...
  • 1 year later...

I know this is an old thread.  But in case if anyone's looking for answer for the 'smarty not carrying the assigned variables' problem, here are my fixes.

Override

/controllers/front/listing/CategoryController.php

Then you can attach your variables in the 'initContent()' function.

<?php
class CategoryController extends CategoryControllerCore
{
    public function initContent()
    {
        parent::initContent();

        if ($this->category->checkAccess($this->context->customer->id)) {
            $this->doProductSearch(
                'catalog/listing/category',
                [
                    'entity' => 'category',
                    'id' => $this->category->id,
                ]
            );
        }
        $this->context->smarty->assign([
            'custom_variable' => 'some custom values...',
        ]);
    }
}

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...