天天看點

ajax 送出訂單,php-在Woocommerce 3中通過ajax送出并在結帳時建立訂單

我在結帳表單中添加了一個按鈕:

并在functions.php檔案中添加了一個AJAX代碼段:

add_action('wp_head', 'ajax_call_place_order');

function ajax_call_place_order() {

?>

jQuery(document).ready(function($) {

$(document).on("click", "#ajax-order-btn" ,function(e) {

e.preventDefault();

var data = {

action: 'ajax_order',

};

$.post('<?php echo esc_url( home_url() ); ?>/wp-admin/admin-ajax.php', data);

});

});

}

這是AJAX回調,可通過該回調以程式設計方式建立訂單:

add_action('wp_ajax_ajax_order', 'ajax_order_callback_wp');

add_action( 'wp_ajax_nopriv_ajax_order', 'ajax_order_callback_wp' );

function ajax_order_callback_wp() {

$address = array(

'first_name' => 'John',

'last_name' => 'Doe',

'company' => 'Speed Society',

'email' => '[email protected]',

'phone' => '760-555-1212',

'address_1' => '123 Main st.',

'address_2' => '104',

'city' => 'San Diego',

'state' => 'Ca',

'postcode' => '92121',

'country' => 'US'

);

$order = wc_create_order();

$order->add_product( get_product('275962'), 1); // This is an existing SIMPLE product

$order->set_address( $address, 'billing' );

$order->calculate_totals();

$order->update_status("Completed", 'Imported order', TRUE);

}

問題是我找不到一種方法來擷取目前的訂單資料,并在以程式設計方式建立訂單而不是目前的寫死資料時使用該資料.我需要在Checkout頁面上與目前訂單完全相同的訂單.

我正在嘗試使用WC_Checkout以及方法create_order()和get_checkout_fields(),但沒有成功.

解決方法:

對于PHP訂單建立,我使用了來自WC_Checkout create_order()方法的自定義克隆,該克隆非常有效.所有送出的資料将按順序自動設定.

對于自定義訂單中繼資料和自定義訂單項目中繼資料,可以将所有woocommerce預設挂鈎用作:

> woocommerce_checkout_create_order

> woocommerce_checkout_update_order_meta

> woocommerce_checkout_create_order_line_item

>等等…

我還對jQuery代碼進行了一些必要的更改以使其正常工作,并通過Ajax *(現在位于頁腳中)*向PHP發送了必要的格式化資料.

通過Ajax建立訂單的完整代碼:

add_action('wp_footer', 'checkout_billing_email_js_ajax' );

function checkout_billing_email_js_ajax() {

// Only on Checkout

if( is_checkout() && ! is_wc_endpoint_url() ) :

?>

jQuery(function($){

if (typeof wc_checkout_params === 'undefined')

return false;

$(document.body).on("click", "#ajax-order-btn" ,function(evt) {

evt.preventDefault();

$.ajax({

type: 'POST',

url: wc_checkout_params.ajax_url,

contentType: "application/x-www-form-urlencoded; charset=UTF-8",

enctype: 'multipart/form-data',

data: {

'action': 'ajax_order',

'fields': $('form.checkout').serializeArray(),

'user_id': <?php echo get_current_user_id(); ?>,

},

success: function (result) {

console.log(result); // For testing (to be removed)

},

error: function(error) {

console.log(error); // For testing (to be removed)

}

});

});

});

endif;

}

add_action('wp_ajax_ajax_order', 'submited_ajax_order_data');

add_action( 'wp_ajax_nopriv_ajax_order', 'submited_ajax_order_data' );

function submited_ajax_order_data() {

if( isset($_POST['fields']) && ! empty($_POST['fields']) ) {

$order = new WC_Order();

$cart = WC()->cart;

$checkout = WC()->checkout;

$data = [];

// Loop through posted data array transmitted via jQuery

foreach( $_POST['fields'] as $values ){

// Set each key / value pairs in an array

$data[$values['name']] = $values['value'];

}

$cart_hash = md5( json_encode( wc_clean( $cart->get_cart_for_session() ) ) . $cart->total );

$available_gateways = WC()->payment_gateways->get_available_payment_gateways();

// Loop through the data array

foreach ( $data as $key => $value ) {

// Use WC_Order setter methods if they exist

if ( is_callable( array( $order, "set_{$key}" ) ) ) {

$order->{"set_{$key}"}( $value );

// Store custom fields prefixed with wither shipping_ or billing_

} elseif ( ( 0 === stripos( $key, 'billing_' ) || 0 === stripos( $key, 'shipping_' ) )

&& ! in_array( $key, array( 'shipping_method', 'shipping_total', 'shipping_tax' ) ) ) {

$order->update_meta_data( '_' . $key, $value );

}

}

$order->set_created_via( 'checkout' );

$order->set_cart_hash( $cart_hash );

$order->set_customer_id( apply_filters( 'woocommerce_checkout_customer_id', isset($_POST['user_id']) ? $_POST['user_id'] : '' ) );

$order->set_currency( get_woocommerce_currency() );

$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );

$order->set_customer_ip_address( WC_Geolocation::get_ip_address() );

$order->set_customer_user_agent( wc_get_user_agent() );

$order->set_customer_note( isset( $data['order_comments'] ) ? $data['order_comments'] : '' );

$order->set_payment_method( isset( $available_gateways[ $data['payment_method'] ] ) ? $available_gateways[ $data['payment_method'] ] : $data['payment_method'] );

$order->set_shipping_total( $cart->get_shipping_total() );

$order->set_discount_total( $cart->get_discount_total() );

$order->set_discount_tax( $cart->get_discount_tax() );

$order->set_cart_tax( $cart->get_cart_contents_tax() + $cart->get_fee_tax() );

$order->set_shipping_tax( $cart->get_shipping_tax() );

$order->set_total( $cart->get_total( 'edit' ) );

$checkout->create_order_line_items( $order, $cart );

$checkout->create_order_fee_lines( $order, $cart );

$checkout->create_order_shipping_lines( $order, WC()->session->get( 'chosen_shipping_methods' ), WC()->shipping->get_packages() );

$checkout->create_order_tax_lines( $order, $cart );

$checkout->create_order_coupon_lines( $order, $cart );

do_action( 'woocommerce_checkout_create_order', $order, $data );

// Save the order.

$order_id = $order->save();

do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data );

echo 'New order created with order ID: #'.$order_id.'.' ;

}

die();

}

代碼進入您的活動子主題(或活動主題)的function.php檔案中.經過測試和工作.

标簽:ajax,woocommerce,wordpress,php,jquery

來源: https://codeday.me/bug/20191024/1922779.html