If this is your first visit, check our documentation. To post, register or login.
Forums / Code Collaboration / How to refresh shopping cart page?

How to refresh shopping cart page?

Started by dave224 on 25 Jul 2020, 8:13 PM · Views: 5,939

Results 1 to 5 of 5
25 Jul 2020, 8:13 PM
#1
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

How to refresh shopping cart page?

I have a requirement to prohibit a certain group of customers from purchasing one or more specific products. I've seen the add-on which effectively hides the category and/or products from a specified customer group. But I have had problems setting up a development branch to integrate the add-on. So I decided to work on an extra cart action that removes the prohibited products from a customers cart and displays a message on the shopping cart page telling them that the product(s) will not be added to the cart. The shopping cart page should remain displayed with the prohibited products not displayed in the cart, and with updated subtotal price.

I have used the shopping cart class method "remove(product_id)" and while notifier trace tells me that the remove method is executed for the prohibited products, and the warning message is displayed, the products remain displayed on the shopping cart page when multiple prohibited products are selected from the product listing page. There are no php errors logged. I think that, even though the products have been removed, the page display has not been updated to reflect the current cart contents. How can I refresh the shopping cart display? I'll admit there could be reasons for the problem other than no cart refresh. The code to call the remove method is:

$_SESSION['cart']->remove($pid);

Anyone's help is greatly appreciated!

Dave

zc155f, php7.1.x, heavy customization

25 Jul 2020, 9:57 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: How to refresh shopping cart page?

I recommend calling actionRemoveProduct() instead. You can see an example of it in main_cart_actions.php

When it's finished it will refresh the page with the updated data.

Code, circa 1.5.5:
https://github.com/zencart/zencart/blob/c31871ba8eda38504fe8332c67a176071f5a166f/includes/classes/shopping_cart.php#L2205-L2207

If you have multiple products to handle, then go ahead with calling remove() on them in a loop, and then call actionRemoveProduct ... or just do the same redirect that it does.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

28 Jul 2020, 12:18 AM
#3
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: How to refresh shopping cart page?

Thank you DrByte! Works great. I have one more minor problem. When I add one prohibited item to an empty cart, that the extra cart action removes, the message I want displayed doesn't appear on the empty cart page using $messageStack->add_session('shopping_page',...). I got around that by using $messageStack->add_session('header',...) which displays the message at the top of the empty cart page. But I would prefer the message be displayed in the center column. I also tried $messageStack->add('shopping_cart',...), but that didn't help. Everything works perfectly if there are other items in the cart.

How can I get the message to display in the center column on the empty cart page?

Thank you.
Dave

28 Jul 2020, 11:10 AM
#4
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: How to refresh shopping cart page?

Dave224:

Thank you DrByte! Works great. I have one more minor problem. When I add one prohibited item to an empty cart, that the extra cart action removes, the message I want displayed doesn't appear on the empty cart page using $messageStack->add_session('shopping_page',...). I got around that by using $messageStack->add_session('header',...) which displays the message at the top of the empty cart page. But I would prefer the message be displayed in the center column. I also tried $messageStack->add('shopping_cart',...), but that didn't help. Everything works perfectly if there are other items in the cart.

How can I get the message to display in the center column on the empty cart page?

Thank you.
Dave
For whatever reason, the as-shipped tpl_shopping_cart_default.php doesn't invoke the messageStack on an empty cart. Find this section (starting on line 181 for the zc157 template_default version):

<?php
  } else {
?>

<h2 id="cartEmptyText"><?php echo TEXT_CART_EMPTY; ?></h2>

<?php
$show_display_shopping_cart_empty = $db->Execute(SQL_SHOW_SHOPPING_CART_EMPTY);

while (!$show_display_shopping_cart_empty->EOF) {
?>

... and add the message-stack display:

<?php
  } else {
?>

<h2 id="cartEmptyText"><?php echo TEXT_CART_EMPTY; ?></h2>
[B]<?php if ($messageStack->size('shopping_cart') > 0) echo $messageStack->output('shopping_cart'); ?>[/B]
<?php
$show_display_shopping_cart_empty = $db->Execute(SQL_SHOW_SHOPPING_CART_EMPTY);

while (!$show_display_shopping_cart_empty->EOF) {
?>
28 Jul 2020, 3:03 PM
#5
dbltoe avatar

dbltoe

Totally Zenned

Join Date:
Jan 2004
Location:
N of San Antonio TX
Posts:
9,904
Plugin Contributions:
23

Re: How to refresh shopping cart page?

If you are using responsive_classic or a clone of it, the line number is 209 for the responsive_classic version of the tpl_shopping_cart_default.php in 1.5.7.

Need a hand with Zen Cart? Hire the team at myZenCartZone.com for upgrades, repairs, and custom plugins.
Zen Cart Certified hosting at myZenCartHost.com