Jump to content

Get product carriers on product listing


malo6

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.

Link to comment
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.

Link to comment
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?

Link to comment
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
Link to comment
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.

Link to comment
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.

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