1
votes

In Woocommerce I am using woocommerce_product_get_price like to alter my product prices and it is working fineā€¦ But when I add to cart a product, the cart total and cart item are zero.

That is my code:

add_filter( 'woocommerce_add_cart_item', 'set_custom_cart_item_prices', 20, 2 ); 
function set_custom_cart_item_prices( $cart_data, $cart_item_key ) {
    $new_price = $cart_data['data']->get_price() * 2;
    $cart_data['data']->set_price( $new_price );
    $cart_data['new_price'] = $new_price;
    return $cart_data;
}

add_filter( 'woocommerce_get_cart_item_from_session', 'set_custom_cart_item_prices_from_session', 20, 3 );
function set_custom_cart_item_prices_from_session( $session_data, $values, $key ) {
    if ( ! isset( $session_data['new_price'] ) || empty ( $session_data['new_price'] ) )
        return $session_data;

    $session_data['data']->set_price( $session_data['new_price'] );

    return $session_data;
}

Its doesn't work. I have also tried to use woocommerce_before_calculate_totals without success

How to avoid zero prices on cart? Any help is appreciated.

1

1 Answers

0
votes

Updated: You are not using the correct hooks and the correct way. The following will replace all your code. The price changes will be done on simple products, variable products and product variations.

The 1st function is the rate to be applied to your product prices (so you will define in it your rate calculation).

The 2nd function will change all products displayed price on all product pages instead of using your custom function hooked in woocommerce_product_get_price filter hook.

The 3rd Function will change the products price in cart items, on cart and checkout pages and in orders items too.

// The price rate to be applied
function get_product_price_rate() {
    // HERE define the price rate to be applied
    return 1.25; // +25%
}

// Change the product displayed price on product pages
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 10, 2 );
function custom_price_html( $price, $product ){
    $rate = get_product_price_rate();

    // Simple products and product variations
    if( in_array( $product->get_type() , array(  'simple' , 'variation' ) ) ) {
        $regular_price = wc_get_price_to_display( $product, array( 'price' => ( $product->get_regular_price() * $rate ) ) );
        $active_price  = wc_get_price_to_display( $product, array( 'price' => ( $product->get_price() * $rate ) ) );

        if ( '' === $product->get_price() ) {
            $price = apply_filters( 'woocommerce_empty_price_html', '', $product );
        } elseif ( $product->is_on_sale() ) {
            $price = wc_format_sale_price( $regular_price, $active_price ) . $product->get_price_suffix();
        } else {
            $price = wc_price( $active_price ) . $product->get_price_suffix();
        }
    }
    // Variable products
    elseif ( 'variable' === $product->get_type() ) {
        $prices = $product->get_variation_prices( true );

        if ( empty( $prices['price'] ) ) {
            $price = apply_filters( 'woocommerce_variable_empty_price_html', '', $this );
        } else {
            $min_price     = current( $prices['price'] ) * $rate;
            $max_price     = end( $prices['price'] ) * $rate;
            $min_reg_price = current( $prices['regular_price'] ) * $rate;
            $max_reg_price = end( $prices['regular_price'] ) * $rate;

            if ( $min_price !== $max_price ) {
                $price = wc_format_price_range( $min_price, $max_price );
            } elseif ( $this->is_on_sale() && $min_reg_price === $max_reg_price ) {
                $price = wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) );
            } else {
                $price = wc_price( $min_price );
            }
            $price .= $product->get_price_suffix();
        }
    }
    return $price;
}

// Change cart items prices (and order items prices)
add_action('woocommerce_before_calculate_totals', 'increase_cart_item_prices', 100, 1 );
function increase_cart_item_prices( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop Through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        $rate = get_product_price_rate();
        $cart_item['data']->set_price( $cart_item['data']->get_price() * $rate );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.