3

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();
            exit;
        //echo $prices    = get_price_sign_up_fee($product);
        exit;
}

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);

Thanks

UPDATE


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.

UPDATE 2


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 ?

Thanks

Mittul At TechnoBrave
  • 1,142
  • 3
  • 25
  • 70
  • To find an answer to your question, you will have to adjust what the `get_sign_up_fee()` function returns, this can be done via the [woocommerce_subscriptions_product_sign_up_fee](https://github.com/wp-premium/woocommerce-subscriptions/blob/master/includes/class-wc-subscriptions-product.php#L520-L529) filter hook, or instead of using `get_sign_up_fee()`, use/write your own custom function which return the desired results. – 7uc1f3r Jul 10 '21 at 08:27
  • @7uc1f3r Thanks for your comment. I have updated my answer with a hook which I have found. Can you please support. Thanks – Mittul At TechnoBrave Jul 12 '21 at 06:28
  • The hook you use in your updated question contains the `$include` part, for the string you can then use [woocommerce_subscriptions_product_price_string](https://github.com/wp-premium/woocommerce-subscriptions/blob/master/includes/class-wc-subscriptions-product.php#L399). Your hook is on line 234, this one on line 399. All the code in between shows you what happens by default in that file, and can therefore also be adjusted – 7uc1f3r Jul 12 '21 at 10:30
  • I am still not getting it what should I do from here ? Can you please help me by answering a question with some basic code .. I am pretty close .. Thanks – Mittul At TechnoBrave Jul 12 '21 at 11:15
  • I don't use that plugin, so I can't test it and I'd rather not post answers that I'm not 100% sure will work (because I haven't tested it, I can't experience any issues). That's why I respond via comments to push you in the right direction. Regards – 7uc1f3r Jul 12 '21 at 11:17

1 Answers1

1

woocommerce_variable_subscription_price_html is the correct hook that you should be using, and here's an example that might work for you:

add_filter( 'woocommerce_variable_subscription_price_html', 'variable_subscription_custom_price_html', 10, 2 );
function variable_subscription_custom_price_html( $price, $product ) {
    // Unhook to avoid infinite loop.
    remove_filter( 'woocommerce_variable_subscription_price_html', __FUNCTION__, 10, 2 );

    // List of product IDs for variations you're hiding.
    $hidden_products = array( 4171 );

    // Check if the product has any variations that you manually hid.
    $hidden = array_intersect( $hidden_products, get_children( array(
        'post_parent' => $product->get_id(),
        'post_type'   => 'product_variation',
        'fields'      => 'ids',
    ) ) );

    if ( ! empty( $hidden ) ) {
        $prices = $product->get_variation_prices( true )['price'];
        asort( $prices ); // sort from lowest to highest price

        $variation = wc_get_product( array_keys( $prices )[0] );

        // It's a variation, so we manually add the 'From: text'.
        $price = 'From: ' . $variation->get_price_html();
    }

    // Re-hook this function.
    add_filter( 'woocommerce_variable_subscription_price_html', __FUNCTION__, 10, 2 );

    return $price;
}
Sally CJ
  • 15,362
  • 2
  • 16
  • 34
  • Thanks for your reply and support. I will use the above reference code and will update you soon. Thanks – Mittul At TechnoBrave Jul 19 '21 at 07:33
  • Sure, take your time. PS: The `remove_filter()` and `add_filter()` should be in the `if` block, but they do work outside the `if`, so I'll keep them there for now. – Sally CJ Jul 19 '21 at 09:07