Jump to content

Show product flag "Product is already in cart" in product-list


dianacd

Recommended Posts

Hello there, 


I am new developer in Prestashop, I use Prestashop 1.7

 

I added a sold out flag when product quantity is 0 with the following code:

 

    {if $product.quantity==0}
        <li>
            <img src="soldout.png" style="position:absolute;max-width:65px" />
        </li>
    {/if}


But when the customer adds the product to the cart (if the only quantity available is 1) the sold out label will be shown in the product list.


I would like to instead, show a flag that says "Already in cart" in this case but I am not sure how to verify if the current product is on cart.

 

Someone has any idea in how to do the condition to verify current product is in cart?

 

Thanks!

Link to comment
Share on other sites

Hi there,

This is my idea. You need to create a new module to show it.

1. 1 file js, after loading product list page, it will get the all id products (you can get id_product by product url, data_idproduct, ...) of this page, after that use ajax >> sending this list id_product to front controller of module.

2. Front controller will get list id_product,

get the products in the current cart ($product = $this->context->cart->getProducts(true))

=> compare 2 arrays => product in cart

3. in function success of js, check and show the flag (already in cart)

Thanks,

Link to comment
Share on other sites

3 hours ago, prestamonste said:

Hi there,

This is my idea. You need to create a new module to show it.

1. 1 file js, after loading product list page, it will get the all id products (you can get id_product by product url, data_idproduct, ...) of this page, after that use ajax >> sending this list id_product to front controller of module.

2. Front controller will get list id_product,

get the products in the current cart ($product = $this->context->cart->getProducts(true))

=> compare 2 arrays => product in cart

3. in function success of js, check and show the flag (already in cart)

Thanks,

Thank you for your idea!! I will try this and post the outcome :)

Link to comment
Share on other sites

Need to add the same in the product controller.
$product_id = (product ID);
$item_exist_cart = 0;
foreach($this->context->cart->getProducts() as $key => $value){
    if($product_id == $value['id_product']){
        $item_exist_cart = 1;
    }
}

Assign $item_exist_cart from controller & use the varabile in the tpl to show the message.

Link to comment
Share on other sites

  • 8 months later...
On 13/7/2020 at 9:57 AM, dianacd dice:

Ciao, 


Sono un nuovo sviluppatore in Prestashop, utilizzo Prestashop 1.7

 

Ho aggiunto un flag di esaurimento quando la quantità del prodotto è 0 con il seguente codice:

 

    {if $ product.quantity == 0}
        <li>
            <img src = "soldout.png" style = "position: absolute; max-width: 65px" />
        </li>
    {/ if}


Ma quando il cliente aggiunge il prodotto al carrello (se l'unica quantità disponibile è 1) l'etichetta esaurita verrà mostrata nell'elenco dei prodotti.


Vorrei invece mostrare un flag che dice "Già nel carrello" in questo caso ma non sono sicuro di come verificare se il prodotto corrente è nel carrello.

 

Qualcuno ha idea di come fare la condizione per verificare che il prodotto corrente sia nel carrello?

 

Grazie!

in quale file hai aggiunto il codice ?

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