WooCommerce 文档

post_title: WooCommerce CLI examples sidebar_label: Examples sidebar_position: 3


WooCommerce CLI 示例

每个命令的完整文档可通过 --help 获取。以下是一些示例命令,展示 CLI 的功能。

以下所有示例都使用用户 ID 1(通常是管理员账户),但您应将其替换为您自己的用户账户。

您还可以通过查看 CLI 测试的测试文件 来查找其他示例(无输出)。

每个命令都有一个 .feature 文件。例如,这些是一些支付网关命令

清除产品/购物瞬态缓存

命令:

$ wp wc tool run clear_transients --user=1

响应:

成功:已更新系统状态工具 clear_transients。

列出所有系统工具

命令:

$ wp wc tool list --user=1

响应:

WooCommerce 工具箱

本工具箱提供了多种实用功能,用于管理和维护您的 WooCommerce 商店。

工具列表

以下是可用的工具列表:

+----------------------------+----------------------------------+-------------------------------+-----------------------------------------------------------------------------------+
| id                         | name                             | action                        | description                                                                       |
+----------------------------+----------------------------------+-------------------------------+-----------------------------------------------------------------------------------+
| clear_transients           | WC 瞬态对象                    | 清理瞬态对象              | 此工具将清理产品/商店的瞬态对象缓存。                           |
| clear_expired_transients   | 过期瞬态对象               | 清理过期瞬态对象      | 此工具将清理 WordPress 中的所有过期瞬态对象。                       |
| delete_orphaned_variations | 孤立的变体              | 删除孤立的变体    | 此工具将删除所有没有父对象的变体。                        |
| recount_terms              | 产品分类计数                      | 重新计算产品分类计数                 | 此工具将重新计算产品分类计数,当您以隐藏产品于目录的方式更改设置时非常有用。 |
|                            |                                  |                               |                                                                       |
| reset_roles                | 权限                     | 重置权限            | 此工具会将管理员、客户和商店管理员角色重置为默认值。 如果您的用户无法访问所有 WooCommerce 管理页面,请使用此功能。               |
|                            |                                  |                               |                                                                       |
| clear_sessions             | 客户会话                | 清理所有会话            | <strong class="red">注意:</strong> 此工具将删除所有客户会话数据,包括任何当前的购物车内容。                            |
|                            |                                  |                               |                                                                       |
| install_pages              | 安装 WooCommerce 页面        | 安装页面                 | <strong class="red">注意:</strong> 此工具将安装所有缺失的 WooCommerce 页面。 已经定义和设置的页面不会被替换。                |
|                            |                                  |                               |                                                                       |
| delete_taxes               | 删除所有 WooCommerce 税率 | 删除所有税率          | <strong class="red">注意:</strong> 此选项将删除所有税率,请谨慎使用。                                                                |
|                            |                                  |                               |                                                                       |
| reset_tracking             | 重置使用情况跟踪设置    | 重置使用情况跟踪设置 | 这将重置您的使用情况跟踪设置,导致它再次显示“选择加入”横幅,并且不会发送任何数据。                                                 |
|                            |                                  |                               |                                                                       |
+----------------------------+----------------------------------+-------------------------------+-----------------------------------------------------------------------------------+

工具说明:

使用方法:

  1. 选择您要使用的工具。
  2. 点击相应的按钮或链接。
  3. 根据提示完成操作。

注意事项:

常见问题:

更多信息:

术语表:

创建客户

命令:

$ wp wc customer create --email='woo@woo.local' --user=1 --billing='{"first_name":"Bob","last_name":"Tester","company":"Woo", "address_1": "123 Main St.", "city":"New York", "state:": "NY", "country":"USA"}' --shipping='{"first_name":"Bob","last_name":"Tester","company":"Woo", "address_1": "123 Main St.", "city":"New York", "state:": "NY", "country":"USA"}' --password='hunter2' --username='mrbob' --first_name='Bob' --last_name='Tester'

回复:

成功:已创建客户 17。

以 CSV 格式获取客户信息

命令:

$ wp wc customer get 17 --user=1 --format=csv

响应:

字段,值
id,17
date_created,2016-12-09T20:22:10
date_modified,2016-12-09T20:22:10
email,woo@woo.local
first_name,Bob
last_name,Tester
role,customer
username,mrbob
billing,"{""first_name"":""Bob"",""last_name"":""Tester"",""company"":""Woo"",""address_1"":""123 Main St."",""address_2"":"""",""city"":""New York"",""state"":"""",""postcode"":"""","
"country"":""USA"",""email"":"""",""phone"":""""}"
shipping,"{""first_name"":""Bob"",""last_name"":""Tester"",""company"":""Woo"",""address_1"":""123 Main St."",""address_2"":"""",""city"":""New York"",""state"":"""",""postcode"":"""",
""country"":""USA""}"
is_paying_customer,false
meta_data,"[{""id"":825,""key"":""shipping_company"",""value"":""Woo""},{""id"":829,""key"":""_order_count"",""value"":""0""},{""id"":830,""key"":""_money_spent"",""value"":""0""}]"
orders_count,0
total_spent,0.00
avatar_url,http://2.gravatar.com/avatar/5791d33f7d6472478c0b5fa69133f09a?s=96

在订单 355 上添加客户备注

命令:

$ wp wc order_note create 355 --note="Great repeat customer" --customer_note=true --user=1

回复:

Success: Created order_note 286.

获取订单备注

命令:

$ wp wc order_note get 355 286 --user=1

回复:

+---------------+-----------------------+
| Field         | Value                 |
+---------------+-----------------------+
| id            | 286                   |
| date_created  | 2016-12-09T20:27:26   |
| note          | Great repeat customer |
| customer_note | true                  |
+---------------+-----------------------+

更新优惠券

命令:

$ wp wc shop_coupon update 45 --amount='10' --discount_type='percent' --free_shipping=true --user=1

响应:

Success: Updated shop_coupon 45.

获取优惠券

命令:

$ wp wc shop_coupon get 45 --user=1

响应:

+-----------------------------+---------------------+
| 字段                        |                   |
+-----------------------------+---------------------+
| id                          | 45                  |
| code                        | hello               |
| amount                      | 10.00               |
| date_created                | 2016-08-09T17:37:28 |
| date_modified               | 2016-12-09T20:30:32 |
| discount_type               | percent             |
| description                 | Yay                 |
| date_expires                | 2016-10-22T00:00:00 |
| usage_count                 | 2                   |
| individual_use              | false               |
| product_ids                 | []                  |
| excluded_product_ids        | []                  |
| usage_limit                 | null                |
| usage_limit_per_user        | null                |
| limit_usage_to_x_items      | null                |
| free_shipping               | true                |
| product_categories          | []                  |
| excluded_product_categories | []                  |
| exclude_sale_items          | false               |
| minimum_amount              | 0.00                |
| maximum_amount              | 0.00                |
| email_restrictions          | []                  |
| used_by                     | ["1","1"]           |
| meta_data                   | []                  |
+-----------------------------+---------------------+

使用 WP CLI 升级数据库

当 WooCommerce 新版本发布时,通常会提示您运行数据库升级。虽然可以通过 WP 管理员界面执行此操作,但高级用户可能更倾向于通过 CLI 运行这些升级,例如,在可能发生超时的繁忙商店中。

要更新数据库,请使用以下 CLI 命令:

$ wp wc update

无需额外参数,且仅会运行待审的升级。更新运行完毕后,将显示新版本。