/** * Triggered when an order is paid * @param int $order_id */ public function order_paid($order_id) { // Get the order $order = new WC_Order($order_id); if (get_post_meta($order_id, 'wc_paid_listings_packages_processed', true)) { return; } foreach ($order->get_items() as $item) { $product = wc_get_product($item['product_id']); if ($product->is_type(array('job_package', 'resume_package', 'job_package_subscription', 'resume_package_subscription')) && $order->customer_user) { // Give packages to user for ($i = 0; $i < $item['qty']; $i++) { $user_package_id = wc_paid_listings_give_user_package($order->customer_user, $product->id, $order_id); } // Approve job or resume with new package if (isset($item['job_id'])) { $job = get_post($item['job_id']); if (in_array($job->post_status, array('pending_payment', 'expired'))) { wc_paid_listings_approve_job_listing_with_package($job->ID, $order->customer_user, $user_package_id); } } elseif (isset($item['resume_id'])) { $resume = get_post($item['resume_id']); if (in_array($resume->post_status, array('pending_payment', 'expired'))) { wc_paid_listings_approve_resume_with_package($resume->ID, $order->customer_user, $user_package_id); } } } } update_post_meta($order_id, 'wc_paid_listings_packages_processed', true); }
/** * Purchase a job package * @param int|string $package_id * @param int $job_id * @return bool Did it work or not? */ private static function process_package($package_id, $is_user_package, $job_id) { if ($is_user_package) { $user_package = wc_paid_listings_get_user_package($package_id); $package = wc_get_product($user_package->get_product_id()); // Give job the package attributes update_post_meta($job_id, '_job_duration', $user_package->get_duration()); update_post_meta($job_id, '_featured', $user_package->is_featured() ? 1 : 0); update_post_meta($job_id, '_package_id', $user_package->get_product_id()); update_post_meta($job_id, '_user_package_id', $package_id); if ($package && 'listing' === $package->package_subscription_type) { update_post_meta($job_id, '_job_expires', ''); // Never expire automatically } // Approve the job if (in_array(get_post_status($job_id), array('pending_payment', 'expired'))) { wc_paid_listings_approve_job_listing_with_package($job_id, get_current_user_id(), $package_id); } do_action('wcpl_process_package_for_job_listing', $package_id, $is_user_package, $job_id); return true; } elseif ($package_id) { $package = wc_get_product($package_id); // Give job the package attributes update_post_meta($job_id, '_job_duration', $package->get_duration()); update_post_meta($job_id, '_featured', $package->is_featured() ? 1 : 0); update_post_meta($job_id, '_package_id', $package_id); if ('listing' === $package->package_subscription_type) { update_post_meta($job_id, '_job_expires', ''); // Never expire automatically } // Add package to the cart WC()->cart->add_to_cart($package_id, 1, '', '', array('job_id' => $job_id)); woocommerce_add_to_cart_message($package_id); // Clear cookie wc_setcookie('chosen_package_id', '', time() - HOUR_IN_SECONDS); wc_setcookie('chosen_package_is_user_package', '', time() - HOUR_IN_SECONDS); do_action('wcpl_process_package_for_job_listing', $package_id, $is_user_package, $job_id); // Redirect to checkout page wp_redirect(get_permalink(woocommerce_get_page_id('checkout'))); exit; } }