本文整理汇总了PHP中WC_Checkout::create_order方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Checkout::create_order方法的具体用法?PHP WC_Checkout::create_order怎么用?PHP WC_Checkout::create_order使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Checkout
的用法示例。
在下文中一共展示了WC_Checkout::create_order方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create_orders
public function create_orders()
{
global $wpdb, $woocommerce;
if (empty($this->settings['orders_per_hour'])) {
return;
}
set_time_limit(0);
$woocommerce->init();
$woocommerce->frontend_includes();
$session_class = apply_filters('woocommerce_session_handler', 'WC_Session_Handler');
// Class instances
require_once WC()->plugin_path() . '/includes/abstracts/abstract-wc-session.php';
$woocommerce->session = new WC_Session_Handler();
$woocommerce->cart = new WC_Cart();
// Cart class, stores the cart contents
$woocommerce->customer = new WC_Customer();
// Customer class, handles data such as customer location
$woocommerce->countries = new WC_Countries();
$woocommerce->checkout = new WC_Checkout();
//$woocommerce->product_factory = new WC_Product_Factory(); // Product Factory to create new product instances
$woocommerce->order_factory = new WC_Order_Factory();
// Order Factory to create new order instances
$woocommerce->integrations = new WC_Integrations();
// Integrations class
// clear cart
if (!defined('WOOCOMMERCE_CHECKOUT')) {
define('WOOCOMMERCE_CHECKOUT', true);
}
$woocommerce->cart->empty_cart();
$product_ids = $this->settings['products'];
if (empty($product_ids)) {
$products = $wpdb->get_col("SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'product'");
foreach ($products as $product_id) {
$product_ids[] = $product_id;
}
}
for ($x = 0; $x < $this->settings['orders_per_hour']; $x++) {
$cart = array();
$num_products = rand($this->settings['min_order_products'], $this->settings['max_order_products']);
$create_user = false;
if ($this->settings['create_users']) {
$create_user = rand(1, 100) <= 50 ? true : false;
}
if ($create_user) {
$user_id = self::create_user();
} else {
$user_id = self::get_random_user();
}
// add random products to cart
for ($i = 0; $i < $num_products; $i++) {
$idx = rand(0, count($product_ids) - 1);
$product_id = $product_ids[$idx];
$woocommerce->cart->add_to_cart($product_id, 1);
}
// process checkout
$data = array('billing_country' => get_user_meta($user_id, 'billing_country', true), 'billing_first_name' => get_user_meta($user_id, 'billing_first_name', true), 'billing_last_name' => get_user_meta($user_id, 'billing_last_name', true), 'billing_company' => '', 'billing_address_1' => get_user_meta($user_id, 'billing_address_1', true), 'billing_address_2' => '', 'billing_city' => get_user_meta($user_id, 'billing_city', true), 'billing_state' => get_user_meta($user_id, 'billing_state', true), 'billing_postcode' => get_user_meta($user_id, 'billing_postcode', true), 'billing_email' => get_user_meta($user_id, 'billing_email', true), 'billing_phone' => get_user_meta($user_id, 'billing_phone', true), 'shipping_country' => get_user_meta($user_id, 'shipping_country', true), 'shipping_first_name' => get_user_meta($user_id, 'shipping_first_name', true), 'shipping_last_name' => get_user_meta($user_id, 'shipping_last_name', true), 'shipping_company' => '', 'shipping_address_1' => get_user_meta($user_id, 'shipping_address_1', true), 'shipping_address_2' => '', 'shipping_city' => get_user_meta($user_id, 'shipping_city', true), 'shipping_state' => get_user_meta($user_id, 'shipping_state', true), 'shipping_postcode' => get_user_meta($user_id, 'shipping_postcode', true), 'shipping_email' => get_user_meta($user_id, 'shipping_email', true), 'shipping_phone' => get_user_meta($user_id, 'shipping_phone', true));
$checkout = new WC_Checkout();
$woocommerce->cart->calculate_totals();
$order_id = $checkout->create_order();
if ($order_id) {
update_post_meta($order_id, '_payment_method', 'bacs');
update_post_meta($order_id, '_payment_method_title', 'Bacs');
update_post_meta($order_id, '_shipping_method', 'free_shipping');
update_post_meta($order_id, '_shipping_method_title', 'Free Shipping');
update_post_meta($order_id, '_customer_user', absint($user_id));
foreach ($data as $key => $value) {
update_post_meta($order_id, '_' . $key, $value);
}
do_action('woocommerce_checkout_order_processed', $order_id, $data);
$order = new WC_Order($order_id);
// figure out the order status
$status = 'completed';
$rand = mt_rand(1, 100);
$completed_pct = $this->settings['order_completed_pct'];
// e.g. 90
$processing_pct = $completed_pct + $this->settings['order_processing_pct'];
// e.g. 90 + 5
$failed_pct = $processing_pct + $this->settings['order_failed_pct'];
// e.g. 95 + 5
if ($this->settings['order_completed_pct'] > 0 && $rand <= $completed_pct) {
$status = 'completed';
} elseif ($this->settings['order_processing_pct'] > 0 && $rand <= $processing_pct) {
$status = 'processing';
} elseif ($this->settings['order_failed_pct'] > 0 && $rand <= $failed_pct) {
$status = 'failed';
}
if ($status == 'failed') {
$order->update_status($status);
} else {
$order->payment_complete();
$order->update_status($status);
}
}
// clear cart
$woocommerce->cart->empty_cart();
}
}