Change Pricing Title on Variable Item
-
I would like to change the pricing to state the lowest price per item. However, since my items are bundled it shows that price. How can I change the pricing to say “As low as .80 per card” rather than the “$20-$40”? See first product image on this page.
https://itsallaboutthecards.amawebcreations.com/index.php/product-category/eagle-scouts/eagle-scout-court-of-honor-invitations/
-
Try this code in functions.php in your child theme:
<?php add_filter( 'woocommerce_variable_sale_price_html', 'custom_variable_price', 40, 2 ); add_filter( 'woocommerce_variable_price_html', 'custom_variable_price', 40, 2 ); function custom_variable_price( $price, $_product ) { $product_id = $_product->id; switch ($product_id) { case 6522: return 'From $0.80 per item'; default: return $price; } // end switch } // end function
Thank you @lorro however, I will want this to be done on various products with different price points. Won’t this code create that .80 price for all items?
No, only for product id 6522. You can add more “case”s for other products if you wish.
case 6522: case 6523: return 'From $0.80 per item'; // applies to 6522 & 6523 case 6524: return 'From £1.20 per item'; default: // anything else
@lorro I have over 500 products, this would take forever! Is there no other way?
Oh, thought there was just a few.
Is there a pattern, maybe all products in category abc are one thing?
If there’s a lot of permutations, you would need a custom field for this text on the product edit page, then rejig the function to look for the data in the custom field or use the default if the custom field is blank.
Presumably you are not putting the unit price in a field where it can be found by code.
There still has to be some logic for telling WC what text to use.
@lorro yes, there is a pattern by category. Can I replace the product id with a category id?
No, we need to get the terms for the product.
<?php add_filter( 'woocommerce_variable_sale_price_html', 'custom_variable_price', 40, 2 ); add_filter( 'woocommerce_variable_price_html', 'custom_variable_price', 40, 2 ); function custom_variable_price( $price, $_product ) { $terms = get_the_terms( $_product->id, 'product_cat' ); foreach ($terms as $term) { switch ($term->term_id) { case 20: // my category id case 21: // my category id, same text as category 20 return 'From $0.80 per item'; case 22: // some other category return 'From $1.20 per item'; default: } // end switch } // end foreach return $price; // any other category } // end function
If a product is in multiple categories, the first text found will be returned.
@lorro this worked great for changing my product category prices. However, I do have a few product items I want to change individually within the category. Is this possible if I already have changed the entire category?
Yes, try:
<?php add_filter( 'woocommerce_variable_sale_price_html', 'custom_variable_price', 40, 2 ); add_filter( 'woocommerce_variable_price_html', 'custom_variable_price', 40, 2 ); function custom_variable_price( $price, $_product ) { $terms = get_the_terms( $_product->id, 'product_cat' ); foreach ($terms as $term) { switch ($term->term_id) { case 20: // my category id case 21: // my category id, same text as category 20 $price = 'From $0.80 per item'; break; case 22: // some other category $price = 'From $1.20 per item'; break; default: } // end switch } // end foreach // now do the specials $product_id = $_product->id; switch ($product_id) { case 6522: case 6523: $price = 'From $0.80 per item'; // applies to 6522 & 6523 case 6524: $price = 'From £1.20 per item'; default: // anything else } // end switch return $price; } // end function
Sorry, not tested.
When you’ve customised it, ensure the code validates here:
https://phpcodechecker.com/Thank you @lorro this did not quite work. No price is showing now. Any ideas?
@lorro actually is just does not change the price, price is there but the same as the rest of the category
@lorro did you se my above response? any other suggestions?
Can’t test it, but it looks like I forgot the breaks in the second switch, so:
<?php add_filter( 'woocommerce_variable_sale_price_html', 'custom_variable_price', 40, 2 ); add_filter( 'woocommerce_variable_price_html', 'custom_variable_price', 40, 2 ); function custom_variable_price( $price, $_product ) { $terms = get_the_terms( $_product->id, 'product_cat' ); foreach ($terms as $term) { switch ($term->term_id) { case 20: // my category id case 21: // my category id, same text as category 20 $price = 'From $0.80 per item'; break; case 22: // some other category $price = 'From $1.20 per item'; break; default: } // end switch } // end foreach // now do the specials $product_id = $_product->id; switch ($product_id) { case 6522: case 6523: $price = 'From $0.80 per item'; // applies to 6522 & 6523 break; // forgot these break; lines in my earlier post case 6524: $price = 'From £1.20 per item'; break; default: // anything else } // end switch return $price; } // end function
If that doesn’t get it going, please post the actual code you have used at pastbin.com (free service) and post the pastebin.com link here.
Thank. Here is the actual code I am using. Where should I put the breaks to make the last price change to $.25 work??
add_filter( ‘woocommerce_variable_sale_price_html’, ‘custom_variable_price’, 40, 2 );
add_filter( ‘woocommerce_variable_price_html’, ‘custom_variable_price’, 40, 2 );
function custom_variable_price( $price, $_product ) {
$terms = get_the_terms( $_product->id, ‘product_cat’ );
foreach ($terms as $term) {
switch ($term->term_id) {
case 134: // some other category
return ‘$0.80-$2.00’;
case 132: // some other category
return ‘$1.50-$2.00’;
case 138: // some other category
return ‘$1.25’;
case 142: // some other category
return ‘$0.30’;
case 140: // some other category
return ‘$5.00’;
case 141: // some other category
return ‘$1.00-$1.50’;
case 139: // some other category
return ‘$1.00’;
case 143: // some other category
return ‘$3.00’;
case 150: // some other category
return ‘$0.80-$2.00’;
case 152: // some other category
return ‘$0.80-$2.00’;
case 153: // some other category
return ‘$0.80-$2.00’;
case 156: // some other category
return ‘$0.80-$2.00’;
case 159: // some other category
return ‘$1.00’;
case 160: // some other category
return ‘$1.50-$2.00’;
case 154: // some other category
return ‘$0.80-$2.00’;
case 157: // some other category
return ‘$5.00’;
case 158: // some other category
return ‘$3.00’;
case 163: // some other category
return ‘$0.80-$2.00’;
default:
} // end switch
} // end foreach
// now do the specials
$product_id = $_product->id;
switch ($product_id) {case 6952:
$price = ‘$0.25’; // applies to 6952default: // anything else
} // end switchreturn $price; // any other category
} // end function
Is product 6952 categorised with any of the categories higher up in the function? If so, the function has returned before the program flow reaches the specials section. You’ll need to replace the returns with
$price =
with abreak;
after: See:
https://pastebin.com/LTN17ijK
- The topic ‘Change Pricing Title on Variable Item’ is closed to new replies.