Jump to content

minimal quantity in shopping cart product line


ruisonika

Recommended Posts

Hello developers,
I need your help to achive this (i'm with prestashop 1.6):
- in shopping-cart-product-line (first step of checkout) i have quantities with + and - ;
- i could change quantities with + or - signs but only 1 quantity for click...imagine if the product has minimal quantity, so the result should be increase or decrease the minimal quantity of the product, or 1 if there aren't minimal quantity. 
This is the piece of the code that works in it.

<td class="cart_quantity text-center" data-title="{l s='Quantity'}">
		{if (isset($cannotModify) && $cannotModify == 1)}
			<span>
				{if $quantityDisplayed == 0 AND isset($customizedDatas.$productId.$productAttributeId)}
					{$product.customizationQuantityTotal}
				{else}
					{$product.cart_quantity-$quantityDisplayed}
				{/if}
			</span>
		{else}
			{if isset($customizedDatas.$productId.$productAttributeId) AND $quantityDisplayed == 0}
				<span id="cart_quantity_custom_{$product.id_product}_{$product.id_product_attribute}_{$product.id_address_delivery|intval}" >{$product.customizationQuantityTotal}</span>
			{/if}
			{if !isset($customizedDatas.$productId.$productAttributeId) OR $quantityDisplayed > 0}

				<input type="hidden" value="{if $quantityDisplayed == 0 AND isset($customizedDatas.$productId.$productAttributeId)}{$customizedDatas.$productId.$productAttributeId|@count}{else}{$product.cart_quantity-$quantityDisplayed}{/if}" name="quantity_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}_hidden" />
				<input size="2" type="number" autocomplete="off" readonly style="background:white;border: 1px solid #e6e7e8;" class="cart_quantity_input form-control grey text inputqtyfield" min="{if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}" value="{if $quantityDisplayed == 0 AND isset($customizedDatas.$productId.$productAttributeId)}{$customizedDatas.$productId.$productAttributeId|@count}{else}{$product.cart_quantity-$quantityDisplayed}{/if}"  name="quantity_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" />
				
				<div class="cart_quantity_button clearfix">
				{if $product.minimal_quantity < ($product.cart_quantity-$quantityDisplayed) OR $product.minimal_quantity <= 1}
					<a rel="nofollow" class="cart_quantity_down btn btn-default button-minus" id="cart_quantity_down_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" href="{$link->getPageLink('cart', true, NULL, "add=1&amp;id_product={$product.id_product|intval}&amp;ipa={$product.id_product_attribute|intval}&amp;id_address_delivery={$product.id_address_delivery|intval}&amp;op=down&amp;token={$token_cart}")|escape:'html':'UTF-8'}" title="{l s='Subtract'}">
				<span><i class="icon-minus" ></i></span>
				</a>
				{else}
					<a class="cart_quantity_down btn btn-default button-minus disabled" href="#" id="cart_quantity_down_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" title="{l s='You must purchase a minimum of %d of this product.' sprintf=$product.minimal_quantity}">
					<span><i class="icon-minus" ></i></span>
				</a>
				{/if}
                	<a rel="nofollow" class="cart_quantity_up btn btn-default button-plus" id="cart_quantity_up_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" href="{$link->getPageLink('cart', true, NULL, "add=1&amp;id_product={$product.id_product|intval}&amp;ipa={$product.id_product_attribute|intval}&amp;id_address_delivery={$product.id_address_delivery|intval}&amp;token={$token_cart}")|escape:'html':'UTF-8'}" title="{l s='Add'}"><span><i class="icon-plus" style="padding:.7em;"></i></span></a>
				</div>
			{/if}
		{/if}
	</td>

Thanks for all the help/ideias or brainstorming :)
 

Link to comment
Share on other sites

i don't know, if i understand you correctly

but <input  type="number" ...> has attribute step , did you try?

 

