Change price of product in WooCommerce cart and checkout
Asked Answered
E

2

6

I'm creating a WooCommerce add-on to customize the product, based on selected options by the visitor and custom price is calculated and stored into session table also.
I am able to get that value in cart page also.

My problem:
I would like to change the default price of the product and replace it with new calculated value in the WooCommerce process as cart, checkout, payment, mail notifications, order...

Any advice please?

Exophthalmos answered 7/7, 2017 at 15:32 Comment(0)
E
7

Ce right hook to get it working is woocommerce_before_calculate_totals. But you will have to complete (replace) the code to get the new price in the hooked function below:

add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_prices', 10, 1 );
function custom_cart_items_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 ) {
        // Get the product id (or the variation id)
        $product_id = $cart_item['data']->get_id();

        // GET THE NEW PRICE (code to be replace by yours)
        $new_price = 500; // <== Add your code HERE

        // Updated cart item price
        $cart_item['data']->set_price( $new_price ); 
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works on WooCommerce versions 3+. But as you don't give any code I can't test it for real getting the new price from session…

Elvera answered 7/7, 2017 at 21:38 Comment(3)
above code is working fine but after placing order in that payment page the original product price is being calculated.Legalese
Could you explain the 'DOING_AJAX' and did_action(...) >= 2 parts?Diegodiehard
@Legalese and others, I made use of the addition of an apply_filters('woocommerce_get_cart_contents', 'custom_cart_items_prices_refresh', 0, 1); to make sure it always shows what it's supposed to. You may have to customize the _refresh function, rather than make it identical to the other one.Diegodiehard
S
0
function save_subscription_wrap_data( $cart_item_data, $product_id ) {
    $include_as_a_addon_subscription = get_field('include_as_a_addon_subscription',$product_id);
    $subscricption_product_data = get_field('subscricption_product',$product_id);
    $current_user  = is_user_logged_in() ? wp_get_current_user() : null;
    $subscriptions = wcs_get_users_subscriptions( $current_user->ID );
    if($include_as_a_addon_subscription == "yes")
    {
        foreach ( $subscriptions as $subscription_id =>  $subscription ) {
            $subscription_status = $subscription->get_status();
        }
        if($subscription_status == 'active')
        {
            $cart_item_data[ "subscribe_product" ] = "YES";  
        }
    }
    return $cart_item_data;
     
}
add_filter( 'woocommerce_add_cart_item_data', 'save_subscription_wrap_data', 99, 2 );

function render_meta_on_cart_and_checkout1( $cart_data, $cart_item = null ) {
   $meta_items = array();
    
    if( !empty( $cart_data ) ) {
        $meta_items = $cart_data;
    }
    if( isset( $cart_item["subscribe_product"] ) ) {
        $meta_items[] = array( "name" => "Product Type", "value" => "Package Addon" );
    }
    return $meta_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout1', 100, 2 );

function calculate_gift_wrap_fee( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        
        $additionalPrice = 100;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["subscribe_product"] ) ) {                
                if( method_exists( $value['data'], "set_price" ) ) {
                                        
                    $orgPrice = floatval( $value['data']->get_price() );
                    //$value['data']->set_price( $orgPrice + $additionalPrice );
                    $value['data']->set_price(0);
                } else {
                        
                    $orgPrice = floatval( $value['data']->price );
                    //$value['data']->price = ( $orgPrice + $additionalPrice );
                    $value['data']->price = (0);
                }           
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_gift_wrap_fee', 99 );
Spinning answered 15/6, 2022 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.