wordpress 在WooCommerce中根据客户计费国家更改产品价格

zwghvu4y  于 9个月前  发布在  WordPress
关注(0)|答案(1)|浏览(188)

我需要根据计费国家更改货币,使用以下代码可以以某种方式进行更改,但它只适用于简单产品,而不是可变产品。

add_filter('woocommerce_get_price', 'return_custom_price', $product, 2);

function return_custom_price($price, $product) {    
    global $post, $woocommerce;
    
    // Array containing country codes
    $county = array('US');
    // Amount to increase by
    //$amount = 5;
    // If the custromers shipping country is in the array
    if ( in_array( $woocommerce->customer->get_billing_country(), $county ) && is_checkout() ){
        // Return the price plus the $amount
       return $new_price = ($price/260)*1.25;
    } else {
         
        // Otherwise just return the normal price
        return $price;
    }
}

字符串

yb3bgrhw

yb3bgrhw1#

钩子woocommerce_get_price自WooCommerce 3以来已过时和弃用,并已被以下钩子取代:

  • woocommerce_product_get_price(用于产品)
  • woocommerce_product_variation_get_price(用于可变产品的变体)。

您的代码中还有一些其他错误。请尝试以下修改后的代码:

add_filter('woocommerce_product_get_price', 'country_based_cart_item_price', 100, 2);
add_filter('woocommerce_product_variation_get_price', 'country_based_cart_item_price', 100, 2);

function country_based_cart_item_price( $price, $product ) {    
    // Define below in the array the desired country codes
    $targeted_countries = array('US');
    $billing_country    = WC()->customer->get_billing_country();

    // Only on cart and checkout pages 
    if ( ( is_checkout() || is_cart() ) && in_array($billing_country, $targeted_countries) ){
        // Returns changed price
       return $price / 260 * 1.25;
    }
    return $price;
}

字符串
代码放在你的子主题的functions.php文件中(或插件中)。测试和工作。
查看:通过WooCommerce 3+中的挂钩更改产品价格

相关问题