<input size="2" type="number" autocomplete="off" readonly style="background:white;border: 1px solid #e6e7e8;" class="cart_quantity_input form-control grey text inputqtyfield" 
step="{if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}"
min="{if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}" 
value="{if $quantityDisplayed == 0 AND isset($customizedDatas.$productId.$productAttributeId)}{$customizedDatas.$productId.$productAttributeId|@count}{else}{$product.cart_quantity-$quantityDisplayed}{/if}" name="quantity_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" />
				
				

 

Link to comment
Share on other sites

2 hours ago, EvaF said:

i don't know, if i understand you correctly

but <input  type="number" ...> has attribute step , did you try?

 


<input size="2" type="number" autocomplete="off" readonly style="background:white;border: 1px solid #e6e7e8;" class="cart_quantity_input form-control grey text inputqtyfield" 
step="{if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}"
min="{if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}" 
value="{if $quantityDisplayed == 0 AND isset($customizedDatas.$productId.$productAttributeId)}{$customizedDatas.$productId.$productAttributeId|@count}{else}{$product.cart_quantity-$quantityDisplayed}{/if}" name="quantity_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" />
				
				

 

Thanks @EvaF for your reply, but i've made input readonly, for user not to be able to write inside...but your solution is right if i remove the readonly attribute.
What i would like is with the plus sign and minus sign to add or subtract {if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if} but i cannot achieve this yet.
I think is this line for plus sign
 

<a rel="nofollow" class="cart_quantity_up btn btn-default button-plus" id="cart_quantity_up_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" href="{$link->getPageLink('cart', true, NULL, "add=1&amp;id_product={$product.id_product|intval}&amp;ipa={$product.id_product_attribute|intval}&amp;id_address_delivery={$product.id_address_delivery|intval}&amp;token={$token_cart}")|escape:'html':'UTF-8'}" title="{l s='Add'}"><span><i class="icon-plus" style="padding:.7em;"></i></span></a>

and this line for minus
 

<a rel="nofollow" class="cart_quantity_down btn btn-default button-minus" id="cart_quantity_down_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" href="{$link->getPageLink('cart', true, NULL, "add=1&amp;id_product={$product.id_product|intval}&amp;ipa={$product.id_product_attribute|intval}&amp;id_address_delivery={$product.id_address_delivery|intval}&amp;op=down&amp;token={$token_cart}")|escape:'html':'UTF-8'}" title="{l s='Subtract'}">

Thanks again for all your help @EvaF
 

Captura de ecrã 2020-12-05, às 19.56.35.png

Link to comment
Share on other sites

aha srry i didn't notice it "readonly"

and what do you want to achieve?? to reload the page by each pressing of "+" (resp."-") ?

if so then "+"buton could  look like  ( analogical the "-" button)
 

<a rel="nofollow" class="cart_quantity_up btn btn-default button-plus" 
id="cart_quantity_up_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" 
href="{$link->getPageLink('cart', true, NULL, "add=1&qty={if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}&id_product={$product.id_product|intval}&ipa={$product.id_product_attribute|intval}&id_address_delivery={$product.id_address_delivery|intval}&token={$token_cart}")|escape:'html':'UTF-8'}" 
title="{l s='Add'}">
<span><i class="icon-plus" style="padding:.7em;"></i></span></a>

 




 

Edited by EvaF (see edit history)
Link to comment
Share on other sites

9 minutes ago, EvaF said:

aha srry i didn't notice it "readonly"

and what do you want to achieve?? to reload the page by each pressing of "+" (resp."-") ?

if so then "+"buton could  look like  ( analogical the "-" button)
 


<a rel="nofollow" class="cart_quantity_up btn btn-default button-plus" 
id="cart_quantity_up_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" 
href="{$link->getPageLink('cart', true, NULL, "add={if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}&id_product={$product.id_product|intval}&ipa={$product.id_product_attribute|intval}&id_address_delivery={$product.id_address_delivery|intval}&token={$token_cart}")|escape:'html':'UTF-8'}" 
title="{l s='Add'}">
<span><i class="icon-plus" style="padding:.7em;"></i></span></a>

 

and in php code to handle the value of parameter "add"



 

