File: /var/www/html/ielts-store/wp-content/plugins/automatewoo/includes/Jobs/SetupGuestCustomers.php
<?php
namespace AutomateWoo\Jobs;
use AutomateWoo\Customer_Factory;
use AutomateWoo\Cron;
use AutomateWoo\Jobs\Traits\ValidateItemAsIntegerId;
use Exception;
/**
* Goes through every guest order and creates a customer for it.
*
* @since 5.2.0
*/
class SetupGuestCustomers extends AbstractBatchedActionSchedulerJob implements StartOnHookInterface {
use ValidateItemAsIntegerId;
/**
* Setup guest customer complete option name
*
* @var string
*/
protected $complete_option = '_automatewoo_setup_guest_customers_complete';
/**
* Get the name of the job.
*
* @return string
*/
public function get_name() {
return 'setup_guest_customers';
}
/**
* Get the name of an action to attach the job's start method to.
*
* @return string
*/
public function get_start_hook() {
return Cron::FOUR_HOUR_WORKER;
}
/**
* Process a single item.
*
* @param int $order_id
* @param array $args The args for this instance of the job. Args are already validated.
*
* @throws JobException If item can't be found.
*/
protected function process_item( $order_id, array $args ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
throw JobException::item_not_found();
}
$customer = Customer_Factory::get_by_order( $order );
if ( ! $customer ) {
throw JobException::item_not_found();
}
if ( ! $customer->get_date_last_purchased() ) {
// set the last purchase date
$orders = wc_get_orders(
[
'type' => 'shop_order',
'status' => wc_get_is_paid_statuses(),
'limit' => 1,
'customer' => $customer->get_email(),
'orderby' => 'date',
'order' => 'DESC',
]
);
if ( $orders ) {
$customer->set_date_last_purchased( $orders[0]->get_date_created() );
$customer->save();
}
}
}
/**
* Get a new batch of items.
*
* @param int $batch_number The batch number increments for each new batch in the job cycle.
* @param array $args The args for this instance of the job. Args are already validated.
*
* @return int[]
* @throws Exception If an error occurs. The exception will be logged by ActionScheduler.
*/
protected function get_batch( int $batch_number, array $args ) {
if ( get_option( $this->complete_option ) ) {
return false;
}
// guest orders
return wc_get_orders(
[
'type' => 'shop_order',
'limit' => $this->get_batch_size(),
'offset' => $this->get_query_offset( $batch_number ),
'status' => wc_get_is_paid_statuses(),
'customer_id' => 0,
'return' => 'ids',
]
);
}
/**
* Can the job start.
*
* @return bool Returns true if the job can start.
*
* @throws Exception If an error occurs. The exception will be logged by ActionScheduler.
*/
protected function can_start(): bool {
if ( get_option( $this->complete_option ) ) {
return false;
}
return parent::can_start();
}
/**
* Called when the job is completed.
*
* @param int $final_batch_number The final batch number when the job was completed.
* If equal to 1 then no items were processed by the job.
* @param array $args The args for this instance of the job.
*/
protected function handle_complete( int $final_batch_number, array $args ) {
update_option( $this->complete_option, true, false );
}
}