0

I want to change the prices (e.g for switzerland CH) and add an amount to all the prices..

I have this for my functions.php, but it does not work...

    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;
    };

}
3

You need to use the woocommerce_get_price filter:

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:

To do this for just one product you can simply run a check on the product id via $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;
    }
} 

Simply replace the '12' with your desired product ID. ('5' and '6' are the checkout pages respectively)

| improve this answer | |
  • How would I do this for a single product ID? – Marc Ster Jun 26 '15 at 13:05
  • Have edited the answer to include a check on the product id – Und3rTow Jun 26 '15 at 13:39
  • One more problem.. it works for every site but not the cart and not the checkout! Any idea? – Marc Ster Jun 26 '15 at 19:24
  • see edited snippet - because the product id is the also the post id, you will need to include a check on the cart and checkout page id as well – Und3rTow Jun 26 '15 at 20:28
  • hmm it did not change :(.. isn't the '5' and '6' just checking for 2 other ids? Did not change something in the checkout pages – Marc Ster Jun 28 '15 at 10:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.