使用woocommerce_product_get_price钩子查看价格问题

时间:2022-10-25 11:20:18

I need to double the price for every product in Woocommerce frontend. For this I used the following code:

我需要为Woocommerce前端的每个产品加倍价格。为此我使用了以下代码:

add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
    return $price*2;
}

But there is an error using this code. checkout page price is not correct. for example the product orginal price is 10. We double the price by this code . so the product price is 20 now. When i added this product to the cart then cart and checkout page price is 40. That means this multiplication is take place in two times.

但是使用此代码时出错。结帐页面价格不正确。例如,产品原始价格为10.我们通过此代码将价格翻倍。所以现在的产品价格是20。当我将此产品添加到购物车时,购物车和结帐页面价格为40.这意味着此乘法将发生两次。

Please help to solve this.

请帮忙解决这个问题。

2 个解决方案

#1


2  

Updated To double the price:

更新要加倍价格:

1) First you will restrict your code to single product and archives pages only:

1)首先,您将代码限制为单个产品和归档页面:

add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
    if( is_shop() || is_product_category() || is_product_tag() || is_product() )
        return $price*2;

    return $price;
}

2) Then for cart and checkout pages, you will change the cart item price this way:

2)然后对于购物车和结账页面,您将以这种方式更改购物车商品价格:

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

    // Set and register the new calculated price
    $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;

    // Get the new calculated price and update cart session item price
    $session_data['data']->set_price( $session_data['new_price'] );

    return $session_data;
}

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

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

Tested and working. It will change all prices as you expect in cart, checkout and order pages…

经过测试和工作。它会在购物车,结账和订单页面中按预期更改所有价格......

#2


0  

Is there a way too pass a variable across in to the function and return venerable price instead?

是否有一种方法可以将变量传递给函数并返回合理的价格?

add_filter( 'woocommerce_product_get_price', 'run_shortcodes_on_prices', 10, 3 );

function run_shortcodes_on_prices( $price , $string, $product){
    if( is_shop() || is_product_category() || is_product_tag() || is_product() )

    return $string;
}

#1


2  

Updated To double the price:

更新要加倍价格:

1) First you will restrict your code to single product and archives pages only:

1)首先,您将代码限制为单个产品和归档页面:

add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
    if( is_shop() || is_product_category() || is_product_tag() || is_product() )
        return $price*2;

    return $price;
}

2) Then for cart and checkout pages, you will change the cart item price this way:

2)然后对于购物车和结账页面,您将以这种方式更改购物车商品价格:

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

    // Set and register the new calculated price
    $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;

    // Get the new calculated price and update cart session item price
    $session_data['data']->set_price( $session_data['new_price'] );

    return $session_data;
}

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

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

Tested and working. It will change all prices as you expect in cart, checkout and order pages…

经过测试和工作。它会在购物车,结账和订单页面中按预期更改所有价格......

#2


0  

Is there a way too pass a variable across in to the function and return venerable price instead?

是否有一种方法可以将变量传递给函数并返回合理的价格?

add_filter( 'woocommerce_product_get_price', 'run_shortcodes_on_prices', 10, 3 );

function run_shortcodes_on_prices( $price , $string, $product){
    if( is_shop() || is_product_category() || is_product_tag() || is_product() )

    return $string;
}