I am interested in showing my users how much %
they save when purchasing a particular variation product.
For example if I have a variable subscription, with two variations.
- £24 for 1 Week
- £30 for 2 Week
Then save percentage = ( ((24 * 2) - 30) / 30 ) * 100 = 60%
I want to show Subscribe & save 60%
when a user has selected product 2.
My progress
I know how to show this text on variation but I have problem fetching this percentage value.
Find my code below which is trying to get discount.
function add_data_point_for_savings($available_variations)
{
if (is_array($available_variations)) {
$product = wc_get_product($available_variations['variation_id']);
$period = \WC_Subscriptions_Product::get_length($product);
$parent_id = $product->get_parent_id();
$parent_product = wc_get_product($parent_id);
$minimum_price = $parent_product->get_variation_price('min', true);
if (isset($available_variations['variation_id'])) {
$available_variations['underline_text'] = 'Subscribe & Save 00%';
}
}
return $available_variations;
}
add_action('woocommerce_available_variation', 'add_data_point_for_savings', 5);
As you can see I am adding this as a datapoint in available_variation
.
I am stuck at getting day value for every variation. If I get day count for every variation I will be able to find the save percentage.
I am aware of these two functions.
- WC_Subscriptions_Product::get_length Gives length eg.
6
- WC_Subscriptions_Product::get_period Gives period eg.
week
,month
The issue with these functions is that it returns 0 when I try to get length.
To sum it all up.
I want to find the day count for a subscription product variation, given I have its id.