我有这个功能,我需要让它在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提前