• Resolved rgbear

    (@rgbear)


    Hi, I wrote a function to customize product prices, but I need to edit them inside cart and checkout page. The problem is that nay function that tries to edit the prices inside the cart, don’t work, I think because there is the other filter that already changes the prices: there is probabily a conflict I don’t know how to solve.

    FUNCTION THAT CHANGE PRICES GLOBALLY

    function product_custom_price($price, $product) {
    if ( ! is_page( 'cart' ) || ! is_cart() || ! is_checkout() || ! is_page( 'checkout' ) ) {
    
    		$custom_price = $product->get_regular_price();
    		$my_current_lang = apply_filters( 'wpml_current_language', NULL );
    		//ITALIA 
    		$ssivata = 7.93;
    		$ivaitaperprezzofabbrica = 122/100;
    		$arrotondamento = 4/10;
    		if( $my_current_lang =='it' ) {
    		$tax = 122/100;
    		$shipping = 6.5;
    		}
    		if( $my_current_lang =='at' ) {
    			$tax = 120/100;
    			$shipping = 24;
    		}
    		//calcola prezzo di fabbrica
    		$custom_price1 = $custom_price - $ssivata;
    		$custom_price2 = $custom_price1 / $ivaitaperprezzofabbrica;
    		//calcolo del prezzo
    		$custom_price3 = $custom_price2 + $shipping;
    		$custom_price4 = $custom_price3 * $tax;
    		$custom_price5 = $custom_price4 + $arrotondamento;
    		return round($custom_price5,0,PHP_ROUND_HALF_UP);
    	}
    }
    add_filter('woocommerce_product_get_price', 'product_custom_price', 10, 2);
    add_filter( 'woocommerce_product_variation_get_price', 'product_custom_price', 10, 2 );
    add_filter( 'woocommerce_product_get_sale_price', 'product_custom_price', 10, 2 );

    FUNCTION THAT should CHANGE PRICES IN CART (the value of the price is just for test)

    if ( is_page( 'cart' ) || is_cart() || is_checkout() || is_page( 'checkout' ) ) {
    
    add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 20, 1);
    
    	function add_custom_price( $cart_obj ) {
    
    		// This is necessary for WC 3.0+
    		if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    			return;
    
    		// Avoiding hook repetition (when using price calculations for example)
    		if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
    			return;
    
    		// Loop through cart items
    		foreach ( $cart_obj->get_cart() as $cart_item ) {
    			$cart_item['data']->set_price( 40 );
    		}
    	}
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter rgbear

    (@rgbear)

    Ok I removed the conflicting filter, now works better.

    add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 20, 1);
    
    //if ( is_page( 'cart' ) || is_cart() || is_checkout() || is_page( 'checkout' ) ) {
    	remove_filter( 'woocommerce_product_get_price', 'product_custom_price', 10, 2 );
    	remove_filter( 'woocommerce_product_variation_get_price', 'product_custom_price', 10, 2 );
    	remove_filter( 'woocommerce_product_get_sale_price', 'product_custom_price', 10, 2 );
    //}
    
    	function add_custom_price( $cart_obj ) {
    
    			// This is necessary for WC 3.0+
    			if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    				return;
    
    			// Avoiding hook repetition (when using price calculations for example)
    			if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
    				return;
    				global $woocommerce;
    				$ssivata = 7.93;
    				$ivaitaperprezzofabbrica = 122/100;
    				$arrotondamento = 4/10;
    				$my_current_lang = apply_filters( 'wpml_current_language', NULL );
    				$selected_country = $woocommerce->customer->get_shipping_country();
    
    				if( $my_current_lang =='it' || $selected_country =='IT' ) {
    				// Loop through cart items
    				foreach ( $cart_obj->get_cart() as $cart_item ) {
    					$cart_item['data']->set_price( 40 );
    					}
    				}
    				if( $my_current_lang =='at' || $selected_country =='AT' ) {
    					foreach ( $cart_obj->get_cart() as $cart_item ) {
    						$cart_item['data']->set_price( 4067414 );
    					}
    				}
    	}
    Luminus Alabi a11n

    (@luminus)

    Automattic Happiness Engineer

    Hi @rgbear,

    You mention that it works better. I presume that means you’ve got it sorted.

    Thread Starter rgbear

    (@rgbear)

    Hi @luminus , everything seems to work. Thank you for the reply. I set “Mark as resolved”

    I want to set custom price on specific product on cart. above the code set all product same price but i want to do on specific product. is that possible?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Override custom prices in cart/checkout’ is closed to new replies.