
內容簡介
如果你正在創建 WooCommerce 的主題或開發外掛,這個外掛對你來說非常珍貴!
WooCommerce 使用 WordPress 邏輯和資料庫表來儲存商店貨品、訂單、結帳等資料...
如果你正在與商店整合支付網關或任何第三方應用程式,你有時需要為特定訂單儲存一些額外的資料。延伸訂單加入自訂資料往往非常困難,甚至是不可能的。
擴充 WC 訂單
以下是如何在你的程式中延伸 WooCommerce 訂單的簡單方法:
| WC_CustomOrderData::extend($order);
要將自訂屬性設為訂單,只需輸入:
| $order->custom->your_custom_property = ‘some value’;
之後,你可以通過參考「custom」訂單屬性來使用自訂訂單屬性:
| $custom_property = $order->custom->your_custom_property;
你總是可以使用標準的 PHP 方法檢查自訂訂單屬性是否存在:
| if(isset($order->custom->your_custom_property)) { doStuff(); }
所有自訂訂單屬性都儲存在單獨的資料庫表中。當你完成使用訂單時,請確保保存自訂資料,方法如下:
| $order->custom->save();
注意:儲存的物件和陣列會以陣列形式還原。
作者
Miloš Đekić 是來自塞爾維亞貝爾格萊德的軟體愛好者,他喜歡創建有用的軟體。
外掛標籤
開發者團隊
② 後台搜尋「WooCommerce Custom Order Data」→ 直接安裝(推薦)
原文外掛簡介
If you are extending WooCommerce by creating themes or developing plugins you will find this plugin to be priceless!
WooCommerce uses WordPress logic and database tables to store data on store items, orders, checkouts…
If you are integrating a payment gateway or any 3rd party app with your store, you sometimes need to
store some extra data for specific orders. It tends to be very hard or even impossible to extend orders
with custom data.
Extend WC Orders
Here’s how easy it is to extend a WooCommerce order in your code:
| WC_CustomOrderData::extend($order);
To set a custom property to an order, just type:
| $order->custom->your_custom_property = ‘some value’;
After that you can use your custom order property by referring the “custom” order property:
| $custom_property = $order->custom->your_custom_property;
You can allways check if a custom order property exists using the standard PHP way:
| if(isset($order->custom->your_custom_property)) { doStuff(); }
All custom order properties are saved in a separate database table. When you finish working with the order,
make sure you save custom data by typing:
| $order->custom->save();
Note: Storred objects and arrays will be restored as arrays.
Author
Miloš Đekić is a software enthusiast from Belgrade,
Serbia. He loves to create useful software.
