Jump to content

Products in the same Category in Column Right


Recommended Posts

Hi Prestashoppers!

I'm trying to do some modifications to ProductsCategory module, which shows products in the same category in product page.

To achieve this goal I've edited this file:

modules/productscategory/productscategory.php

Briefly, I copied the code of public function hookProductFooter($params) and create the function for Right Column hook.

The function I've created is the following. I've just modified the quantity of products, cache id and added a new varaible ($category) for smarty.

	public function hookRightColumn($params)
	{
		$id_product = (int)$params['product']->id;
		$product = $params['product'];

		$cache_id = 'productscategory_side|'.$id_product.'|'.(isset($params['category']->id_category) ? (int)$params['category']->id_category : (int)$product->id_category_default);

		if (!$this->isCached('productscategory_side.tpl', $this->getCacheId($cache_id)))
		{

			$category = false;
			if (isset($params['category']->id_category))
				$category = $params['category'];
			else
			{
				if (isset($product->id_category_default) && $product->id_category_default > 1)
					$category = new Category((int)$product->id_category_default);
			}

			if (!Validate::isLoadedObject($category) || !$category->active)
				return false;

			// Get infos
			$category_products = $category->getProducts($this->context->language->id, 1, 12); /* 12 products max. */
			$nb_category_products = (int)count($category_products);
			$middle_position = 0;

			// Remove current product from the list
			if (is_array($category_products) && count($category_products))
			{
				foreach ($category_products as $key => $category_product)
				{
					if ($category_product['id_product'] == $id_product)
					{
						unset($category_products[$key]);
						break;
					}
				}

				$taxes = Product::getTaxCalculationMethod();
				if (Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE'))
				{
					foreach ($category_products as $key => $category_product)
					{
						if ($category_product['id_product'] != $id_product)
						{
							if ($taxes == 0 || $taxes == 2)
							{
								$category_products[$key]['displayed_price'] = Product::getPriceStatic(
									(int)$category_product['id_product'],
									true,
									null,
									2
								);
							} elseif ($taxes == 1)
							{
								$category_products[$key]['displayed_price'] = Product::getPriceStatic(
									(int)$category_product['id_product'],
									false,
									null,
									2
								);
							}
						}
					}
				}

				// Get positions
				$middle_position = (int)round($nb_category_products / 2, 0);
				$product_position = $this->getCurrentProduct($category_products, (int)$id_product);

				// Flip middle product with current product
				if ($product_position)
				{
					$tmp = $category_products[$middle_position - 1];
					$category_products[$middle_position - 1] = $category_products[$product_position];
					$category_products[$product_position] = $tmp;
				}

				// If products tab higher than 30, slice it
				if ($nb_category_products > 30)
				{
					$category_products = array_slice($category_products, $middle_position - 15, 30, true);
					$middle_position = 15;
				}
			}

			// Display tpl
			$this->smarty->assign(
				array(
					'categoryProducts' => $category_products,
					'middlePosition' => (int)$middle_position,
					'category' => $category, /* Added by Ruben Felix */
					'ProdDisplayPrice' => Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE')
				)
			);
		}
		
		return $this->display(__FILE__, 'productscategory_side.tpl', $this->getCacheId($cache_id));
	}

As you can see, this function use a different template for display. It uses productscategory_side.tpl which is a file I've created. This file exists both in themes/mytheme/modules/productscategory/ and modules/productscategory/views/templates/hook

Content for this file is the following:

<!-- MODULE Block products in the same category -->
<!-- {if $categoryProducts && $categoryProducts|@count > 0} -->
<div id="products-category_block_right" class="block products_block">
	<h4 class="title_block">
    		<a href="{$category->getLink()|escape:'html'}" title="{l s='Products In The Same Category' mod='productscategory'}">{l s='Products In The Same Category' mod='productscategory'}</a>
	</h4>
	<div class="block_content">
        	{include file="$tpl_dir./product-list-home.tpl" products=$categoryProducts id="pc-block"}
		<script type="text/javascript">
			$(document).ready(function(){
				$("#pc-block").owlCarousel({
				<!-- {if $categoryProducts|@count > 1} -->
					loop:true,
					autoplay: true,
					margin:10,
					nav:true,
				<!-- {else}
					loop:false,
					nav:false,
				{/if} -->
					responsive:{
		                            0:{
		                                items:2,
		                                margin:0
		                            },
		                            480:{
		                                items:3,
		                                margin:0
		                            },
		                            768:{
		                                items:1,
		                                margin:0
		                            },
		                            992:{
		                                items:1,
		                                margin:0
		                            },
		                            1200:{
		                                items:1,
		                                margin:0
		                            }
					}	
				})
			});
		</script>
	</div>
</div>
<!-- {/if} -->
<!-- /MODULE Block products in the same category -->

This file is specific for the theme I use, which has included owl.carousel library.

 

I've register the hook in displayRightColumn, delete the ProductFooter and Header hooks. I've tested module is loaded writing print "hola" at the beginning of the function and it was showed when loaded product page.

I've also deactivated cache and force compilation temporary.

 

Hope you can help me sorting this problem out.

 

Regards.

Ruben Felix

Lima, Perú

  • Like 1
Link to comment
Share on other sites

Ok, I've found the solution.

Just have to use this code for product and id_product

$product = $this->context->controller->getProduct();
$id_product = (int)$product->id;	

And this one for category:

$category = $this->context->controller->getCategory();
if (!isset($category->id_category))
{
if (isset($product->id_category_default) && $product->id_category_default > 1)
	$category = new Category((int)$product->id_category_default);
}
Link to comment
Share on other sites

  • 1 year later...

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