跳到主要内容

有用的核心函数

WooCommerce 核心函数在前端和后台都可用。 它们位于 includes/wc-core-functions.php 文件中,可以由主题和插件使用。

条件函数

WooCommerce 条件函数用于确定当前的查询/网页。

is_woocommerce

如果当前网页使用了 WooCommerce 模板,则返回 true(购物车和结账是标准网页,使用短代码,因此不包含在内)。

is_woocommerce()

is_shop

当浏览产品类型归档页面(商店)时,返回 true

is_shop()

is_product

当浏览单个产品时,返回 true

is_product()

优惠券函数

wc_get_coupon_code_by_id

根据优惠券 ID 获取优惠券代码。

wc_get_coupon_code_by_id( $id )

参数 $id 是优惠券 ID。

wc_get_coupon_id_by_code

根据代码获取优惠券 ID。

wc_get_coupon_id_by_code( $code, $exclude = 0 )

$code 是优惠券代码,$exclude 用于排除 ID(如果正在检查是否存在)。

用户函数

wc_customer_bought_product

检查客户是否购买了某个商品。 检查可以通过电子邮件或用户 ID 或两者同时进行。

wc_customer_bought_product( $customer_email, $user_id, $product_id )

wc_get_customer_total_spent

获取客户的总消费金额。

wc_get_customer_total_spent( $user_id )

$user_id 是客户的用户 ID。

wc_get_customer_order_count

获取客户的总订单数量。

wc_get_customer_order_count( $user_id )

$user_id 是客户的用户 ID。

格式化函数

wc_get_dimension

将 WooCommerce 尺寸单位的测量值 $dimension 转换为目标单位 $to_unit

wc_get_dimension( $dimension, $to_unit, $from_unit = '' )

示例用法:

wc_get_dimension( 55, 'in' );
wc_get_dimension( 55, 'in', 'm' );

wc_get_weight

将 WooCommerce 重量单位的重量 $weight 转换为目标重量单位 $to_unit

wc_get_weight( $weight, $to_unit, $from_unit = '' )

示例用法:

wc_get_weight( 55, 'kg' );
wc_get_weight( 55, 'kg', 'lbs' );

wc_clean

使用 sanitize_text_field 清理变量。 数组会递归地进行清理。 非标量值将被忽略。

wc_clean( $var )

wc_price

使用正确的十位小数位数和货币符号格式化传入的价格。

wc_price( $price, $args = array() )

$args 数组包含一个名为 ex_tax_label 的选项;如果为 true,则会附加一个“不含税”的信息。

订单函数

wc_get_orders

此函数是检索订单的标准方法,基于某些参数。 应该使用此函数来检索订单,以便在迁移到自定义表时,函数仍然有效。

wc_get_orders( $args )

参数和用法

wc_get_order

这是用于返回订单的主要函数,使用 WC_Order_Factory 类。

wc_get_order( $the_order = false )

the_order 参数可以是订单的文章对象或文章 ID。

wc_orders_count

返回特定订单状态的订单数量。

wc_orders_count( $status, string $type = '' )

根据给定的 $term 搜索订单。

wc_order_search( $term )

网页函数

wc_get_page_id

根据名称获取 WooCommerce 网页 ID,例如 "thankyou"。

wc_get_page_id( $page )

wc_get_endpoint_url

获取 $endpoint 的 URL,该 URL 会根据固定链接设置而变化。

wc_get_endpoint_url( $endpoint, $value = '', $permalink = '' )

产品函数

wc_get_products

此函数是基于某些参数检索产品的标准方法。

wc_get_products( $args )

参数和用法

wc_get_product

这是用于返回产品的核心函数。它使用 WC_Product_Factory 类。

wc_get_product( $the_product = false )

$the_product 参数可以是产品的文章对象或文章 ID。

wc_get_product_ids_on_sale

返回一个包含正在促销中的产品 ID 的数组。

wc_get_product_ids_on_sale()

返回一个包含精选产品 ID 的数组。

wc_get_featured_product_ids()

获取基于产品分类和标签的产品相关产品。

wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array() )

账户函数

wc_get_account_endpoint_url

获取账户端点 URL。

wc_get_account_endpoint_url( $endpoint )

属性函数

wc_get_attribute_taxonomies

获取产品属性的分类法。

wc_get_attribute_taxonomies()

wc_attribute_taxonomy_name

获取给定产品属性的分类法名称。

wc_attribute_taxonomy_name( $attribute_name )

wc_attribute_taxonomy_id_by_name

根据名称获取产品属性 ID。

wc_attribute_taxonomy_id_by_name( $name )

REST 函数

wc_rest_prepare_date_response

解析并格式化日期,符合 ISO8601/RFC3339 标准。

wc_rest_prepare_date_response( $date, $utc = true )

$utc 参数设置为 false 以获取本地/偏移时间。

wc_rest_upload_image_from_url

从给定的 URL 上传图像。

wc_rest_upload_image_from_url( $image_url )

wc_rest_urlencode_rfc3986

根据 RFC 3986 对 $value 进行编码。

wc_rest_urlencode_rfc3986( $value )

wc_rest_check_post_permissions

检查 REST API 上文章的权限。

wc_rest_check_post_permissions( $post_type, $context = 'read', $object_id = 0 )

$context 参数的可选值,表示请求的上下文,包括 read(已读)、create(创建)、edit(编辑)、delete(删除)和 batch(批量)。