I have a WooCommerce subscription product with 2 purposes (one should never expire and one should expire). I want to enable automatic renewal for one product which is a membership product that never expires and disable automatic renewal for others. WooCommerce provides a Manual Renewal option on the settings page however it applies to all products.
I want to enable manual renewal only for a specific product so that I can use the automatic methods for others. How can I achieve this?
CodePudding user response:
You can try this :
add_filter( 'woocommerce_subscription_product_renewal_rule', 'customize_subscription_renewal_rule', 10, 2 );
function customize_subscription_renewal_rule( $renewal_rule, $subscription ) {
// Define the ID of the product for which you want to enable manual renewal
$product_id = 123;
if ( $subscription->get_product_id() === $product_id ) {
return 'manual';
}
return $renewal_rule;
}
This code will check if the subscription product ID is equal to the defined product ID, if it matches it will return 'manual' to enable the manual renewal and if not it will return the default renewal rule. Hope it helps :)
CodePudding user response:
Use the woocommerce_checkout_update_order_meta
hook to update the payment method for the subscription after the order is placed. You can use the update_meta_data
method of the WC_Subscription object to update the payment method for the subscription.
add_action( 'woocommerce_checkout_update_order_meta', 'update_subscription_payment_method', 10, 2 );
function update_subscription_payment_method( $order_id, $data ) {
$order = wc_get_order( $order_id );
//check if the order has a subscription
if ( wcs_order_contains_subscription( $order ) ) {
//loop through subscriptions
foreach ( wcs_get_subscriptions_for_order( $order_id ) as $subscription ) {
// check for the product_id
if ( $subscription->get_product_id() === $product_id ) {
$subscription->update_meta_data( '_payment_method', 'manual' );
$subscription->save();
}
}
}
}
Please replace the $product_id
with the actual product id for which you want to update the payment method.And yes you will need to have the WooCommerce Subscriptions plugin installed and activated for this code to work.
If you want to update the payment method to automatic based on some condition, you can change manual
to automatic
or vice versa in the function and also update the condition accordingly.
$subscription->update_meta_data( '_payment_method', 'automatic' );
CodePudding user response:
I found one Woocommerce hook when subscription payment is complete, this works fine in my scenario. I compared meta between automatic and manual subscriptions got difference in _requires_manual_renewal
add_action('woocommerce_subscription_payment_complete', function($subscription){
$order_items = $subscription->get_items();
// Loop through order items
foreach ( $order_items as $item_id => $item ) {
// To get the subscription variable product ID and simple subscription product ID
$product_id = $item->get_product_id();
if($product_id == 295 ){
update_post_meta($subscription->id,'_requires_manual_renewal',false);
}
else{
update_post_meta($subscription->id,'_requires_manual_renewal',true);
}
}
},10,2);