I have used the following code to change price from functions.php. It was working for a single product even in cart page also. How can i use the code for different products with different pricing? Is that possible?
'team' is a taxonomy and 'A-team', 'B-team' are the terms. I am assigning products to 'A-team' and 'B-team'.
For example :
For 'A-Team' : product_id 14 = $200, 15 = $600, 17 = $800;
For 'B-Team' : product_id 14 = $100, 15 = $300, 17 = $400;
Original Price : product_id 14 = $55, 15 = $44, 17 = $88;
In taxonomy archive page :
If 'A-Team' archive products should show
[product_id 14 = $200, 15 = $600, 17 = $800;]
If 'B-Team' archive products should show
[product_id 14 = $100, 15 = $300, 17 = $400;]
My Code :
function change_price($price, $productd){
if($productd->id == 16):
return 160;
else:
return $price;
endif;
add_filter('woocommerce_get_price','change_price', 10, 2);
add_filter('woocommerce_get_regular_price','change_price', 10, 2);
add_filter('woocommerce_get_sale_price','change_price', 10, 2);
Thanks in advance.
I have changed my code as follows. It's working for me. Hope this may helps for some other who have some problem.
`add_filter( 'woocommerce_get_price', function ($price, $productd ) {
if($team_id) :
return get_post_meta($productd->id, 'tax_'.$team_id , true);
else:
return $price;
endif;
}, 10, 2 );`
No problems even at 'Add-to-cart' functionality also.
Thanks,
Satya
Thank you Satya, and helgatheviking. I had created a custom field for users in which they set their currency (USD or EURO). And I had created two custom fields for every product: "_price_usd" "_price_euro", each containing the product's US dollar price and the European price in EUROs. Using an anonymous function solved a couple of problems. Now I just have to input the right suffix or prefix depending on the user's currency. Here is my adapted version of your code:
add_filter( 'woocommerce_get_price', function ($price, $productd ) {
$logged_in_user_id = get_current_user_id();
$user_currency = get_user_meta( $logged_in_user_id, 'usd_euro', true );
$new_price = strtolower("_price_".$user_currency);
return get_post_meta($productd->id, $new_price, true);
}, 10, 2 );