Jump to content
Sign in to follow this  
motorcity

MPQ doesn't update Cart in Header

Recommended Posts

I trying to get Minimum Products Quantity to update both the quantity in shopping_cart.php and header.php

What I'm getting is the cart in header updates to any quantity you set, including less than the minimum quantity. But the shopping_cart page enforces the minimum quantity. So you can have two different quantities and totals displaying at the same time.

Here's my Cart-In-Header code;

<td><?php echo ' <a rel="nofollow" href="' . tep_href_link(FILENAME_SHOPPING_CART, '', 'SSL') . '">' . ( HEADER_TITLE_CART_CONTENTS) . '</a>' ; ?>
<br>
<b><?php echo $cart->count_contents() . ' ' ;?>
<?php echo $cart->count_contents() == "1" ? "Item " : "Items " ; ?>
<br><b>
<?php echo $currencies->format($cart->show_total());
?>

 

And here's the MPQ part added to catalog/shopping_cart.php

//Minimum quantity code
if(MINIMUM_ORDERS == 'true'){
 $min_order_query = tep_db_query("select p.minorder as min_quant FROM " . TABLE_PRODUCTS . " p where p.products_id = '".$products[$i]['id']."'");
while ($min_order = tep_db_fetch_array($min_order_query)) {
if ($products[$i]['quantity'] < $min_order['min_quant'] ) {
$products[$i]['min_quant']=$min_order['min_quant'];
}
}
if ($products[$i]['quantity'] < $products[$i]['min_quant'] ) {
$products[$i]['quantity']=$products[$i]['min_quant'];
$cart->add_cart($products[$i]['id'],$products[$i]['quantity'],$products[$i]['attributes']);
$cart_notice = sprintf(MINIMUM_ORDER_NOTICE, $products[$i]["name"], $products[$i]["min_quant"]);
}
}
//End Minimum quantity code

Share this post


Link to post
Share on other sites

Hi

 

I'm not au fait with the contribution but the code in shopping_cart is actually adding items whereas the

$cart->count_contents()

in the cart header is only counting what it believes is in the cart.

 

