要怎麼實作超商取貨,先來看看別人怎麼做:
ezShip for WooCommerce
https://cloudwp.pro/market/plugins/premium-plugins/ezship/
看畫面和設計就好了,沒有真的要買。
ezShip 與 WooCommerce 的物流服務串接
https://cloudwp.pro/blog/ezship-for-woocommerce-%E8%B6%85%E5%95%86%E7%89%A9%E6%B5%81%E6%9C%8D%E5%8B%99/
ezShip 的做法,是把 browser redirect 到 ezShip的介面,完全跳脫 woocommerce 的情況下,讓 user 去選物流。
綠界ECPay PHP 購物車模組
https://github.com/ECPay/ECPayPHPCartModule
綠界的source code 裡放了很多有用資訊,要hook 的點。
參數的詳細說明:
物流整合 API 介接規格說明
https://www.ecpay.com.tw/Content/files/ecpay_030.pdf
綠界串好的範例:
https://www.minwt.com/minwt-2/myapp/17208.html
下面的 code, 可以看的出來,ECPay 在後台串超商門市資訊,用了 4個欄位,用來放 ‘門市名稱’, ‘purchaserStore’,’門市地址’, ‘purchaserAddress’,’門市電話’, ‘purchaserPhone’,’門市代號’, ‘CVSStoreID’。
function EcPay_custom_admin_billing_fields($fields){
global $theorder;
$fields['purchaserStore'] = array(
'label' => __( '門市名稱', 'purchaserStore' ),
'value' =>get_post_meta( $theorder->id, '_purchaserStore', true ),
'show' => true
);
$fields['purchaserAddress'] = array(
'label' => __( '門市地址', 'purchaserAddress' ),
'value' =>get_post_meta( $theorder->id, '_purchaserAddress', true ),
'show' => true
);
$fields['purchaserPhone'] = array(
'label' => __( '門市電話', 'purchaserPhone' ),
'value' =>get_post_meta( $theorder->id, '_purchaserPhone', true ),
'show' => true
);
$fields['CVSStoreID'] = array(
'label' => __( '門市代號', 'CVSStoreID' ),
'value' =>get_post_meta( $theorder->id, '_CVSStoreID', true ),
'show' => true
);
return $fields;
}
下面這段 code, 把訂單裡的資料,放進 mysql database:
function my_custom_checkout_field_save( $order_id )
{
// custom field
$purchaserStore = '_purchaserStore' ;
$purchaserAddress = '_purchaserAddress';
$purchaserPhone = '_purchaserPhone' ;
$CVSStoreID = '_CVSStoreID' ;
// save custom field to order
if( !empty($_POST['purchaserStore']) && !empty($_POST['purchaserAddress']) ){
update_post_meta( $order_id, $purchaserStore , wc_clean( $_POST['purchaserStore'] ) );
update_post_meta( $order_id, $purchaserAddress, wc_clean( $_POST['purchaserAddress'] ) );
update_post_meta( $order_id, $purchaserPhone , wc_clean( $_POST['purchaserPhone'] ) );
update_post_meta( $order_id, $CVSStoreID , wc_clean( $_POST['CVSStoreID'] ) );
}
}
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_save' );
這段 code, 很實用,為切貨運方式不同,會必填欄位不同:
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
global $woocommerce;
$shipping_method = $woocommerce->session->get( 'chosen_shipping_methods' );
if ($shipping_method[0] == "ecpay_shipping" && (! $_POST['purchaserStore']) )
wc_add_notice( __( '請選擇取貨門市' ), 'error' );
}
上面的 code 對應回去的 WooCommerce code 是 includes/class-wc-checkout.php:
/**
* Process the checkout after the confirm order button is pressed.
*/
public function process_checkout() {
try {
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-process_checkout' ) ) {
WC()->session->set( 'refresh_totals', true );
throw new Exception( __( 'We were unable to process your order, please try again.', 'woocommerce' ) );
}
wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
wc_set_time_limit( 0 );
do_action( 'woocommerce_before_checkout_process' );
if ( WC()->cart->is_empty() ) {
throw new Exception( sprintf( __( 'Sorry, your session has expired. <a href="%s" class="wc-backward">Return to shop</a>', 'woocommerce' ), esc_url( wc_get_page_permalink( 'shop' ) ) ) );
}
do_action( 'woocommerce_checkout_process' );
$errors = new WP_Error();
$posted_data = $this->get_posted_data();
// Update session for customer and totals.
$this->update_session( $posted_data );
// Validate posted data and cart items before proceeding.
$this->validate_checkout( $posted_data, $errors );
foreach ( $errors->get_error_messages() as $message ) {
wc_add_notice( $message, 'error' );
}
if ( empty( $posted_data['woocommerce_checkout_update_totals'] ) && 0 === wc_notice_count( 'error' ) ) {
$this->process_customer( $posted_data );
$order_id = $this->create_order( $posted_data );
$order = wc_get_order( $order_id );
if ( is_wp_error( $order_id ) ) {
throw new Exception( $order_id->get_error_message() );
}
do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );
if ( WC()->cart->needs_payment() ) {
$this->process_order_payment( $order_id, $posted_data['payment_method'] );
} else {
$this->process_order_without_payment( $order_id );
}
}
} catch ( Exception $e ) {
wc_add_notice( $e->getMessage(), 'error' );
}
$this->send_ajax_failure_response();
}
門市的資訊,似乎便利商店的官方都沒有提供,所以需要自已來弄database,由於付費的服務裡的資料拿不到,所以自已來整理一下:
7-11 全台門市查詢
http://www.7-11.com.tw/search.asp
全家 萊爾富 OK 門市查詢
http://cvs.map.com.tw/
http://map.ezship.com.tw
全家 FamilyMart 全台門市查詢
http://www.family.com.tw/marketing/inquiry.aspx
萊爾富 Hi-Life 全台門市查詢
http://www.hilife.com.tw/storeInquiry_street.aspx
OK – MART 全台門市查詢
http://www.okmart.com.tw/convenient_shopSearch.asp
發現 ezShip 有免費串接服務:
參數版資料交換文件(ezship_WebOrder_HttpRequest.pdf:
https://www.ezship.com.tw/file/ezship_WebOrder_HttpRequest.pdf
ezShip網站串接說明文件-參數版
https://www.ezship.com.tw/file/ezship_WebOrder_HttpRequest.pdf
網站串接用法(基本版) – 超商取貨
http://blog.webgolds.com/view/301
弄了半天,ezShip 不能選取7-11 門市,ezShip的服務只有提供全家、萊爾富、OK。