Skip to content

Instantly share code, notes, and snippets.

@ThomasLeCoz
Created December 16, 2015 15:49
Show Gist options
  • Save ThomasLeCoz/6db589ed8580dc910402 to your computer and use it in GitHub Desktop.
Save ThomasLeCoz/6db589ed8580dc910402 to your computer and use it in GitHub Desktop.
function send_receipt($order_id){
//if($new_status == 'processing' && $status != 'completed' || $new_status == 'completed' && $status == 'processing'){
if($status != 'processing' && $status != 'completed') {
$order = new WC_Order($order_id);
$products = $order->get_items();
foreach($products as $product){
$courses_id = get_post_meta($product['product_id'], '_related_course', true);
if($courses_id && is_array($courses_id)){
foreach($courses_id as $cid) {
$already_in = false;
if(empty($order->customer_user) || empty($cid)) {
error_log("[EMPTY] User id: " . $order->customer_user . " Course Id:" . $cid);
return;
}
$meta = get_post_meta( $cid, '_sfwd-courses', true );
$access_list = $meta['sfwd-courses_course_access_list'];
error_log('Access List: ' .$access_list);
if (!empty( $access_list )) {
error_log("Access List not empty: " . $access_list);
$access_list = explode(",", $access_list);
foreach($access_list as $c) {
if(trim($c) == $order->customer_user)
$already_in = true;
}
}
if (empty( $access_list ) || !$already_in) {
error_log("Empty list or user doesn't have access yet");
ld_update_course_access($order->customer_user, $cid, $remove = false);
// If it's a subscription...
if (WC_Subscriptions_Order::order_contains_subscription($order) || WC_Subscriptions_Renewal_Order::is_renewal( $order )) {
error_log("Subscription (may be renewal) detected");
if ($sub_key = WC_Subscriptions_Manager::get_subscription_key($order_id, $product['product_id'])) {
error_log("Subscription key: " . $sub_key);
$subscription_r = WC_Subscriptions_Manager::get_subscription( $sub_key );
$start_date = $subscription_r['start_date'];
error_log("Start Date:" . $start_date);
update_user_meta($order->customer_user, "course_".$cid."_access_from", strtotime($start_date));
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment