WooCommerce改变了某个国家的价格

时间:2015-06-26 作者:Marc Ster

我想更改价格(例如瑞士CH),并在所有价格中添加金额。。

我有这个功能。php,但它不工作。。。

    add_action( \'woocommerce_change_price\' );
function woocommerce_change_price() {
global $woocommerce;

$county     = array(\'CH\');
$amount     = 5;

if ( in_array( $woocommerce->customer->get_shipping_country(), $county ){
    woocommerce_get_price += $amount;
    woocommerce_get_regular_price; += $amount;
    woocommerce_get_sale_price += $amount;
    woocommerce_order_amount_item_subtotal += $amount;
    };

}

1 个回复
最合适的回答,由SO网友:Und3rTow 整理而成

您需要使用woocommerce_get_price 过滤器:

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(\'CH\');
    // Amount to increase by
    $amount = 5;
    // If the custromers shipping country is in the array
    if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ){
        // Return the price plus the $amount
       return $new_price = $price + $amount;
    } else {
        // Otherwise just return the normal price
        return $price;
    }
} 
<小时>

EDIT:

要仅对一个产品执行此操作,您只需通过$post:

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(\'CH\');
    // Get the post id 
    $post_id = $post->ID;
    // Amount to increase by
    $amount = 5;
    // If the customers shipping country is in the array and the post id matches
    if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) && ( $post_id == \'12\' || $post_id == \'5\' || $post_id == \'6\' ) ){
        // Return the price plus the $amount
       return $new_price = $price + $amount;
    } else {
        // Otherwise just return the normal price
        return $price;
    }
} 
只需将“12”替换为所需的产品ID。(“5”和“6”分别是结帐页)

结束

相关推荐

允许登录用户执行外部PHP脚本

我需要在WP外部调用一个PHP脚本,我想利用WP的凭据,以便只有登录的用户才能访问此脚本。我曾尝试在外部PHP脚本中嵌入此代码,但无法使用(其中XXX是域名): require(\'/var/www/vhosts/xxx.xxx.xxx/httpdocs/wordpress/wp-blog-header.php\'); if ( !is_user_logged_in () ) { //the script exits if the user is not logged