[solved] Cart - Smarty or UDT for delleting product with specific id from Cart

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
lapak
Forum Members
Forum Members
Posts: 33
Joined: Mon Sep 06, 2010 8:43 pm

[solved] Cart - Smarty or UDT for delleting product with specific id from Cart

Post by lapak »

Hello, I'm still struggling with quite specific e-shop and now i get stuck with making page, where customer can choose from several delivery methods. Because I didn't find module, which allow customer choose from different delivery methods, which have different prices, and at tops I also need free delivery, when total price is higher than x, I'm trying to do simple page, where choose of delivery company simply adds specific product (example of product: UPS Packet) to the Cart. That isn't so difficult, but when customer do that, than he (for some reason) return back to the shop, and then he visit this delivery selection page again, It would add second delivery product to the Cart and he will pay it twice. So I would like to add some smarty or UDT on top of this page, which simply look into the Cart, and if there will be product with specified id (which will be these delivery method products), it will remove it from Cart. (And then user chooses method again, it will add it again, and everything is fine...)

I'm of course trying to figure it out myself and so far my Delivery method page looks like this:
{Cart product=7} // this is one of the delivery methods products
{Cart action='viewcart' viewcarttemplate='viewcart_init'} // viewcart_init template is absolutely blank, I just get Cart variables filled in

and now, when I put there {$cartitems|print_r}, If i understand it right, i have there array with Objects, which represents Products. For sure, I'll put the output there, because I'm still not big friend with arrays and objects... (In this example, there are two products in cart: )

Code: Select all

stdClass Object ( [product_id] => 2 [sku] => 00301 [quantity] => 1 [base_price] => 0 [type] => 1 [pending] => [unit_weight] => 0 [unit_price] => 123 [summary] => Makové snění Platno - 20x30 (123.00)  [item_total] => [file_location] => http://localhost/linnette-obrazy/uploads/Products/product_2 [flds] => Array ( [image] => stdClass Object *RECURSION* [pomer_stran] => stdClass Object *RECURSION* ) [categories] => Array ( [0] => stdClass Object ( [id] => 1 [name] => Bokeh [value] => [file_location] => http://localhost/linnette-obrazy/uploads/Products/categories/1 ) [1] => stdClass Object ( [id] => 2 [name] => Detail a zátiší [value] => [file_location] => http://localhost/linnette-obrazy/uploads/Products/categories/2 ) [2] => stdClass Object ( [id] => 3 [name] => Anything else [value] => [file_location] => http://localhost/linnette-obrazy/uploads/Products/categories/3 ) ) [quantity_box] =>  [remove_box] =>  ) stdClass Object ( [product_id] => 7 [sku] => [quantity] => 1 [base_price] => 100 [type] => 1 [pending] => [unit_weight] => 0 [unit_price] => 100 [summary] => Poštovné - Česká Pošta  [item_total] => [file_location] => http://localhost/linnette-obrazy/uploads/Products/product_7 [flds] => Array ( [image] => stdClass Object *RECURSION* [pomer_stran] => stdClass Object *RECURSION* ) [categories] => Array ( [0] => stdClass Object ( [id] => 1 [name] => Bokeh [value] => [file_location] => http://localhost/linnette-obrazy/uploads/Products/categories/1 ) [1] => stdClass Object ( [id] => 2 [name] => Detail a zátiší [value] => [file_location] => http://localhost/linnette-obrazy/uploads/Products/categories/2 ) [2] => stdClass Object ( [id] => 3 [name] => Anything else [value] => [file_location] => http://localhost/linnette-obrazy/uploads/Products/categories/3 ) ) [quantity_box] =>  [remove_box] =>  ) Array Array
So now I need smarty or UDT, which comes to top of my page and my thinking goes this way:
{foreach from objects in $cartitems}
{if id of product == x}
 delete from $cartitems object with this id
{/if}
{/foreach}

So I hope, that you understand my crappy English and I will be very appreciated for any help or advice.
Last edited by lapak on Fri Sep 17, 2010 2:49 pm, edited 1 time in total.
lapak
Forum Members
Forum Members
Posts: 33
Joined: Mon Sep 06, 2010 8:43 pm

Re: Cart - Smarty or UDT for delleting product with specific id from Cart

Post by lapak »

ok, so I started to learn some PHP, and delleting specific item form array wasn't so difficult at all :)
So far, my UDT now looks like this:

Code: Select all

$cartitems = $params['cartitems'];

$i = 0;
while ($i <= (count($cartitems)-1)) {
 if($cartitems[$i]->product_id == 7) {
  unset($cartitems[$i]);
 };
$i++;
};

$cartitems = array_values($cartitems);
$smarty->assign('cartitems', $cartitems);
So now, i have in {$cartitems} only items, which i want. But now, of course, when I return to Cart, or any other page, In Cart is everything as It was before. It's logical, cos cart still use data, from his own storage. So now I need to store items from my new variable, where Cart module stores items. And I don't know, where is that :)
lapak
Forum Members
Forum Members
Posts: 33
Joined: Mon Sep 06, 2010 8:43 pm

Re: Cart - Smarty or UDT for delleting product with specific id from Cart

Post by lapak »

It seems, that I'm just talking to myself, but anyway, I got the solution, so if somebody will ever need it, here it is:

Make UDT, for example called delete_delivery:

Code: Select all

$id_postovneho = 7; //here comes id of product, which we want to delete

$lin_items = unserialize(base64_decode($_SESSION[cgcart]));

$i = 0;
while ($i <= (count($lin_items)-1)) {
 if($lin_items[$i]->get_product_id() == $id_postovneho) {
  unset($lin_items[$i]);
 };
$i++;
};

$lin_items = array_values($lin_items);

$lin_towrite = base64_encode(serialize($lin_items));

$_SESSION["cgcart"] = $lin_towrite;
And just simply call this UDT in template with {delete_delivery} and voila, product is gone from Cart...
spinfold
Forum Members
Forum Members
Posts: 42
Joined: Fri Nov 12, 2010 10:15 am

Re: [solved] Cart - Smarty or UDT for delleting product with

Post by spinfold »

I know this is (almost exactly) 2 years old, but this just got me out of a hole. Many thanks!
Post Reply

Return to “Modules/Add-Ons”