WooCommerce Change Pricing Based on Custom Field
Here's a little snippet which may be useful if you need to adjust pricing in WooCommerce based on a custom field. In this instance, my client wanted to increase the price of items within a specific category of products for users who were either not signed into the website, or who were not being offered wholesale pricing.
I added this function to the theme's functions.php file. It's called every time WooCommerce generates a product price. I'm sure it can be tidier - let me know on Twitter if you found this helpful or can suggest improvements!
function return_custom_price($price, $product) { // set default multiplier $multiplier = 1; global $post, $blog_id; $product = wc_get_product($post_id); $post_id = $post->ID; // get product categories $catrgories = get_the_terms( $post->ID, 'product_cat' ); // loop through categories foreach ($categories as $category) { $category_id = $tcategory->term_id; // if category is a match if($category_id==[multiplier_category_id]){ // get user's ID $user = get_current_user_id(); if($user){ // get custom field value as $level $level = get_field('[custom_field_name]', "user_".$user); }else{ $level = '[default_level]'; // change multiplier - in this case adding 10% to the price $multiplier = 1.1; } } } // calculate price based on multiplier $price = ($price*$multiplier); // return price return $price; } add_filter('woocommerce_get_price', 'return_custom_price', 10, 2); }