我正在使用 woocommerce 订阅功能,我需要为购物车中的每个项目创建单独的订阅记录。
当前行为:它正在为_subscription_length具有相同值的多个项目创建单个订阅记录(基本上按该字段对项目进行分组)
我想每次为每个项目创建单独的订阅。我尝试了很多钩子但没有运气。
我尝试了以下代码,但它正在创建单独的订阅记录,但与此同时,它还创建组合的订阅记录,并且在单独的记录中,订单数据和 ID 没有获取链接。
add_action('woocommerce_checkout_order_processed', 'create_individual_subscriptions', 20, 2);
function create_individual_subscriptions($order_id, $posted_data) {
$order = wc_get_order($order_id);
$items = $order->get_items();
foreach ($items as $item_id => $item) {
$product = $item->get_product();
// Check if the product is a subscription type
if ($product && ($product->is_type('subscription') || $product->is_type('variable-subscription') || $product->is_type('subscription_variation'))) {
$access_from = get_post_meta($product->get_id(), 'access_from', true);
$access_end = get_post_meta($product->get_id(), 'access_end', true);
if (!$access_from || !$access_end) {
continue; // Skip if dates are not properly set
}
$start_date = new DateTime($access_from);
$end_date = new DateTime($access_end);
// Create a new subscription
$new_subscription = wcs_create_subscription(array(
'start_date' => $start_date->format('Y-m-d H:i:s'),
'customer_id' => $order->get_customer_id(),
'billing_period' => $product->get_meta('_subscription_period'),
'billing_interval' => $product->get_meta('_subscription_period_interval'),
'parent_id' => $order_id,
));
// Add product to the subscription
$new_subscription->add_product($product, $item->get_quantity());
// Set the specific dates for the subscription
$new_subscription->update_meta_data('_schedule_start', $start_date->format('Y-m-d H:i:s'));
$new_subscription->update_meta_data('_schedule_trial_end', $start_date->format('Y-m-d H:i:s'));
$new_subscription->update_meta_data('_schedule_next_payment', $start_date->format('Y-m-d H:i:s'));
$new_subscription->update_meta_data('_schedule_end', $end_date->format('Y-m-d H:i:s'));
$new_subscription->calculate_totals();
$new_subscription->save();
// Optionally handle payment and status setup here
// e.g., $new_subscription->payment_complete();
// e.g., $new_subscription->update_status('active');
}
}
}
此代码将为购物车中的每个商品创建单独的订阅记录,并删除原始订单商品,确保不会创建合并的订阅记录。
add_action('woocommerce_checkout_order_processed', 'create_individual_subscriptions', 20, 2);
function create_individual_subscriptions($order_id, $posted_data) {
$order = wc_get_order($order_id);
$items = $order->get_items();
foreach ($items as $item_id => $item) {
$product = $item->get_product();
// Check if the product is a subscription type
if ($product && ($product->is_type('subscription') || $product->is_type('variable-subscription') || $product->is_type('subscription_variation'))) {
$access_from = get_post_meta($product->get_id(), 'access_from', true);
$access_end = get_post_meta($product->get_id(), 'access_end', true);
if (!$access_from || !$access_end) {
continue; // Skip if dates are not properly set
}
$start_date = new DateTime($access_from);
$end_date = new DateTime($access_end);
// Create a new subscription for each item
$new_subscription = wcs_create_subscription(array(
'start_date' => $start_date->format('Y-m-d H:i:s'),
'customer_id' => $order->get_customer_id(),
'billing_period' => $product->get_meta('_subscription_period'),
'billing_interval' => $product->get_meta('_subscription_period_interval'),
'parent_id' => $order_id,
));
// Add product to the subscription
$new_subscription->add_product($product, $item->get_quantity());
// Set the specific dates for the subscription
$new_subscription->update_meta_data('_schedule_start', $start_date->format('Y-m-d H:i:s'));
$new_subscription->update_meta_data('_schedule_trial_end', $start_date->format('Y-m-d H:i:s'));
$new_subscription->update_meta_data('_schedule_next_payment', $start_date->format('Y-m-d H:i:s'));
$new_subscription->update_meta_data('_schedule_end', $end_date->format('Y-m-d H:i:s'));
$new_subscription->calculate_totals();
$new_subscription->save();
// Remove the item from the original order
$order->remove_item($item_id);
}
}
// Recalculate order totals after removing items
$order->calculate_totals();
$order->save();
}