wordpresswoocommerce

How to change / for x month text on a WooCommerce variable subscription product


Similar to This but not entirely.

I have a variable subscription product that can be paid over 12 monthly instalments.

if "Number of Instalments" is "Pay in Full" Id like to be able to change the £xxx for 1 month text: Pay in Full

And then keep the next 11 installments as for 2 month / 3 month etc.

I know that this code will only show the prices but I cant seem to modify it to only alter the 1 month (Pay in Full option):

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

function remove_subscription_inclusions( $include, $product ) {
    $include['subscription_length'] = '';
    $include['subscription_period'] = '';
    return $include;
}

I did try this but it did not have any effect:

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

function custom_subscription_price_string_inclusions( $include, $product ) {
    // Check if the product is a subscription product
    if ($product->is_type('subscription')) {
        // Get the variations of the subscription product
        $variations = $product->get_available_variations();

        // Loop through each variation to check for the 1-month payment option
        foreach ($variations as $variation) {
            // Get the variation's price and subscription length/period
            $regular_price = $variation['display_price'];
            $subscription_length = isset($variation['attributes']['pa_subscription_length']) ? $variation['attributes']['pa_subscription_length'] : '';
            $subscription_period = isset($variation['attributes']['pa_subscription_period']) ? $variation['attributes']['pa_subscription_period'] : '';

            // Check if this variation is the 1 month (single payment) option
            if ('1' === $subscription_length && 'month' === $subscription_period) {
                // Modify the price string for the 1 month single payment option
                $include['price_string'] = '£' . $regular_price . ' (Full payment for 1 month)';
                // Remove subscription length and period to avoid duplication
                $include['subscription_length'] = '';
                $include['subscription_period'] = '';
            }
        }
    }

    return $include;
}


Solution

  • This is how I got it working: I used javascript/jQuery as it works regardless of WooCommerce internal class differences.

    add_action('wp_footer', 'custom_replace_subscription_price_label');
    
    function custom_replace_subscription_price_label() {
        // Only run this script on single product pages
        if (!is_product()) return;
    
        // Output JavaScript to modify price display dynamically
        ?>
        <script type="text/javascript">
            jQuery(document).ready(function($) {
                // Hook into variation change event on product page
                $('form.variations_form').on('show_variation', function(event, variation) {
                    
                    // Find the price container
                    var $priceEl = $('.woocommerce-variation-price .price');
    
                    // If variation or price data is missing, stop here
                    if (!variation || !variation.price_html) return;
    
                    // Grab the HTML of the price string
                    const rawHTML = variation.price_html;
    
                    // If the price string contains "for 1 month", replace it
                    if (rawHTML.includes('for 1 month')) {
                        const updatedHTML = rawHTML.replace(/for 1 month/, 'Pay in Full');
                        
                        // Update the HTML in the DOM with the new version
                        $priceEl.html(updatedHTML);
                    }
                });
            });
        </script>
        <?php
    }
    
    //FOR CART & CHECKOUT
    
    // Modify the price column in the cart
    add_filter('woocommerce_cart_item_price', 'custom_replace_subscription_price_cart', 10, 3);
    
    // Modify the subtotal column in the cart
    add_filter('woocommerce_cart_item_subtotal', 'custom_replace_subscription_price_cart', 10, 3);
    
    function custom_replace_subscription_price_cart($price_html, $cart_item, $cart_item_key) {
        // Check if the price string contains "for 1 month"
        if (strpos($price_html, 'for 1 month') !== false) {
            // Replace it with "Pay in Full"
            $price_html = str_replace('for 1 month', 'Pay in Full', $price_html);
        }
    
        // Return the modified (or unmodified) price string
        return $price_html;
    }