#StackBounty: #wordpress #woocommerce #hook-woocommerce #woocommerce-subscriptions WordPress WooCommerce get_sign_up_fee based on avail…

Bounty: 100

I am using some custom logics to show or hide variations in variable products using this hook woocommerce_get_children https://stackoverflow.com/a/67804835/6829420 and its working fine as per my expectations.

However, I am having wrong ‘Sign Up Fee’ in the variable product page because its keep getting the values from one of my hidden variations. Here below is my try and what I have done so far.

add_filter( 'woocommerce_subscriptions_product_price_string', 'subscr_product_price_string', 10, 3 );
function subscr_product_price_string( $subscription_string, $product, $include ){
    if (is_admin() || (! $product->is_type( 'variable-subscription' ))) return $subscription_string;

    if ( $include['sign_up_fee'] && $product->get_sign_up_fee( $product ) > 0 ) {
         $available_variations = $product->get_available_variations();
        echo count($available_variations);
        echo $product->get_sign_up_fee();
        //echo $prices    = get_price_sign_up_fee($product);

Say for example, I have 3 variations "Blue, Yellow and Green" and I have hide "Blue" and go to variation product page.

Then If I do this,

$available_variations = $product->get_available_variations();
echo count($available_variations);

Then its showing 2 but my echo $product->get_sign_up_fee(); keep getting value from "Blue" which has the lowest value but this is wrong value.

enter image description here

Can someone guide me how can I get sign up values based on my available variations only.

I have already updated default From value using below code with woocommerce_variable_price_html hook and its working fine as its getting value based on available variations.

function updateFromDefaultValue($price, $product) {
    $prefix = sprintf('%s: ', __('From', 'iconic'));
    $min_price_regular = $product->get_variation_regular_price('min', true);
    $min_price_sale    = $product->get_variation_sale_price('min', true);
    $max_price = $product->get_variation_price('max', true);
    $min_price = $product->get_variation_price('min', true);
    $price = ($min_price_sale == $min_price_regular) ?
        wc_price($min_price_regular) :
        '<del>' . wc_price($min_price_regular) . '</del>' . '<ins>' . wc_price($min_price_sale) . '</ins>';

    return ($min_price == $max_price) ?
        $price :
        sprintf('%s%s', $prefix, $price);

add_filter('woocommerce_variable_price_html', 'updateFromDefaultValue', 10, 2);



I have found a hook woocommerce_subscriptions_product_price_string_inclusions through which I am getting an updated sign_up_fee value based on my "Available Subscription". Below is my code.

add_filter('woocommerce_subscriptions_product_price_string_inclusions', 'updateSubscriptionPeriod', 10, 2);

function updateSubscriptionPeriod( $include, $product ) {
    if (is_admin() || (! $product->is_type( 'variable-subscription' ))) return $include;

    $min_max_variation_data = $product->get_min_and_max_variation_data( array_keys( $prices ) );

    if(empty($min_max_variation_data) || empty($min_max_variation_data['subscription'])) return $include;
    $include['sign_up_fee'] = $min_max_variation_data['subscription']['signup-fee'];
    $include['trial_length'] = 5; 
    $include['subscription_period'] = 50; 
    $include['subscription_length'] = $min_max_variation_data['subscription']['interval']; 
    $include['subscription_price'] = $min_max_variation_data['max']['price'];    
    return $include;

Now I am able to get value 20, please see screenshot below.

enter image description here

But as you can see the difference in my screenshot, I am still not getting the updated values here every 3 years with a 2-week free trial a as it should be said, very 4 years with a 2-month free trial because this is what my values from my available variations.

You can also see I am trying to put some static values as well as dynamic values like this.

  $include['trial_length'] = 5; 
    $include['subscription_period'] = 50; 
    $include['subscription_length'] = $min_max_variation_data['subscription']['interval']; 
    $include['subscription_price'] = $min_max_variation_data['max']['price'];    

But its just not updating in my product page.

Can someone guide me please how can I update these due values.

Thanks in advance.


I have searched across and found woocommerce_subscriptions_product_price_string hook and here is my attempt.

add_filter( 'woocommerce_subscriptions_product_price_string', 'subscriptions_custom_price_string', 20, 3 );
function subscriptions_custom_price_string( $price_string, $product, $args ) {

    // Get the trial length to check if it's enabled
    $trial_length = $args['trial_length'];
    $speriod = $args['subscription_period'];
    $sfee = $args['sign_up_fee'];
    $sfee = wc_price($sfee);
    $sign_up_fee = isset($args['sign_up_fee']) ? __(" and a $sfee sign-up fee", "woocommerce") : '';
    if( $trial_length > 0 )
        $price_string = $args['price'] . ' / ' . $speriod . $sign_up_fee;

    return $price_string;

And its showing like this below.

enter image description here

So now basically its updating but its not correct as I lost a format and some other important values.

Can someone guide how can I properly overwrite this filter ?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.