Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the coder-elementor domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rocket domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114
How to change YITH subscription reccuring price on WooCommerce cart - Code Stap

How to change YITH subscription reccuring price on WooCommerce cart

  • Home
  • Questions
  • How to change YITH subscription reccuring price on WooCommerce cart

How to change YITH subscription reccuring price on WooCommerce cart

Expert [addtoany]

Sep '24

To change the recurring price of a YITH Subscription on the WooCommerce cart using code, you'll need to tap into WooCommerce and YITH Subscription hooks and filters. Here’s a detailed guide on how to achieve this:

Steps to Change the Recurring Price in WooCommerce Cart

  1. Understand the Structure:

    • YITH WooCommerce Subscription plugin creates subscription products with recurring prices.
    • You need to target the recurring price in the cart and adjust it as necessary.
  2. Add Custom Code to Your Theme:

    • You should add custom code to your theme's functions.php file or create a custom plugin to avoid losing changes when updating the theme.
  3. Use Filters to Modify the Recurring Price:

    • Use hooks provided by WooCommerce and YITH to modify the recurring price.

Code Example

Here’s a code example to modify the recurring price for YITH Subscription products in the WooCommerce cart:

Example


add_action('woocommerce_before_calculate_totals', 'change_recurring_price_in_cart', 20, 1);

function change_recurring_price_in_cart($cart) {
    // Check if cart is empty
    if (is_admin() && !defined('DOING_AJAX')) return;

    // Loop through cart items
    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        // Check if item is a subscription product
        if (isset($cart_item['data']) && $cart_item['data'] instanceof WC_Product_Subscription) {
            $product = $cart_item['data'];

            // Get the current subscription price
            $subscription_price = $product->get_price();

            // Calculate the new recurring price
            $new_recurring_price = $subscription_price * 1.2; // For example, increase by 20%

            // Set the new price for the cart item
            $cart_item['data']->set_price($new_recurring_price);
        }
    }
}

Explanation of the Code

  1. Hook into WooCommerce Cart Calculation:

    • woocommerce_before_calculate_totals: This hook runs before the cart totals are calculated. It's a suitable place to modify item prices before they are displayed or processed.
  2. Check if Cart is Empty:

    • Ensures that the code doesn’t run in the admin area or during AJAX requests.
  3. Loop Through Cart Items:

    • Loop through each item in the cart to check if it’s a subscription product.
  4. Check if Product is a Subscription:

    • Use instanceof WC_Product_Subscription to verify that the product is a subscription.
  5. Modify the Recurring Price:

    • Retrieve the current subscription price and apply your custom logic to calculate the new price.
    • Use $cart_item['data']->set_price($new_recurring_price); to set the new price.

Additional Considerations

  • Testing: Make sure to test the code thoroughly in a staging environment before deploying it to production.
  • Backup: Always back up your site before making changes to the code.
  • Custom Plugin: For easier management and future updates, consider placing this code into a custom plugin rather than adding it to the theme’s functions.php.

This example shows a simple way to modify recurring prices, but you can adapt the logic to meet more complex requirements or pricing rules as needed.

Related Questions & Topics