如何将Woocommerce订阅产品同步至Infusionsoft?求方案与插件推荐
Hey there! Let's tackle your WooCommerce + Infusionsoft subscription sync issue. I've got two main approaches for you: using a pre-built plugin (the easiest route) or extending your existing custom code to handle subscriptions.
Plugin-Based Solution
If you want to avoid diving deeper into code, there are solid WooCommerce plugins that natively support syncing subscriptions to Infusionsoft:
- WooCommerce Infusionsoft Integration: This plugin works seamlessly with WooCommerce Subscriptions, automatically mapping your WooCommerce subscription products to Infusionsoft's subscription plans. You can configure sync rules directly in the plugin settings—no need to write extra code for basic sync needs.
- Infusionsoft by Keap for WooCommerce: Built specifically for Keap (formerly Infusionsoft), this plugin includes support for subscription sync, customer data sync, and order tracking. It handles the heavy lifting of API calls behind the scenes.
Both plugins will detect when a customer purchases a subscription product and create a corresponding subscription in Infusionsoft, including details like billing cycle, price, and customer association.
Custom Code Approach
Since you already have code syncing regular orders, you can extend it to handle subscription products. Here's how to adjust your existing code:
First, make sure you have the WooCommerce Subscriptions plugin installed (it's required for WooCommerce subscription products). Then, add logic to check if a product is a subscription, and call Infusionsoft's Subscription API to create the subscription.
Modified Code Snippet
// Inside your order sync loop where you process each product $title = $product->get_name(); $unit_price = $product->get_price(); $sku = $product->get_sku(); $qty = $item->get_quantity(); $p_id = $product->get_id(); // Check if the product is a subscription product if (wcs_is_subscription_product($product)) { // Get the Infusionsoft contact ID (assumed you saved this during order/customer sync) $infusionsoft_contact_id = get_post_meta($order->get_id(), 'infusionsoft_contact_id', true); // Map WooCommerce subscription details to Infusionsoft's required format $subscription_cycle = $product->get_meta('_subscription_period'); // e.g., 'month' $interval_type = strtoupper($subscription_cycle); // Convert to INFUSIONSOFT format: 'MONTHLY' $interval_length = $product->get_meta('_subscription_interval'); // e.g., 1 for every 1 month $subscription_data = [ 'contactId' => $infusionsoft_contact_id, 'productId' => 'YOUR_INFUSIONSOFT_SUBSCRIPTION_PRODUCT_ID', // Replace with your Infusionsoft product ID 'paymentSchedule' => [ 'intervalType' => $interval_type, 'intervalLength' => $interval_length, 'startDate' => current_time('Y-m-d'), 'totalOccurrences' => -1, // Set to -1 for unlimited subscriptions ], 'price' => $unit_price, 'quantity' => $qty ]; // Call Infusionsoft's Subscription API to create the subscription $api_response = wp_remote_post('https://YOUR_INFUSIONSOFT_DOMAIN/api/rest/v1/subscriptions', [ 'headers' => [ 'Authorization' => 'Bearer YOUR_INFUSIONSOFT_API_TOKEN', 'Content-Type' => 'application/json' ], 'body' => json_encode($subscription_data) ]); // Handle the API response (save subscription ID to order meta for future reference) if (!is_wp_error($api_response)) { $response_body = json_decode(wp_remote_retrieve_body($api_response), true); if (isset($response_body['id'])) { update_post_meta($order->get_id(), 'infusionsoft_subscription_id', $response_body['id']); } } } else { // Your existing regular product sync logic $p_desc = $product->get_short_description(); $p_desc = str_replace(array("'",'"'), '', $p_desc); if (strpos($p_desc, '<') !== false) { $p_desc = $title; } $products[$p_id] = [ 'name' => $title, 'desc' => $p_desc, 'price' => $unit_price, 'sku' => $sku, 'quantity' => $qty ]; }
Key Notes to Keep in Mind
- Infusionsoft API Permissions: Ensure your API token has the necessary permissions to create subscriptions (check Infusionsoft's API docs for the required scopes).
- Product Mapping: You'll need to manually map your WooCommerce subscription products to corresponding products in Infusionsoft (use the Infusionsoft product ID in the code above).
- Customer Sync: Make sure the customer associated with the order is already synced to Infusionsoft (you need their
contactIdto link the subscription). - Error Handling: Add extra error handling (like logging API failures) to debug issues if syncs don't work as expected.
内容的提问来源于stack exchange,提问作者neeraj rai

