Link to home
Start Free TrialLog in
Avatar of sharingsunshine
sharingsunshineFlag for United States of America

asked on

Woo Commerce Function Displaying Duplicate Values

I need help with why this woo commerce function is returning duplicate values.

add_action ('woocommerce_review_order_after_cart_contents', 'display_item_qv', 10);

function display_item_qv () {

	global $wpdb;
	
	//$wpdb->show_errors = TRUE;
//$wpdb->suppress_errors = FALSE;
	
	$val = 0;
	
	foreach( WC()->cart->get_cart() as $cart_item ) {
   $product_in_cart = $cart_item['product_id'];
   

$item_qv = $wpdb->get_row("
SELECT DISTINCT
    p.id AS 'Product ID',
    p.post_title AS 'Product Name',
    t.name AS 'QV'
FROM 
	wp_posts AS p
INNER JOIN
    wp_term_relationships AS tr ON p.id = tr.object_id
INNER JOIN
	wp_term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
INNER JOIN
    wp_terms AS t ON t.term_id = tt.term_id
WHERE 
	tt.taxonomy='pa_qv'
AND 
	p.post_type = 'product'

AND id=$product_in_cart", ARRAY_N);
	

		
		
$val += $item_qv[2] * $cart_item['quantity']; 
	//echo $item_qv[2];
	/*echo "<pre>";
	print_r($item_qv);
	echo "</pre>";*/
	
	}
echo "The total is ". $val;
	//print_r($item}
}

However, it is giving duplicate values and I have been unable to remove the duplicate value.  Please tell me what I need to change to just get one value.  

Here is a screenshot
https://gyazo.com/a442f57ffba799a48417d65a260217a0 - the 12.1 12.1 at the top of the image on the right side should only be 12.1 once. 

If this can't be fixed, please tell me how to use Class WC_Product_Attribute instead.

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Just casting a quick glance over your code. You're running the code in a loop, depending on how items you have in the cart. If you have 2 items, then it will run over it twice. Can't see from your screen shot what you have in there.

Also, the code above should be outputting The total is xxx, but that's not showing in your screenshot, so there is some discrepancy between the code and the screenshot.
Avatar of sharingsunshine

ASKER

Let me clear up those discrepancies
https://gyazo.com/c751c429bb4ec5cebf049f8dd076313f - this is the cart.

Here is my latest code
add_action ('woocommerce_review_order_after_cart_contents', 'display_item_qv', 10);

function display_item_qv () {

	global $wpdb;
	
	//$wpdb->show_errors = TRUE;
//$wpdb->suppress_errors = FALSE;
	
	$val = 0;
	
	foreach( WC()->cart->get_cart() as $cart_item ) {
   $product_in_cart = $cart_item['product_id'];
   

$item_qv = $wpdb->get_row("
		
SELECT DISTINCT
    p.id AS 'Product ID',
    p.post_title AS 'Product Name',
    t.name AS 'QV'
FROM 
	wp_posts AS p
INNER JOIN
    wp_term_relationships AS tr ON p.id = tr.object_id
INNER JOIN
	wp_term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
INNER JOIN
    wp_terms AS t ON t.term_id = tt.term_id
WHERE 
	tt.taxonomy='pa_qv'
AND 
	p.post_type = 'product'

AND id=$product_in_cart", ARRAY_N);
	

		
$val += $item_qv[2] * $cart_item['quantity']; 
	//echo $item_qv[2];
	//echo "<pre>";
	//print_r($item_qv);
	//echo "</pre>";
	
	}
echo "The total is ". $val;
	//print_r($item}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did it!  Thanks, so much.

If you don't mind can you tell me how you knew that was the problem?
Excellent news.

I tested the hook on my sandbox installation of WooCommerce, just echoing out a simple "Testing" string. It showed up twice, so it was clear that the hook was firing twice - a quick Google search for "woocommerce_review_order_after_cart_contents hook firing twice" identified it as an AJAX issue. Dropped in the is_axax() check and it only echoed once ... problem solved :)
OK, thanks for the explanation and ultimately solving the problem.