In WooCommerce, based on " Woocommerce get variation product price " answer code, I have been using the following to show the price beside each variation on my variable products dropdowns:
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';
}
return $term;
}
It works great on all products except one (which I use a separate plugin for as it's for a gift card).
Is it possible to exclude one specific product ID or a product category, OR for it to apply to only specific product categories? If so, what should be added to the code and where to apply that?
I also use Woo Discount Rules plugin for price discounts and that's the code included for that:
function woocs_fixed_raw_woocommerce_price_method($tmp_val, $product_data, $price){
remove_filter('woocs_fixed_raw_woocommerce_price', 'woocs_fixed_raw_woocommerce_price_method', 10, 3);
global $flycart_woo_discount_rules;
if(!empty($flycart_woo_discount_rules)){
global $product;
if(empty($product)){
$discount_price = $flycart_woo_discount_rules->pricingRules->getDiscountPriceOfProduct($product_data);
if($discount_price !== null) $tmp_val = $discount_price;
}
}
add_filter('woocs_fixed_raw_woocommerce_price', 'woocs_fixed_raw_woocommerce_price_method', 10, 3);
return $tmp_val;
}
add_filter('woocs_fixed_raw_woocommerce_price', 'woocs_fixed_raw_woocommerce_price_method', 10, 3);
Updated (for Woo Discount Rules plugin usage - see at the end)
Your code is a bit outdated since WooCommerce 3: woocommerce_price()
is replaced by wc_Price()
In the following revisited code, you will be able to exclude some product Ids:
// for woocommerce 3+
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term_name ) {
global $product, $wpdb;
if( ! is_a($product, 'WC_Product') )
return $term_name;
// Excluding product IDs
$excluded_product_ids = array(37, 53);
if( in_array( $product->get_id(), $excluded_product_ids ) )
return $term_name;
if( $product->is_type('variable') && ! empty($term_name) ) {
$term_slug = $wpdb->get_var("SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term_name'");
$term_slug = empty($term_slug) ? sanitize_title( $term_name ) : $term_slug;
$variation_id = (int) $wpdb->get_var( $wpdb->prepare("
SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products
ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '%s'
AND products.post_parent = %d
", $term_slug, $product->get_id() ) );
if( $variation_id > 0 ){
$variation = wc_get_product( $variation_id );
$term_name .= ' (' . strip_tags( wc_price( wc_get_price_to_display( $variation ) ) ) . ')';
}
}
return $term_name;
}
Code goes in functions.php file of your active child theme (or active theme) . Tested and works.
Maybe, to make it work with the Woo Discount Rules plugin, try to replace:
$term_name .= ' (' . strip_tags( wc_price( wc_get_price_to_display( $variation ) ) ) . ')';
by:
$term_name .= ' (' . strip_tags( wc_price( $variation->get_price() ) ) . ')';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.