woocommerce_before_calculate_totals in woocommerce v3。*

时间:2017-05-17 17:23:28

标签: wordpress woocommerce

我有这个功能,我需要让它在woocommerce 3 *中工作。它在2.6。*

中完美运行
    add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {

  global $woocommerce;

  $cart_item_meta['estimated_fare'] = WC()->session->get( 'estimated_fare' );


    $custom_price = $cart_item_meta['estimated_fare'] ; // This will be your custome price  
    $target_product_id = get_option('stern_taxi_fare_product_id_wc');
    foreach ( $cart_object->cart_contents as $key => $value ) {
        if ( $value['product_id'] == $target_product_id ) {
            $value['data']->price = $custom_price;
        }
    }
}

我试过这个:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10 , 2);

function add_custom_price( $cart_obj ) {

  global $woocommerce;
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart_item_meta['estimated_fare'] = WC()->session->get( 'estimated_fare' );


    $custom_price = $cart_item_meta['estimated_fare'] ; // This will be your custome price  
    $target_product_id = get_option('stern_taxi_fare_product_id_wc');
    foreach ( $cart_obj->get_cart as $key => $value ) {
        if ( $value['product_id'] == 20) { // $target_product_id ) {
            $value['data']->set_price = $custom_price;
        }

    }
}

但它仍然不起作用,我在购物车中得到0.00的价格。 任何想法如何解决它?

Thnx提前

1 个答案:

答案 0 :(得分:1)

在woocommerce最新版本(3.0.1)中,您需要使用set_price()功能。因此,在您的情况下,您应该将此行$value['data']->price=$custom_price;更改为$value['data']->set_price( $custom_price );

你试错了 - > Wrong