Jump to content

Display carrier in product-list


hhcj

Recommended Posts

Hello, 
 
In my page I have some products that can only be sent through a carrier. 
 
Add this code to the product controller to display the carrier product page 
$this->context->smarty->assign(array(
  'carriers' => $this->product->getCarriers(),

But I also show the carrier in the product list, but there is override for the product-list or do not know how. 

 
Can you help me?
 
thanks
Edited by hhcj (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 7 years later...

You can't do it without a module.
You need to write a module and use the displayProductListReviews hook, or create your own hook and build it into ./themes/classic/templates/catalog/_partials/miniatures/product.tpl

And in order not to show it in the product detail, you will need to treat the pages as an index and category in the hook. This is written either to the TPL template or to the module.
E.g. custom hook and Home and Category pages.

This is just a sample, it is not functional code !!!

TPL file product.tpl:

{if $page.name == 'index' || $page.name == 'category'}
	{block name='product_carrier'}
          {hook h='displayProductListCarrier' id_product=$product.id_product}
    {/block}
{/if}

 

PHP module:

public function install() 
    {
        $this->installConfiguration();
        
        if (Shop::isFeatureActive())
        {
            Shop::setContext(Shop::CONTEXT_ALL);
        }
        
        if (!parent::install())
        {
            return false;
        }
	
		
		$hook = new Hook();
        $hook->name = 'displayProductListCarrier';
        $hook->title = $this->l('Hook show carriers');
        $hook->description = $this->l('This is a hook shoe carriers!');
        $hook->position = 1;
        $hook->add();
        $this->registerHook('displayProductListCarrier');
        
        return true;
    }

public function hookDisplayProductListCarrier($params) 
    {
	$template = '';	
	$getProductCarriers = Db::getInstance()->executeS('
            SELECT c.*
            FROM `' . _DB_PREFIX_ . 'product_carrier` pc
            INNER JOIN `' . _DB_PREFIX_ . 'carrier` c
                ON (c.`id_reference` = pc.`id_carrier_reference` AND c.`deleted` = 0)
            WHERE pc.`id_product` = ' . (int) $params['id_product'] . '
                AND pc.`id_shop` = ' . (int) $this->Context::getContext()->shop->id);

		if ($getProductCarriers){
			$template .= '<div style="display:block">';
			foreach ($getProductCarriers as $carrier){
				$template .= '<span>'.$carrier['name'].'</span><br>';	
			}
            $template .= '</div>';

			return $template;
		} else {
			return;
		}

    }

 

Edited by presta.group (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 1 month later...
On 8/29/2021 at 7:22 PM, presta.group said:

You can't do it without a module.
You need to write a module and use the displayProductListReviews hook, or create your own hook and build it into ./themes/classic/templates/catalog/_partials/miniatures/product.tpl

And in order not to show it in the product detail, you will need to treat the pages as an index and category in the hook. This is written either to the TPL template or to the module.
E.g. custom hook and Home and Category pages.

This is just a sample, it is not functional code !!!

TPL file product.tpl:

{if $page.name == 'index' || $page.name == 'category'}
	{block name='product_carrier'}
          {hook h='displayProductListCarrier' id_product=$product.id_product}
    {/block}
{/if}

 

PHP module:

public function install() 
    {
        $this->installConfiguration();
        
        if (Shop::isFeatureActive())
        {
            Shop::setContext(Shop::CONTEXT_ALL);
        }
        
        if (!parent::install())
        {
            return false;
        }
	
		
		$hook = new Hook();
        $hook->name = 'displayProductListCarrier';
        $hook->title = $this->l('Hook show carriers');
        $hook->description = $this->l('This is a hook shoe carriers!');
        $hook->position = 1;
        $hook->add();
        $this->registerHook('displayProductListCarrier');
        
        return true;
    }

public function hookDisplayProductListCarrier($params) 
    {
	$template = '';	
	$getProductCarriers = Db::getInstance()->executeS('
            SELECT c.*
            FROM `' . _DB_PREFIX_ . 'product_carrier` pc
            INNER JOIN `' . _DB_PREFIX_ . 'carrier` c
                ON (c.`id_reference` = pc.`id_carrier_reference` AND c.`deleted` = 0)
            WHERE pc.`id_product` = ' . (int) $params['id_product'] . '
                AND pc.`id_shop` = ' . (int) $this->Context::getContext()->shop->id);

		if ($getProductCarriers){
			$template .= '<div style="display:block">';
			foreach ($getProductCarriers as $carrier){
				$template .= '<span>'.$carrier['name'].'</span><br>';	
			}
            $template .= '</div>';

			return $template;
		} else {
			return;
		}

    }

 

This solution is valid for product.tpl , but not for product-list.tpl. Am i wrong ?

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...