Jump to content

Sort smarty array in shopping-cart-product-line.tpl


pontusnilbrink

Recommended Posts

Hi,

 

I would like to sort an smarty array containing the id_supplier of the products added to the cart.

In the top of the shopping-cart-product-line.tpl i have added:

{$ids = $product.id_supplier}
{$ids}
<tr><td colspan="7">Seller id: {$product.id_supplier}</td></tr>

so in the code it looks like:

{$ids = $product.id_supplier}
{$ids}
<tr><td colspan="7">Seller id: {$product.id_supplier}</td></tr>
<tr id="product_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}{if !empty($product.gift)}_gift{/if}" class="cart_item{if isset($productLast) && $productLast && (!isset($ignoreProductLast) || !$ignoreProductLast)} last_item{/if}{if isset($productFirst) && $productFirst} first_item{/if}{if isset($customizedDatas.$productId.$productAttributeId) AND $quantityDisplayed == 0} alternate_item{/if} address_{$product.id_address_delivery|intval} {if $odd}odd{else}even{/if}">
	<td class="cart_product">
		<a href="{$link->getProductLink($product.id_product, $product.link_rewrite, $product.category, null, null, $product.id_shop, $product.id_product_attribute)|escape:'html':'UTF-8'}"><img src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'small_default')|escape:'html':'UTF-8'}" alt="{$product.name|escape:'html':'UTF-8'}" {if isset($smallSize)}width="{$smallSize.width}" height="{$smallSize.height}" {/if} /></a>
	</td>

Which looks like this on site:

post-946570-0-85954800-1440421332_thumb.png

 

As you can see this prints out the id's of the suppliers connected to each product. 

By sorting the {$ids} I am hoping to be able to split the cart later on. 

 

Does anyone have an idea of how to sort the {$ids}? 

Any help would be greatly appreciated!

 

I am using Prestashop 1.6.1.0 and localhost (default theme)

Link to comment
Share on other sites

Do I understand you correctly that you want to sort the products by supplier?

 

The nicest way to do that is in the PHP/mysql code.

 

However, you can do it in the Smarty template too. In pseudo-code (not checked) you would get something like:

 

{foreach $products AS $product}

   {$psuppliers[$product.id_supplier][] = $product}

{/foreach}

/* now we put the supplier arrays together in a new products array */

{$myproducts = []}

{foreach $psuppliers AS $psupplier}

   {foreach $psupplier AS $product}

     {$products[] = $product}

   {/foreach}

{/foreach}

  • Like 1
Link to comment
Share on other sites

Hi musicmaster and thank you for replying!

 

I added your code snippet before the "product loop" in shopping-cart.tpl as shown below:

<tbody>
				{assign var='odd' value=0}
				{assign var='have_non_virtual_products' value=false}
				{foreach $products AS $product}
				   {$psuppliers[$product.id_supplier][] = $product}
				{/foreach}
				{* now we put the supplier arrays together in a new products array *}
				{$myproducts = []}
				{foreach $psuppliers AS $psupplier}
				   {foreach $psupplier AS $product}
				     {$products[] = $product}
				   {/foreach}
				{/foreach}				
				{foreach $products as $product}
					{if $product.is_virtual == 0}
						{assign var='have_non_virtual_products' value=true}
					{/if}
					{assign var='productId' value=$product.id_product}
					{assign var='productAttributeId' value=$product.id_product_attribute}
					{assign var='quantityDisplayed' value=0}
					{assign var='odd' value=($odd+1)%2}
					{assign var='ignoreProductLast' value=isset($customizedDatas.$productId.$productAttributeId) || count($gift_products)}
					{* Display the product line *}
					{include file="$tpl_dir./shopping-cart-product-line.tpl" productLast=$product@last productFirst=$product@first}	
					{* Then the customized datas ones*}
					{if isset($customizedDatas.$productId.$productAttributeId)}
						{foreach $customizedDatas.$productId.$productAttributeId[$product.id_address_delivery] as $id_customization=>$customization}
							<tr
								id="product_{$product.id_product}_{$product.id_product_attribute}_{$id_customization}_{$product.id_address_delivery|intval}"
								class="product_customization_for_{$product.id_product}_{$product.id_product_attribute}_{$product.id_address_delivery|intval}{if $odd} odd{else} even{/if} customization alternate_item {if $product@last && $customization@last && !count($gift_products)}last_item{/if}">
								<td></td>
								<td colspan="3">

What it does though is that it adds more of one of the 3 products that are actually added to the cart as shown below:

 

blockcart.png

 

cartsummary.png

Do you have any other idea of how to solve this?

Best Regards, Pontus

Link to comment
Share on other sites

I am not sure what to think about this.

 

First, there was an obvious error in my script 

{$myproducts = []} should be {$products = []}

 

More problematic is my doubt about that supplier ID. A product can have more than one supplier, so I don't know what the id_supplier field should contain. I did some experiments, but the id_supplier field always stays zero - even when I have assigned suppliers to a product.

 

The code you initially wrote only adds to the mystery: 

{$ids = $product.id_supplier}
{$ids}
<tr><td colspan="7">Seller id: {$product.id_supplier}</td></tr>

That means that "$product.id_supplier" the first time prints out as "1 2 1" and the second time as a single figure.

 

So I am afraid I can't help you to solve this mystery any further.

Link to comment
Share on other sites

Hi musicmaster,

 

I will only give each product one supplier so that will be no problem, the reason why the $product.id_supplier returner 1 2 1 was because I ran a foreach on it, maybe I changed it before pasting my code into this thread, sorry for that.

 

Anyway, after changing your code snippet ($myproducts[] to $products) it workes flawlessly.

 

Big thank you for taking time and helping me with this!

 

Best Regards

Pontus

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