On my WooCommerce website I have fabric set to be sold per half meter, with the code below which is all working fine:
function conditional_price_suffix( $price, $product ) {
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
$product_categories = array('half-metre');
if( has_product_categories( $product_categories, $product_id ) )
$price .= ' ' . __('per ½ metre');
return $price;
}
However, how do I manipulate the code to add another option of per "quarter-metre" and output "per ¼ meter" as the suffix.
Any help?
My Answer:
>get_id();
$category_ids = $product->get_category_ids();
if ( in_array( 'fabric', $category_ids ) ) {
$price .= ' per half meter';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 10, 2 );
This code snippet adds a price suffix "per half meter" to the product price if the product belongs to the "fabric" category. You can modify the category name and the price suffix as needed for your specific requirements. Just add this code to your theme's functions.php file or a custom plugin.
Rate this post
4 of 5 based on 1396 votesComments