Jump to content

Get product carriers on product listing


malo6
 Share

Recommended Posts

I want to display text next to the product photo depending on the carroers assigned to the product.
I was partially able to do this on the product page. I modified controllers/front/ProductController.php by changing:
 

            $this->context->smarty->assign(array(
                'pictures' => $pictures,
                'textFields' => $text_fields, ));

To:
 

            $this->context->smarty->assign(array(
                'pictures' => $pictures,
                'carriers' => $this->product->getCarriers(),
                'textFields' => $text_fields, ));

And then in themes/themeX/templates/catalog/product.tpl I can use following code:
 

      {if ($carriers[0]['id_carrier']==30)}
          Custom message
        {/if}

The problem is that the same code doesn't work in product-list.tpl
I will be grateful for your help.

Share this post


Link to post
Share on other sites

10 minutes ago, endriu107 said:

Add in product-list.tpl this {debug} popup with available variables should appear.

I have checked and there is no carrier information there.

Share this post


Link to post
Share on other sites

In the product list, how do you send that variable? Because in the example given by you, if you send only in the product controller, it will be available only in the product page. You should use a hook, available in the template.

Share this post


Link to post
Share on other sites

7 minutes ago, Ress said:

In the product list, how do you send that variable? Because in the example given by you, if you send only in the product controller, it will be available only in the product page. You should use a hook, available in the template.

I send only in product controller. Any tips how to do it via hook?

Share this post


Link to post
Share on other sites

You should do a function like this, in a module:

public function hookDisplayProductPriceBlock($params)
{
	// this check is necessary because the hook is executed several times, with different parameters (unit_price, weight)
	if ($params['type'] != 'before_price') {
		return false;
	}
	
	$product = $params['product'];
	$this->context->smarty->assign(
		array(        
			'custom_product_carriers' => $product->getCarriers(),
		)
   	);

	// you will need to create this tpl and put the code for the front end in it (you will be able to use the $custom_product_carriers variable).
	return $this->display(__FILE__, 'views/templates/hook/product_carriers.tpl');
}

And of course, don't forget to register the hook.

  • Like 1

Share this post


Link to post
Share on other sites

@Ress I'm getting an error:
 

Fatal error: Uncaught Error: Call to a member function getCarriers() on array in 

Line:
 

'custom_product_carriers' => $product->getCarriers(),

 

Share this post


Link to post
Share on other sites

public function hookDisplayProductPriceBlock($params)
{
	// this check is necessary because the hook is executed several times, with different parameters (unit_price, weight)
	if ($params['type'] != 'before_price') {
		return false;
	}
	
	$product = new Product($params['product']->id);
	$this->context->smarty->assign(
		array(        
			'custom_product_carriers' => $product->getCarriers(),
		)
   	);

	// you will need to create this tpl and put the code for the front end in it (you will be able to use the $custom_product_carriers variable).
	return $this->display(__FILE__, 'views/templates/hook/product_carriers.tpl');
}

I updated.

Share this post


Link to post
Share on other sites

The error is gone, but custom_product_carriers variable is empty.
product_carriers.tpl:
 

{$custom_product_carriers|var_dump}	

Result:
 

array(0) { } 

I have checked the products and they have shipping methods assigned to them.

Share this post


Link to post
Share on other sites

Finally got it!
Just a little change.

$product = new Product($params['product']['id_product']);

Thank you very much for your help! :)

  • Like 1

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More