Select Page

最完整的 woocommerce $order info 使用大全

在寫 woocommerce 的程式時候,最常使用到的就是 $order 這個參數了,每次都要用 Google 查找很不方便,這次把它整理並且記錄起來,當作簡易字典查找,就快速且簡單的多了,也謝謝 Rodolfo Melogli 大神分享了好用的文章。

// Get Order ID and Key
$order->get_id();
$order->get_order_key();
 
// Get Order Totals $0.00
$order->get_formatted_order_total();
$order->get_cart_tax();
$order->get_currency();
$order->get_discount_tax();
$order->get_discount_to_display();
$order->get_discount_total();
$order->get_fees();
$order->get_formatted_line_subtotal();
$order->get_shipping_tax();
$order->get_shipping_total();
$order->get_subtotal();
$order->get_subtotal_to_display();
$order->get_tax_location();
$order->get_tax_totals();
$order->get_taxes();
$order->get_total();
$order->get_total_discount();
$order->get_total_tax();
$order->get_total_refunded();
$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();
$order->get_item_count_refunded();
$order->get_total_qty_refunded();
$order->get_qty_refunded_for_item();
$order->get_total_refunded_for_item();
$order->get_tax_refunded_for_item();
$order->get_total_tax_refunded_by_rate_id();
$order->get_remaining_refund_amount();
  
// Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {
   $product_id = $item->get_product_id();
   $variation_id = $item->get_variation_id();
   $product = $item->get_product(); // see link above to get $product info
   $product_name = $item->get_name();
   $quantity = $item->get_quantity();
   $subtotal = $item->get_subtotal();
   $total = $item->get_total();
   $tax = $item->get_subtotal_tax();
   $tax_class = $item->get_tax_class();
   $tax_status = $item->get_tax_status();
   $allmeta = $item->get_meta_data();
   $somemeta = $item->get_meta( '_whatever', true );
   $item_type = $item->get_type(); // e.g. "line_item"
}
 
// Other Secondary Items Stuff
$order->get_items_key();
$order->get_items_tax_classes();
$order->get_item_count();
$order->get_item_total();
$order->get_downloadable_items();
$order->get_coupon_codes();
  
// Get Order Lines
$order->get_line_subtotal();
$order->get_line_tax();
$order->get_line_total();
  
// Get Order Shipping
$order->get_shipping_method();
$order->get_shipping_methods();
$order->get_shipping_to_display();
  
// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();
  
// Get Order User, Billing & Shipping Addresses
$order->get_customer_id();
$order->get_user_id();
$order->get_user();
$order->get_customer_ip_address();
$order->get_customer_user_agent();
$order->get_created_via();
$order->get_customer_note();
$order->get_address_prop();
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_company();
$order->get_billing_address_1();
$order->get_billing_address_2();
$order->get_billing_city();
$order->get_billing_state();
$order->get_billing_postcode();
$order->get_billing_country();
$order->get_billing_email();
$order->get_billing_phone();
$order->get_shipping_first_name();
$order->get_shipping_last_name();
$order->get_shipping_company();
$order->get_shipping_address_1();
$order->get_shipping_address_2();
$order->get_shipping_city();
$order->get_shipping_state();
$order->get_shipping_postcode();
$order->get_shipping_country();
$order->get_address();
$order->get_shipping_address_map_url();
$order->get_formatted_billing_full_name();
$order->get_formatted_shipping_full_name();
$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();
  
// Get Order Payment Details
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();
  
// Get Order URLs
$order->get_checkout_payment_url();
$order->get_checkout_order_received_url();
$order->get_cancel_order_url();
$order->get_cancel_order_url_raw();
$order->get_cancel_endpoint();
$order->get_view_order_url();
$order->get_edit_order_url();
  
// Get Order Status
$order->get_status();
 
// Get Thank You Page URL
$order->get_checkout_order_received_url();

參考資料

https://woocommerce.github.io/code-reference/

wordpress divi 修改 woocommerce 每頁商品顯示數量

