updates - woocommerce_before_calculate_totals in woocommerce 3.0

admin2025-01-07  3

I recently made the jump from woocommerce 2.7 to 3.1 I'm having issues with the woocommerce_before_calculate_totals hook on the below function.

function calculate_embossing_fee( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        /* Gift wrap price */
        $additionalPrice = 5;
        foreach ( $cart_object->cart_contents as $key => $value ) {
            if( isset( $value["embossing_fee"] ) ) {
                $orgPrice = floatval( $value['data']->price );
                                $discPrice = $orgPrice + $additionalPrice;
                $value['data']->set_price($discPrice);
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_embossing_fee', 99 );

Do anyone know why this would be throwing up an error and how I can resolve it?

The error I get is: Sure:

Notice: price was called incorrectly. Product properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/bdop/page.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, call_user_func_array, do_shortcode, preg_replace_callback, do_shortcode_tag, call_user_func, WC_Shortcodes::cart, WC_Shortcodes::shortcode_wrapper, call_user_func, WC_Shortcode_Cart::output, WC_Cart->calculate_totals, do_action('woocommerce_before_calculate_totals'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, calculate_embossing_fee, WC_Abstract_Legacy_Product->__get, wc_doing_it_wrong Please see Debugging in WordPress for more information. (This message was added in version 3.0.) in /Applications/MAMP/htdocs/bdop/wp-includes/functions.php on line 4139

I recently made the jump from woocommerce 2.7 to 3.1 I'm having issues with the woocommerce_before_calculate_totals hook on the below function.

function calculate_embossing_fee( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        /* Gift wrap price */
        $additionalPrice = 5;
        foreach ( $cart_object->cart_contents as $key => $value ) {
            if( isset( $value["embossing_fee"] ) ) {
                $orgPrice = floatval( $value['data']->price );
                                $discPrice = $orgPrice + $additionalPrice;
                $value['data']->set_price($discPrice);
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_embossing_fee', 99 );

Do anyone know why this would be throwing up an error and how I can resolve it?

The error I get is: Sure:

Notice: price was called incorrectly. Product properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/bdop/page.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, call_user_func_array, do_shortcode, preg_replace_callback, do_shortcode_tag, call_user_func, WC_Shortcodes::cart, WC_Shortcodes::shortcode_wrapper, call_user_func, WC_Shortcode_Cart::output, WC_Cart->calculate_totals, do_action('woocommerce_before_calculate_totals'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, calculate_embossing_fee, WC_Abstract_Legacy_Product->__get, wc_doing_it_wrong Please see Debugging in WordPress for more information. (This message was added in version 3.0.) in /Applications/MAMP/htdocs/bdop/wp-includes/functions.php on line 4139
Share Improve this question edited Jul 4, 2017 at 12:18 CodeMascot 4,5372 gold badges15 silver badges25 bronze badges asked Jul 4, 2017 at 9:46 KKBSE2016KKBSE2016 1131 gold badge2 silver badges5 bronze badges 2
  • Please share what error it is throwing. – CodeMascot Commented Jul 4, 2017 at 11:22
  • Sure - added above as too long for a commment. – KKBSE2016 Commented Jul 4, 2017 at 12:11
Add a comment  | 

2 Answers 2

Reset to default 6

Well, the problem is you are calling price directly at $value['data']->price. Make it $value['data']->get_price() and I think you problem will be fixed. So the whole code block will be-

function calculate_embossing_fee( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        /* Gift wrap price */
        $additionalPrice = 5;
        foreach ( $cart_object->cart_contents as $key => $value ) {
            if( isset( $value["embossing_fee"] ) ) {
                // Turn $value['data']->price in to $value['data']->get_price()
                $orgPrice = floatval( $value['data']->get_price() );
                $discPrice = $orgPrice + $additionalPrice;
                $value['data']->set_price($discPrice);
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_embossing_fee', 99 );

Hope that helps.

function action_woocommerce_before_calculate_totals( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    // Get payment method
    $chosen_payment_method = WC()->session->get( 'chosen_payment_method' );
    $chosen_default_deposit = WC()->session->get('deposit_enabled');
    // Compare
    if(empty($chosen_default_deposit))
    {
            if ( $chosen_payment_method == 'bacs' ) {
                

                // Loop through cart items
                foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {     
                    // Get price
                    //echo "<pre>";
                    //print_r($cart_item);
                    //$price = 7000;
                    $custom_bank_transfer_price_text_field = get_post_meta($cart_item['product_id'], '_custom_bank_transfer_price_text_field', true);
                    if(!empty($custom_bank_transfer_price_text_field))
                    {
                        $cart_item['data']->set_price( $custom_bank_transfer_price_text_field );
                    }
                    
                }
            
            }
    }
}

add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257222a421.html

最新回复(0)