@EvaF i had allready tried your sollution but it doesn't work...maybe for what you have said and i quote:
and in php code to handle the value of parameter "add"
I don't know what you mean with this...i have to change other file? can you tell me what file, and what's the code?
Thanks

Link to comment
Share on other sites

yes, you are right -  the "href" confused me ..

you have two options(imho):

1) to remove class "cart_quantity_up" resp "cart_quantity_down" ( and then the cart page will refreshing)

2) prepare Javascript yourself to  set qty and let Ajax call to do the rest

something like:

change  tag <a  for  "+"   this way: ( and for "-" analogical) and add the script outside {foreach...

<a rel="nofollow" class="cart_minquantity_up btn btn-default button-plus" 
min_qty="{if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}"   
id="cart_quantity_up_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" 
href="{$link->getPageLink('cart', true, NULL, "add=1&qty={if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}&id_product={$product.id_product|intval}&ipa={$product.id_product_attribute|intval}&id_address_delivery={$product.id_address_delivery|intval}&token={$token_cart}")|escape:'html':'UTF-8'}" 
title="{l s='Add'}">
<span><i class="icon-plus" style="padding:.7em;"></i></span></a>
<script>
  $(document).ready(function(){
	$('.cart_minquantity_up').off('click').on('click', function(e){
		e.preventDefault();
		upQuantity($(this).attr('id').replace('cart_quantity_up_', ''),$(this).attr('min_qty'));
		$('#' + $(this).attr('id').replace('_up_', '_down_')).removeClass('disabled');
	});
	$('.cart_minquantity_down').off('click').on('click', function(e){
		e.preventDefault();
		downQuantity($(this).attr('id').replace('cart_quantity_down_', ''),$(this).attr('min_qty'));
	});
  });
</script>    

(i didn't test it, so take it with reserve)

note:
href is useless (due preventDefault)
 

<a rel="nofollow" class="cart_minquantity_up btn btn-default button-plus" 
min_qty="{if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}"   
id="cart_quantity_up_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" 
href="#" title="{l s='Add'}">

 

 



 

Edited by EvaF (see edit history)
Link to comment
Share on other sites

50 minutes ago, EvaF said:

yes, you are right -  the "href" confused me ..

you have two options(imho):

1) to remove class "cart_quantity_up" resp "cart_quantity_down" ( and then the cart page will refreshing)

2) prepare Javascript yourself to  set qty and let Ajax call to do the rest

something like:

change  tag <a  for  "+"   this way: ( and for "-" analogical) and add the script outside {foreach...


<a rel="nofollow" class="cart_minquantity_up btn btn-default button-plus" 
min_qty="{if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}"   
id="cart_quantity_up_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" 
href="{$link->getPageLink('cart', true, NULL, "add=1&qty={if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}&id_product={$product.id_product|intval}&ipa={$product.id_product_attribute|intval}&id_address_delivery={$product.id_address_delivery|intval}&token={$token_cart}")|escape:'html':'UTF-8'}" 
title="{l s='Add'}">
<span><i class="icon-plus" style="padding:.7em;"></i></span></a>

<script>
  $(document).ready(function(){
	$('.cart_minquantity_up').off('click').on('click', function(e){
		e.preventDefault();
		upQuantity($(this).attr('id').replace('cart_quantity_up_', ''),$(this).attr('min_qty'));
		$('#' + $(this).attr('id').replace('_up_', '_down_')).removeClass('disabled');
	});
	$('.cart_minquantity_down').off('click').on('click', function(e){
		e.preventDefault();
		downQuantity($(this).attr('id').replace('cart_quantity_down_', ''),$(this).attr('min_qty'));
	});
  });
</script>    

(i didn't test it, so take it with reserve)

note:
href is useless (due preventDefault)
 


<a rel="nofollow" class="cart_minquantity_up btn btn-default button-plus" 
min_qty="{if $product.minimal_quantity > 0}{$product.minimal_quantity}{else}1{/if}"   
id="cart_quantity_up_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" 
href="#" title="{l s='Add'}">

 

 



 

Thanks @EvaF it works :)

 

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