Skip to content

Instantly share code, notes, and snippets.

@nczz
Last active July 28, 2017 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nczz/ee7f24419fb658cd459b64d1b241f6d6 to your computer and use it in GitHub Desktop.
Save nczz/ee7f24419fb658cd459b64d1b241f6d6 to your computer and use it in GitHub Desktop.
WooCommerce 建立客製化訂單與結帳流程
<?php
//準備好或接收要記錄的欄位資訊
$address = array(
'first_name' => '某',
'last_name' => '某某',
'company' => '一介資男',
'email' => 'demo@domain.name',
'phone' => '0912123123',
'address_1' => '106台北市大安區',
'address_2' => '巴拉路巴拉號',
'city' => '台北',
'state' => '',
'postcode' => '106',
'country' => 'TW',
);
//建立一張空的訂單
$order = wc_create_order();
// The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
$order->add_product(get_product(202), 1); // 202 => 產品編號, 1 => 數量
$order->add_product(get_product(204), 2); // 還有其他加入購物車的就一併加一加
// 設定地址(付款人、收貨人)
$order->set_address($address, 'billing');
$order->set_address($address, 'shipping');
// 設定付款方式
$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method($payment_gateways['金流註冊時的代稱(slug)']);
// 計算訂單
$order->calculate_totals();
// 根據選擇地金流去觸發結帳
do_action('woocommerce_receipt_' . $order->get_payment_method(), $order->id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment