- Windows 本地安装 Kibana 查询 Elasticsearch
- Windows 本地安装和使用 Elasticsearch
- WooCommerce 请登录 链接不工作
- 使 WooCommerce 订单搜索支持自定义字段
- 如何使用 WooCommerce Session
- 获取 WooCommerce 页面地址的方法
- WooCommerce后台通过自定义字段检索产品
- WooCommerce 自定义订单号
- WooCommerce Login / Register Redirect
- WooCommerce自带的shortcodes
- WooCommerce 打印订单
- WooCommerce 新用户注册管理员通知
- WooCommerce 移除产品页商品图片的url
- WooCommerce Email 模版增加全局内容的一个方法
- 错误:Call to a member function is_visible() on a non-object
- WooCommerce实用代码集合
- WooCommerce根据支付方式收取额外费用(2021)
- WooCommerce 订单管理
- WooCommerce移除登出账户的确认提示
- WooCommerce列出所有产品分类(2021)
- WooCommerce收据功能的实现
- WooCommerce Dynamic Pricing动态价格表(2021)
- 如何免费试用WordPress付费插件
- 写代码定制WooCommerce产品页模板(2021)
- 自定义WooCommerce Order Details模板明细部分(2021)
- WooCommerce Class WC_Order
- WooCommerce 产品搜索支持 SKU
- WooCommerce定制产品的Additional Information选项卡(2021)
- WooCommerce 定制产品页选项卡(2021)
- WooCommerce admin bar快捷菜单
- WooCommerce库存管理插件Stock Manager for WooCommerce(2021)
- WordPress备份插件UpdraftPlus(2021)
- 隐藏WooCommerce的购物功能
- WooCommerce目录模式Catalog Mode(2021)
- 构建基于WooCommerce和WPML的多语言电商网站 - 概述篇
- 构建基于WooCommerce和WPML的多语言电商网站 - 安装WooCommerce和测试数据
- 配置WordPress运行环境 - Wampserver安装图解
- 构建基于WooCommerce和WPML的多语言电商网站 - 安装和配置 WPML 插件
- 构建基于 WooCommerce 和 WPML 的多语言电商网站 - 使 WooCommerce 和 WPML 协同工作
- WooCommerce Paypal & RMB
- WooCommerce 2.0 来袭
- 如何修改 WooCommerce 插件的模版
- WooCommerce 显示每个产品的总销量
- WooCommerce 中的 Custom JavaScript Event
- WooCommerce Authorize.net CIM Gateway
- 用WooCommerce Fees API 添加手续费
- WooCommerce 产品加入购物车后直接结账
- 设置 Paypal Sandbox 测试 WooCommerce Subscription 产品
- WooCommerce Conditional Tags 详解
- WooCommerce Single Category Selector
- WooCommerce 自定义结账字段(2021)
- WooCommerce 将产品属性加入网站菜单(2021)
- WooCommerce Product API(2021)
- WooCommerce 添加附加费 surcharge(2021)
- WooCommerce 安装中文语言包(2021)
- WooCommerce 营销:订阅促销弹窗和潜在客户发掘(2021)
- WooCommerce 邮件定制、预览和测试(2021)
- WooCommerce 在线站点付款测试(2021)
- WooCommerce Product Archive Image Slider(2021)
- woocommerce_form_field() examples
- WooCommerce 商店页插入 shortcode 问题
- WooCommerce 在 Email Header 中获取用户信息
- WooCommerce 自定义结账字段图文详解
- WooCommerce 2.1.12 - 如何修改相关产品列表
- 修改 WooCommerce My Account 页面的地址格式
- Woocommerce:如何根据国家设置支付方式
- Woocommerce 支付宝插件初探
- 支付宝集成 如何在回调地址中使用自定义参数
- Woocommerce Settings API 如何使用
- Woocommerce 中文货币符号错误如何解决
- WooCommerce 如何扩展支付方式
- Woo commerce 搭建 WordPress 电子商务网站
- WooCommerce 查看所有用户购物车(2021)
- WooCommerce 最近一个月销量排行(2021)
- WooCommerce 后台自定义产品选项(2021)
- WooCommerce 自定义产品列表带分页(2021)
- WooCommerce 设置 - 自定义选项卡和字段(2021)
- WooCommerce My Account Menu Links 定制方法(2021)
- WooCommerce 产品列表增加数量字段
- WooCommerce 可变产品变种的数量限制
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
WooCommerce Class WC_Order
WC_Order这个类是WooCommerce里操作订单的类,实例化这个类就获取了一个订单,之后就能读取订单的各种属性,或者操作订单。本文简单记录了常用的方法。
名词解释
- order_item – 订单中的详细条目,order_item的类型有product、fee、coupon、shipping等,最常见的line_item是指product。
- order_item_meta – order_item的详细信息,如果把order_item类比成post,那order_item_meta就是post_meta。
- item_subtotal – 产品的单价,不包含优惠。
- item_total – 产品优惠完的价格。
- line_subtotal – 一个产品的单价乘以它的购买数量,即一个产品的总价,不包含优惠。
- line_total – 就是line_subtotal加上优惠。
实例化一个WC_Order类
$order = wc_get_order( 871 ); // 871 is the order ID
价格优惠等信息
// Get totals for display on pages and in emails including subtotal,total,shipping,tax,refund etc.
$order->get_order_item_totals();
$order->get_discount_total();
$order->get_discount_tax();
$order->get_shipping_total();
$order->get_shipping_tax();
$order->get_cart_tax();
$order->get_total();
$order->get_total_tax();
$order->get_total_discount();
$order->get_subtotal();
$order->get_tax_totals();
$order->get_formatted_order_total();
$order->get_subtotal_to_display( $compound = false, $tax_display = '' );
$order->get_shipping_to_display( $tax_display = '' );
$order->get_discount_to_display( $tax_display = '' );
// Return an array of shipping costs within this order.
$order->get_shipping_methods();
// Gets formatted shipping method title.
$order->get_shipping_method();
$order->get_coupon_codes();
Getters
$order->get_type();
$order->get_currency();
$order->get_version();
// Gets the order number for display (by default, order ID).
$order->get_order_number();
// e.g. wc_order_48atSN91iwA8C
$order->get_order_key();
$order->get_customer_id();
//Get the user associated with the order. False for guests.
$order->get_user();
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_formatted_billing_full_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_formatted_shipping_full_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_shipping_phone();
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();
$order->get_customer_ip_address();
$order->get_customer_user_agent();
$order->get_customer_note();
$order->get_date_completed();
$order->get_date_paid();
$order->get_cart_hash();
// Raw addresses in array format.
$order->get_address();
$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();
// usually checkout
$order->get_created_via();
// Get a google map url with the shipping address as its parameter.
$order->get_shipping_address_map_url();
// Get basic order data in array format.
$order->get_base_data();
// Get all class data in array format.
$order->get_data();
$order->has_billing_address();
$order->has_shipping_address();
Setters
这些方法只能改变存储在类中的值,不会修改数据库。如果要更改数据库,需要调用save方法
$order->set_version( $value );
$order->set_currency( $value );
$order->set_discount_total( $value );
$order->set_discount_tax( $value );
$order->set_shipping_total( $value );
$order->set_shipping_tax( $value );
$order->set_cart_tax( $value );
$order->set_total( $value );
$order->set_order_key( $value );
$order->set_customer_id( $value );
$order->set_billing_first_name( $value );
$order->set_billing_last_name( $value );
$order->set_billing_company( $value );
$order->set_billing_address_1( $value );
$order->set_billing_address_2( $value );
$order->set_billing_city( $value );
$order->set_billing_state( $value );
$order->set_billing_postcode( $value );
$order->set_billing_country( $value );
$order->set_billing_email( $value );
$order->set_billing_phone( $value );
$order->set_shipping_first_name( $value );
$order->set_shipping_last_name( $value );
$order->set_shipping_company( $value );
$order->set_shipping_address_1( $value );
$order->set_shipping_address_2( $value );
$order->set_shipping_city( $value );
$order->set_shipping_state( $value );
$order->set_shipping_postcode( $value );
$order->set_shipping_country( $value );
$order->set_shipping_phone( $value );
$order->set_payment_method( $payment_method );
$order->set_payment_method_title( $value );
$order->set_transaction_id( $value );
$order->set_customer_ip_address( $value );
$order->set_customer_user_agent( $value );
$order->set_created_via( $value );
$order->set_customer_note( $value );
$order->set_date_completed( $date );
$order->set_date_paid( $date );
$order->set_cart_hash( $value );
$order->set_status( 'wc-failed', 'A manual update', true );
举例:如果用set_status()修改订单状态,需要这样做。
$order->set_status( 'wc-failed', 'A manual update', true );
$order->save();
更新Order Meta,也就是postmeta表里的数据
$order->update_meta_data( 'custom_order_meta_key', 'your value' );
$order->save();
Conditionals
// Check if an order key is valid.
$order->key_is_valid( $key );
// See if order matches cart_hash.
$order->has_cart_hash( $key );
// Checks if an order can be edited, specifically for use on the Edit Order screen.
$order->is_editable( $key );
// Returns if an order has been paid for based on the order status.
$order->is_paid();
// Checks if product download is permitted.
$order->is_download_permitted();
// Checks if an order needs display the shipping address, based on shipping method.
$order->needs_shipping_address();
// Returns true if the order contains a downloadable product.
$order->has_downloadable_item();
// Get downloads from all line items for this order.
$order->get_downloadable_items();
// Checks if an order needs payment, based on status and order total.
$order->needs_payment();
// See if the order needs processing before it can be completed.
$order->needs_processing();
URLs and Endpoints
// Generates a URL so that a customer can pay for their (unpaid - pending) order. Pass 'true' for the checkout version which doesn't offer gateway choices.
$order->get_checkout_payment_url( $on_checkout = false );
// Generates a URL for the thanks page (order received).
$order->get_checkout_order_received_url();
// Generates a URL so that a customer can cancel their (unpaid - pending) order.
$order->get_cancel_order_url();
// Generates a raw (unescaped) cancel-order URL for use by payment gateways.
$order->get_cancel_order_url_raw();
// Generates a URL to view an order from the my account page.
$order->get_view_order_url();
// Get's the URL to edit the order in the backend.
$order->get_edit_order_url();
Order notes
// Adds a note (comment) to the order. Order must exist.
$order->add_order_note( 'my note', $is_customer_note = 0, $added_by_user = false );
$order->get_customer_order_notes();
Refunds
// Get order refunds. refunds are stored as a custom post type
$order->get_refunds();
// Get amount already refunded.
$order->get_total_refunded();
$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();
$order->get_item_count_refunded( $item_type = '' );
$order->get_total_qty_refunded( $item_type = 'line_item' );
$order->get_qty_refunded_for_item( $item_id, $item_type = 'line_item' );
$order->get_total_refunded_for_item( $item_id, $item_type = 'line_item' );
$order->get_tax_refunded_for_item( $item_id, $tax_id, $item_type = 'line_item' );
$order->get_total_tax_refunded_by_rate_id( $rate_id );
$order->get_remaining_refund_amount();
$order->get_remaining_refund_items();
Order Status
$order->set_status( $new_status, $note = '', $manual_update = false );
$order->update_status( $new_status, $note = '', $manual = false );
Order Item Handling
// Return an array of items/products within this order.
$order->get_items( $types = 'line_item' );
$order->get_coupons();
$order->get_fees();
$order->get_taxes();
// Gets the count of order items of a certain type.
$order->get_item_count( $item_type = '' );
// Get an order item object by order_item_id
$order->get_item( $item_id );
$order->remove_item( $item_id );
$order->add_item( $item );
$order->add_product( $product, $qty = 1, $args = array() );
// Item unit price before discount
$order->get_item_subtotal( $item, $inc_tax = false, $round = true );
// Item unit price after discount
$order->get_item_total( $item, $inc_tax = false, $round = true );
// Item total price (unit price * qty) before discount
$order->get_line_subtotal( $item, $inc_tax = false, $round = true );
// Item total price (unit price * qty) after discount
$order->get_line_total( $item, $inc_tax = false, $round = true );
$order->get_item_tax( $item, $round = true );
$order->get_line_tax( $item );
$order->get_formatted_line_subtotal( $item, $tax_display = '' );
示例:获取订单的子项目
// 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();
$name = $item->get_name();
$quantity = $item->get_quantity();
$subtotal = $item->get_subtotal();
$total = $item->get_total();
$tax = $item->get_subtotal_tax();
$taxclass = $item->get_tax_class();
$taxstat = $item->get_tax_status();
$allmeta = $item->get_meta_data();
$somemeta = $item->get_meta( '_whatever', true );
$type = $item->get_type();
}
根据条件获取多个订单
用WC_Order_Query可以获取多个订单,每个订单都是一个WC_Order的实例,可以用上述方法操作。下面的代码会选出最近的10个订单。
$query = new WC_Order_Query( array(
'limit' => 10,
'orderby' => 'date',
'order' => 'DESC',
) );
$orders = $query->get_orders();
参考:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论