wordpress divi 修改 woocommerce 每頁商品顯示數量

在還沒有安裝 Divi 外掛的時候,想要修改 woocommerce 的每頁商品顯示數量,是需要去後台 外觀 -> 自訂 -> 佈景主題 -> woocommerce -> 產品目錄下修改每列產品數以及每頁行數的,但現在安裝了 Divi 外掛後,要改到後台 Divi -> Theme Options -> General 中,修改下面兩個項目才行

  • Number of Posts displayed on Archive pages
  • Number of Products displayed on WooCommerce archive pages

預設 WooCommerce 修改每頁商品顯示數量處

外觀 -> 佈景主題 -> 自訂 -> woocommerce -> 產品目錄

Divi 修改每頁商品顯示數量處

Divi -> Theme Options -> General

WordPress-取得User Id的方法

我們可以用 php 程式,來取得使用者的資訊,最重要的當然是 User ID 的取得,能取得 User ID 才能進行使用者的資訊取得,取得uid的方法百百種,這邊列出四種常用的場景,取得當下登入的使用者ID,另用會員的關鍵資料取得會員ID,利用文章取得作者ID,在商店中取得客戶ID,

1.get_current_user_id()

$current_user_id = get_current_user_id();
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;

2.使用會員資訊取得ID,get_user_by()

利用Email取的ID

$the_user = get_user_by('email', '[email protected]');
$the_user_id = $the_user->ID;

利用登入帳號

$the_user = get_user_by('login', 'rain');
$the_user_id = $the_user->ID;

3.利用文章資訊取得作者 ID

$my_post = get_post( $id ); // $id - Post ID
echo $my_post->post_author; // print post author ID

4.在woocommerce中取得訂單的客戶ID

$order = wc_get_order( 123 ); // your order ID
$customer_id = $order->get_customer_id(); // or $order->get_user_id() – the same

WordPress 存取 user meta 的方法

在 WordPress 中,尤其是使用到 woocommerce 商店功能時候,最常需要客製化的通常是使用者的各式各樣資訊,例如有使用者的手機號碼、推薦人資訊等,通常這些資訊沒有良好的plugin可以支援,欄位高度客製化,這時可以寫點 php 程式,來支持讓自己的網站更美好,以下分CRUD新增、修改、刪除、讀取來做說明。

新增 user meta 的方法

add_user_meta(int $user_id, string $meta_key,  mixed $meta_value,  bool $unique = false);

刪除 user meta 的方法

delete_user_meta( $user_id, $meta_key, $meta_value );

修改 user meta 的方法

update_user_meta ( $user_id, $meta_key, $meta_value, $prev_value );

讀取 user meta 的方法

get_user_meta( $user_id, $meta_key, $single );

參考資料

如何 Debug WooCommerce 3 +

如何 Debug WooCommerce 3 +

woocommerce 在 wordpress 中是可以擁有自己的除錯系統的,可以獨立在 wordpress, php, nginx 以外,可以利用 WooCommerce 官方提供的 Log 記錄功能,可以記錄各種狀態,和自己的標籤,可以比較有秩序且快速的除錯

使用 WC_Logger

WC_Logger官方文件示範了標準用法

$log = new WC_Logger();
$log_entry = print_r( $e, true );
$log_entry .= 'Exception Trace: ' . print_r( $e->getTraceAsString(), true );
$log->log( 'new-woocommerce-log-name', $log_entry );

簡易用法

$logger = wc_get_logger();
$logger->debug( 'debug message', array( 'source' => 'my-extension' ) );

查看 WC Log

在 WP 後台,選擇 WooCommerce -> 狀態

並且選擇 狀態 -> 日誌紀錄 ,選擇完畢後,在選擇你建立的紀錄標籤後,就可以看到清楚的訊息輸出了

同廠加映,用WordPress內建的除錯

編輯 wp-config,將 WP_DEBUG 的選項打開,缺點就是訊息太多,比較不好看清楚

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );