'Woocommerce subscriptions: Assign different subscrption_id to each item in cart
I am using Woocommerce with the Woocommerce Subscriptions plugin and I am trying to assign different subscription_id to each item in the cart. So far I have managed with the following functions to split the cart items when quantity > 1, as you can see from the images I attached but they go all under the same subscription id. Any suggestions would be appreciated.
function1:
function split_products( $cart_item_data, $product_id ){
$unique_cart_item_key = uniqid();
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'split_products', 10, 2 );
function2:
add_action( 'woocommerce_add_to_cart', 'split_products_to_separate_cart_items', 10, 6 );
function split_products_to_separate_cart_items( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
if ( $quantity > 1 ) {
WC()->cart->set_quantity( $cart_item_key, 1 );
for ( $i = 1; $i <= $quantity -1; $i++ ) {
$cart_item_data['unique_key'] = md5( microtime() . rand() . "Hi Mom!" );
WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation, $cart_item_data );
}
}
}
With these two functions above I manage to separate the cart items as you can see from the image below:
But unfortunately they get interpreted as one subscription with common subscription id (2544) as you can see from the following image:
Solution 1:[1]
You need to also modify recurring cart key for each item by using the following filter
add_filter( 'woocommerce_subscriptions_recurring_cart_key' );
Which is found in Class class-wc-subscriptions-cart
in line 1088
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nour Kilany |