To synchronize WooCommerce orders with LearnPress orders, especially for live course batch purchases, you need to hook into WooCommerce's order creation process and then create a corresponding LearnPress order programmatically.
What you want:
- When a WooCommerce order is created (especially for live course batch purchases), create a LearnPress order for the same purchase.
- This ensures both WooCommerce and LearnPress have the order data.
Step-by-step solution:
1. Hook into WooCommerce order creation
Use the woocommerce_thankyou action hook, which fires after an order is created and payment is complete.
add_action('woocommerce_thankyou', 'sync_woocommerce_order_to_learnpress', 10, 1);
function sync_woocommerce_order_to_learnpress($order_id) {
if (!$order_id) return;
$order = wc_get_order($order_id);
if (!$order) return;
// Loop through order items
foreach ($order->get_items() as $item_id => $item) {
$product_id = $item->get_product_id();
// Check if product is associated with a LearnPress course
$course_id = get_post_meta($product_id, '_lp_course_id', true);
if (!$course_id) {
// If no direct meta, maybe product ID is the course ID (depends on your setup)
if (get_post_type($product_id) == 'lp_course') {
$course_id = $product_id;
}
}
if ($course_id && get_post_type($course_id) == 'lp_course') {
// Check course type
$course_type = get_post_meta($course_id, 'os_course_type', true);
if ($course_type === 'live' || $course_type === 'consultancy') {
// Create LearnPress order for this course purchase
create_learnpress_order($order, $course_id, $item);
}
}
}
}
2. Create LearnPress order programmatically
You need to create a LearnPress order linked to the WooCommerce order and user.
function create_learnpress_order($wc_order, $course_id, $wc_order_item) {
$user_id = $wc_order->get_user_id();
if (!$user_id) return;
// Check if LearnPress order already exists for this WooCommerce order to avoid duplicates
$existing_orders = get_posts(array(
'post_type' => 'lp_order',
'meta_query' => array(
array(
'key' => '_wc_order_id',
'value' => $wc_order->get_id(),
'compare' => '='
)
)
));
if (!empty($existing_orders)) {
return; // Order already synced
}
// Prepare LearnPress order data
$order_data = array(
'post_type' => 'lp_order',
'post_status' => 'publish',
'post_author' => $user_id,
'post_title' => 'Order for course #' . $course_id,
'meta_input' => array(
'_user_id' => $user_id,
'_course_id' => $course_id,
'_wc_order_id' => $wc_order->get_id(),
'_order_total' => $wc_order->get_total(),
'_order_status' => 'completed', // or 'processing' depending on your flow
'_order_items' => array(
array(
'product_id' => $wc_order_item->get_product_id(),
'quantity' => $wc_order_item->get_quantity(),
'total' => $wc_order_item->get_total(),
)
)
)
);
// Insert LearnPress order post
$lp_order_id = wp_insert_post($order_data);
if ($lp_order_id) {
// You can add additional meta or trigger LearnPress hooks here if needed
// For example, mark course as purchased/enrolled
do_action('learn_press_order_created', $lp_order_id, $user_id, $course_id);
}
}