I think that the add->cart bit in the shopping cart should be in application_top (there's a bit of code in there under case 'add product') as unless you have redirect to cart enforced after a 'buy now' then the situation may arise for example where I go to your widget page and buy a widget - unless the store send me to shopping_cart then the cart header will show (1) - if I then go to shopping_cart the min quantity code will kick in and change the quantity in the cart but the header may still just say (1) depending when it's code is called (i.e..before or after

$cart->add_cart

 

)

 

Can you show the link to the add-on?

Share this post


Link to post
Share on other sites

Thanks Bob,

here's MPQ with Admin http://addons.oscommerce.com/info/4155

You're right about the header just counting the number of products, but if a change is made that doesn't involve the MPQ code the header updates immediately. So I guess the issue must be with the MPQ code.

Share this post


Link to post
Share on other sites

The problem with that "MQP code" is, that the concept is wrong.

 

It adjust the cart quantity only on the shipping cart page, the cart update the info only if changes are done on shopping_cart.php

 

the right place to do this would be in includes/classes/shopping_cart.php, there are 2 functions of relevance, add_cart() and update(), they need to adjust the quantity according to your min quan

 

Doing this, the cart info will be accurate all time for all store

Share this post


Link to post
Share on other sites

Okay, I appreciate the help.

Any idea if this has already been done using the class file instead? I thought I was using the most common mainstream MPQ but it wouldn't be the first time I was off on finding the contrib.

Share this post


Link to post
Share on other sites

Well the good news is actually bad, I'd rather see errors than have code mods be ignored.

This is what I've got so far;

(The no_break thing is a per product switch I'm going to use instead of an across the board config value)

includes/classes/shopping_cart.php

function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) {
 global $new_products_id_in_cart, $customer_id;
 $products_id_string = tep_get_uprid($products_id, $attributes);
 $products_id = tep_get_prid($products_id_string);
//my add
if ($products['minorder'] > '1' && $products['no_break'] == '1') {
$min_order_query = tep_db_query("select p.minorder as min_quant FROM " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
while ($min_order = tep_db_fetch_array($min_order_query)) {
if ($qty < $min_order['min_quant'] ) {
$products['min_quant']=$min_order['min_quant'];
}
}
if ($qty < $products['min_quant'] ) {
$qty = $products['min_quant'];
}
}
//end add
 if (defined('MAX_QTY_IN_CART') && (MAX_QTY_IN_CART > 0) && ((int)$qty > MAX_QTY_IN_CART)) {
 $qty = MAX_QTY_IN_CART;
 }

 

function update_quantity($products_id, $quantity = '', $attributes = '') {
 global $customer_id;
 $products_id_string = tep_get_uprid($products_id, $attributes);
 $products_id = tep_get_prid($products_id_string);
//my add
if ($products['minorder'] > '1' && $products['no_break'] == '1') {
$min_order_query = tep_db_query("select p.minorder as min_quant FROM " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
while ($min_order = tep_db_fetch_array($min_order_query)) {
if ($quantity < $min_order['min_quant'] ) {
$products['min_quant']=$min_order['min_quant'];
}
}
if ($quantity < $products['min_quant'] ) {
$quantity = $products['min_quant'];
}
}
//end add

Share this post


Link to post
Share on other sites

It's not p.minorder should be just minorder in both db calls.

Tested that, No difference.

I suspect you're saying I need a query to grab both minorder & no_break before I try to make them do something?

If so, should that be before, after or part of the function call?

They're both called later in the file as $products = tep_db_fetch_array($products_query...

Share this post


Link to post
Share on other sites

You do following comparison

 

 

if ($products['minorder'] > '1' && $products['no_break'] == '1')

 

The $products['minorder'] that you use, must have a value, otherwise the comparison is useless

 

Where does this value come from in your code, how does the system know what $products['minorder'] is?

 

I think from nowhere, there is no value

 

It also make no sense that you query for exactly that value, the minorder, that you already used in your comparison

 

You need first to get the value, then you can use it to modify the quantity, in the same way it is done for max qty in cart

Share this post


Link to post
Share on other sites

In the implementation I use for min quantity/max quantity and quantity steps, I'm passing the qty via a class that do all adjustments and return the correct quantity to use. You can see this happening also in QPBPP where I got the idea from

 

This I do in includes/classes/shopping_cart.php, this seems to be the safest place to do since it covers all possible cases, doesn't matter where the action is initiated

 

application_top.php is same good IF all add to cart/update actions are done using 'action' in the url (buy_now, add_product, update etc). It can/could be that some modification is adding stuff to the cart directly, not via application_top.php

Edited by multimixer

Share this post


Link to post
Share on other sites

Hi Joe - try something more like this

 

function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) {
	 global $new_products_id_in_cart, $customer_id;
	 $products_id_string = tep_get_uprid($products_id, $attributes);
	 $products_id = tep_get_prid($products_id_string);
//my add

$min_order_query = tep_db_query("select minorder, no_break FROM " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");

while ($min_order = tep_db_fetch_array($min_order_query)) {
if ($min_order['minorder'] > '1' && $min_order['no_break'] == '1') {
if ($qty < $min_order['minorder'] ) {
 $qty = $products['minorder'];
}
}
}
//end add
	 if (defined('MAX_QTY_IN_CART') && (MAX_QTY_IN_CART > 0) && ((int)$qty > MAX_QTY_IN_CART)) {
	 $qty = MAX_QTY_IN_CART;
	 }

Share this post


Link to post
Share on other sites

Thanks Bob. That's getting me closer.

Seems 'll have to dig into the code for both product_info & shopping_cart as it works somewhat...

Share this post


Link to post
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
Sign in to follow this  

×