• I made some edits to WooCommerce in which I multiply the item price time the item weight on certain items. This is all working well, expect for this one case I’ve run across. When the item gets added to the cart, the line total in the side cart is correct. However, if I refresh the page, the line total in the side cart reverts back to the original price of the item. BUT, if I add another item to the cart, the item that reverted displays the correct price, until I refresh again. I realize you are returning the HTML for the side cart on every ajax request, but it seems on some request it pulls the original item price and other times it actually gets what is in the cart (adjusted price). Any idea??

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author xootix

    (@xootix)

    Hello @douboy

    HTML is always fetched via ajax, whether its on page load or on add to cart.
    You need to update your prices using this hook
    woocommerce_before_cart_contents

    Thread Starter douboy

    (@douboy)

    I did that and it works fine, until the page is refreshed. Then it reverts to the original prices, not the prices I calculate in the hook.

    Thread Starter douboy

    (@douboy)

    Only the line total.

    Plugin Author xootix

    (@xootix)

    This works fine for me.

    add_action( 'woocommerce_before_cart_contents', 'custom_cart_total' );
    function custom_cart_total() {
    
        WC()->cart->subtotal = 0;
        WC()->cart->total = 0;
    
    }
    
    Thread Starter douboy

    (@douboy)

    Sorry, just noticed I was referencing the woocommerce_before_calculate_totals hook. I’ll try your recommendation. Thank you!

    Thread Starter douboy

    (@douboy)

    The woocommerce_before_cart_contents hook didn’t work at all. It used the no altered prices. Also, in your last post, I am not talking about the cart total, I’m talking about the line item total. The cart total is correct for me.

    Plugin Author xootix

    (@xootix)

    I only gave you an example to show how to update cart prices with this hook.
    Does your code works with the normal cart & checkout pages?
    Please share it so I can test. Give this code a try as well.

    add_action( 'woocommerce_before_calculate_totals', 'custom_cart_total' );
    function custom_cart_total() {
    
        foreach ( WC()->cart->get_cart() as $hash => $value ) {
        	$value['data']->set_price( 20 );
        }
    
    }
    add_action( 'woocommerce_before_cart_contents', function(){
    	do_action( 'woocommerce_before_calculate_totals' );
    }, 0 );
    Thread Starter douboy

    (@douboy)

    Here is my code:

    add_filter( 'woocommerce_get_price_html', 'calculate_price_by_multiplier', 10, 2 );
    add_filter( 'woocommerce_cart_item_price', 'custom_cart_price_display', 10, 3);
    add_action( 'woocommerce_before_calculate_totals', 'custom_cart_price'); 
    
    function calculate_price_by_multiplier($price, $product){
    	
    	if (is_null($product)) {
    		return $price;
    	}
    	
    	$multiplier = get_field('price_multiplier');
    	$price = $product->get_price();
    	
        if ($multiplier > 0){
            $price = $multiplier * $price;
        }
    	
        return wc_price($price);
    }
    
    function custom_cart_price_display($price_text, $product_data, $cart_key) {
    	
    	if (is_null($product_data)){
        	return $price_text;
        }
    	
    	$product_id = (empty($product_data['variation_id'])) ? $product_data['product_id'] : $product_data['variation_id'];
    	$product = wc_get_product($product_id);
    	//$weight = floatval($product->get_weight());
    	$price = $product->get_price();
    	$multiplier = get_field('price_multiplier', $product_id);
    	
    	if ($multiplier > 0) {
    		$price = $price * $multiplier;
    	}
    	
    	return wc_price($price);
    
    }
    
    function custom_cart_price( $cart_object ) {
    	
    	$cart_items = $cart_object->cart_contents;
    
    	if ( ! empty( $cart_items ) ) {
    		
    		foreach ( $cart_items as $key => $value ) {
    			$product_id = $value['product_id'];
    			$multiplier = get_field('price_multiplier', $product_id);
    			$price = $value['data']->get_regular_price();
    			
    			if ($multiplier > 0){
    				$value['data']->set_price( $price * $multiplier );
        		}
    		}
    	}
        
    }
    Plugin Author xootix

    (@xootix)

    Ok, made some tests.
    Your code is absolutely fine.
    Now the extra snippet you have to add to make it work with side cart is

    add_action( 'woocommerce_before_cart_contents', function(){
    	WC()->cart->calculate_totals();
    }, 0 );
    Thread Starter douboy

    (@douboy)

    Perfect..thank you very much! Great support. Will be buying the pro version today.

    Plugin Author xootix

    (@xootix)

    You’re Welcome ??
    Would appreciate if you rate the plugin here
    Please mark it resolved if you have no further issues.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Line Total Not Showing Altered Price’ is closed to new replies.