跳到主要内容

短代码的钩子替代方案

以下是加载购物车/结账短代码和块时运行的钩子。其中一些是通用的 WooCommerce 生命周期钩子,另一些是特定于购物车和结账页面的钩子。这些是通过记录每次 do_actionapply_filters 的调用,并审查提到 "钩子" 和 "过滤器" 的旧 WooCommerce Blocks 仓库 中的问题而生成的。

术语表

图标描述
完全支持
🔶部分支持
不支持
未知

如果一个钩子被标记为未知 ❓,这意味着我们无法验证该钩子是否受支持,原因不明。 随着时间的推移,我们将尝试验证更多这些钩子。 目标是使所有表格中都不要再有任何具有未知 ❓ 状态的钩子。

WooCommerce 生命周期 操作

这些钩子在每个 WooCommerce 页面加载时运行,有些会影响块,有些不会,有些只部分影响它们。

旧钩子在块中有效?备注
woocommerce_load_cart_from_session完全支持 ✅
woocommerce_cart_loaded_from_session完全支持 ✅
woocommerce_set_cart_cookies完全支持 ✅
woocommerce_shipping_zone_loaded完全支持 ✅
woocommerce_check_cart_items完全支持 ✅建议使用 woocommerce_store_api_validate_cart_item
woocommerce_before_calculate_totals完全支持 ✅
woocommerce_shipping_init完全支持 ✅
woocommerce_load_shipping_methods完全支持 ✅
woocommerce_cart_calculate_fees完全支持 ✅
woocommerce_calculate_totals完全支持 ✅
woocommerce_after_calculate_totals完全支持 ✅
woocommerce_cart_updated完全支持 ✅
woocommerce_before_get_rates_for_package完全支持 ✅
woocommerce_after_get_rates_for_package完全支持 ✅
woocommerce_checkout_init完全支持 ✅
woocommerce_customer_loaded完全支持 ✅

WooCommerce 生命周期 筛选

本节描述了 WooCommerce 提供的各种生命周期事件,这些事件允许您通过 筛选 来修改 WooCommerce 的行为。

什么是 筛选

筛选 允许您在 WooCommerce 的特定事件发生时,修改其默认行为。 您可以将自定义函数连接到这些 筛选,以便在事件发生时执行您的代码。

如何使用 筛选

要使用 筛选,您需要:

  1. 找到您想要修改的事件的 筛选 名称。
  2. 使用 add_filter() 函数,将您的自定义函数连接到该 筛选
  3. 在您的自定义函数中,修改事件的默认值。

示例:

假设您想修改 WooCommerce 订单总价的计算方式。 您可以使用 woocommerce_order_total 筛选 来实现这一点。

<?php
add_filter( 'woocommerce_order_total', 'custom_order_total' );

function custom_order_total( $total ) {
// 在这里添加您的自定义逻辑来计算订单总价
$total = $total + 10; // 例如,增加 10 元

return $total;
}
?>

常用的 WooCommerce 筛选

以下是一些常用的 WooCommerce 筛选,以及它们所触发的事件:

  • woocommerce_checkout_order_process: 在用户完成结账时触发。
  • woocommerce_email_order_meta_fields: 在发送订单邮件时,允许您添加自定义元数据字段。
  • woocommerce_product_categories_tree_children_render: 用于自定义产品分类树的渲染方式。
  • woocommerce_product_thumbnail: 用于修改产品缩略图。
  • woocommerce_payment_complete: 在支付完成后触发。

更多信息:

有关 WooCommerce 筛选 的更多信息,请参阅 WooCommerce 的官方文档:https://woocommerce.com/document/developers/

重要提示:

  • 在使用 筛选 时,请务必小心,并确保您的自定义代码不会影响 WooCommerce 的正常运行。
  • 在修改任何 筛选 之前,请务必备份您的网站。
  • 如果您不确定如何使用 筛选,请咨询专业的 WooCommerce 开发人员。

示例:修改产品价格

以下示例展示了如何使用 woocommerce_price 筛选 来修改产品价格:

<?php
add_filter( 'woocommerce_price', 'custom_product_price', 10, 2 );

function custom_product_price( $price, $product ) {
// 仅对特定产品修改价格
if ( $product->get_id() == 123 ) { // 假设产品 ID 为 123
$price = $price * 0.9; // 降低 10%
}

return $price;
}
?>

示例:修改产品描述

以下示例展示了如何使用 woocommerce_product_description 筛选 来修改产品描述:

<?php
add_filter( 'woocommerce_product_description', 'custom_product_description', 10, 1 );

function custom_product_description( $description ) {
// 在产品描述的开头添加一段自定义文本
$custom_text = '<p>这是一段自定义文本,将在产品描述的开头显示。</p>';

return $custom_text . $description;
}
?>

示例:修改产品图片大小

以下示例展示了如何使用 woocommerce_product_image_src 筛选 来修改产品图片大小:

<?php
add_filter( 'woocommerce_product_image_src', 'custom_product_image_size', 10, 3 );

function custom_product_image_size( $image_src, $size, $product ) {
// 仅对特定产品修改图片大小
if ( $product->get_id() == 456 ) { // 假设产品 ID 为 456
$image_src = str_replace( array( 'woocommerce-small', 'woocommerce-medium', 'woocommerce-large' ), 'woocommerce-large', $image_src ); // 强制使用 large 尺寸
}

return $image_src;
}
?>

希望这些示例能够帮助您更好地理解如何使用 WooCommerce 筛选。 请记住,在使用 筛选 时,请务必小心

Old hookWorks in blocks?Notes
woocommerce_notice_typesUnknown ❓WC Blocks does not handle additional notice types outside of the core ones. Non-supported notices would be displayed as "info" notices.
woocommerce_kses_notice_allowed_tagsUnknown ❓
woocommerce_product_get_stock_statusFully supported ✅
woocommerce_product_is_in_stockFully supported ✅
woocommerce_product_get_manage_stockUnknown ❓
woocommerce_product_get_tax_classFully supported ✅
woocommerce_product_get_tax_statusUnknown ❓
woocommerce_prices_include_taxUnknown ❓
woocommerce_apply_base_tax_for_local_pickupUnknown ❓
woocommerce_local_pickup_methodsNot supported ❌Does not affect the blocks-based local pickup methods
woocommerce_customer_get_shipping_postcodeFully supported ✅
woocommerce_customer_get_shipping_cityFully supported ✅
woocommerce_customer_taxable_addressUnknown ❓
woocommerce_shipping_methodsFully supported ✅
woocommerce_format_localized_priceUnknown ❓
woocommerce_shipping_local_pickup_optionNot supported ❌Does not affect the blocks-based local pickup methods
woocommerce_shipping_pickup_location_optionUnknown ❓Unsure if changing this changes the way local pickup shows in the Cart/Checkout
woocommerce_shipping_method_supportsFully supported ✅
woocommerce_get_tax_locationUnknown ❓
woocommerce_format_postcodeUnknown ❓
woocommerce_matched_tax_ratesUnknown ❓
woocommerce_find_ratesUnknown ❓
woocommerce_matched_ratesUnknown ❓
woocommerce_cart_totals_get_item_tax_ratesFully supported ✅
woocommerce_adjust_non_base_location_pricesUnknown ❓
woocommerce_product_is_taxableFully supported ✅
woocommerce_price_ex_tax_amountFully supported ✅
woocommerce_tax_roundFully supported ✅
woocommerce_calc_taxFully supported ✅
woocommerce_calculate_item_totals_taxesFully supported ✅
woocommerce_cart_ready_to_calc_shippingFully supported ✅
woocommerce_product_get_virtualFully supported ✅
woocommerce_is_virtualFully supported ✅
woocommerce_product_needs_shippingFully supported ✅
woocommerce_cart_needs_shippingFully supported ✅
woocommerce_customer_get_shipping_address_1Fully supported ✅
woocommerce_customer_get_shipping_address_2Fully supported ✅
woocommerce_cart_display_prices_including_taxFully supported ✅
woocommerce_cart_get_subtotalFully supported ✅
woocommerce_cart_shipping_packagesFully supported ✅
woocommerce_product_get_shipping_class_idFully supported ✅
woocommerce_countries_shipping_countriesFully supported ✅
woocommerce_get_zone_criteriaFully supported ✅
woocommerce_shipping_zone_shipping_methodsFully supported ✅
woocommerce_shipping_free_shipping_is_availableUnknown ❓
woocommerce_product_get_nameFully supported ✅
woocommerce_shipping_method_add_rateFully supported ✅
woocommerce_shipping_flat_rate_is_availableFully supported ✅
woocommerce_evaluate_shipping_cost_argsFully supported ✅
woocommerce_calc_shipping_taxFully supported ✅
woocommerce_localisation_address_formatsFully supported ✅
woocommerce_countries_base_countryUnknown ❓
woocommerce_formatted_address_force_country_displayUnknown ❓
woocommerce_statesFully supported ✅
woocommerce_formatted_address_replacementsUnknown ❓
woocommerce_package_ratesFully supported ✅
woocommerce_shipping_packagesFully supported ✅
woocommerce_shipping_rate_method_idFully supported ✅
woocommerce_shipping_rate_taxesFully supported ✅
woocommerce_shipping_rate_costFully supported ✅
woocommerce_cart_totals_get_fees_from_cart_taxesFully supported ✅
woocommerce_calculated_totalNot supported ❌This does not seem to have any effect
woocommerce_cart_get_discount_totalFully supported ✅
woocommerce_cart_get_cart_contents_totalFully supported ✅
woocommerce_get_price_excluding_taxNot supported ❌This does not seem to have any effect
raw_woocommerce_priceNot supported ❌This does not seem to have any effect
formatted_woocommerce_priceNot supported ❌This does not seem to have any effect
woocommerce_price_trim_zerosNot supported ❌This does not seem to have any effect
woocommerce_get_cart_page_permalinkNot supported ❌This does not seem to have any effect
woocommerce_get_cart_urlNot supported ❌This does not seem to have any effect
woocommerce_checkout_registration_enabledFully supported ✅This does not seem to have any effect
woocommerce_get_checkout_page_permalinkNot supported ❌This does not seem to have any effect
woocommerce_get_checkout_urlNot supported ❌This does not seem to have any effect
woocommerce_checkout_get_valueNot supported ❌This does not seem to have any effect
woocommerce_default_address_fieldsNot supported ❌This does not seem to have any effect
default_checkout_billing_countryNot supported ❌This does not seem to have any effect
default_checkout_shipping_countryNot supported ❌This does not seem to have any effect
woocommerce_get_country_localeFully supported ✅
woocommerce_get_country_locale_defaultUnknown ❓
woocommerce_get_country_locale_baseUnknown ❓
woocommerce_billing_fieldsPartially supported 🔶Editing core fields is not supported, but adding them is via Additional Checkout Fields API
woocommerce_shipping_fieldsPartially supported 🔶Editing core fields is not supported, but adding them is via Additional Checkout Fields API
woocommerce_checkout_fieldsPartially supported 🔶Editing core fields is not supported, but adding them is via Additional Checkout Fields API
woocommerce_cart_item_productNot supported ❌Modifying individual cart items is not possible
woocommerce_payment_gateway_supportsFully supported ✅
woocommerce_customer_get_billing_first_nameFully supported ✅
woocommerce_customer_get_billing_last_nameFully supported ✅
woocommerce_customer_get_billing_companyFully supported ✅
woocommerce_customer_get_billing_address_1Fully supported ✅
woocommerce_customer_get_billing_address_2Fully supported ✅
woocommerce_customer_get_billing_cityFully supported ✅
woocommerce_customer_get_billing_postcodeFully supported ✅
woocommerce_customer_get_billing_phoneFully supported ✅
woocommerce_customer_get_shipping_first_nameFully supported ✅
woocommerce_customer_get_shipping_last_nameFully supported ✅
woocommerce_customer_get_shipping_companyFully supported ✅
woocommerce_get_item_dataPartially supported 🔶Checkout blocks strip most HTML from metadata, allowing only a, b, em, i, strong, br, abbr, and span.
woocommerce_cart_get_subtotal_taxNot supported ❌This does not seem to have any effect
woocommerce_shipping_package_nameFully supported ✅
woocommerce_shipping_rate_idUnknown ❓
woocommerce_shipping_rate_labelFully supported ✅
woocommerce_cart_get_shipping_taxesFully supported ✅
woocommerce_cart_get_fee_taxesFully supported ✅
woocommerce_cart_get_taxesFully supported ✅
woocommerce_rate_codeUnknown ❓
woocommerce_rate_compoundUnknown ❓
woocommerce_rate_labelFully supported ✅
woocommerce_cart_hide_zero_taxesUnknown ❓
woocommerce_cart_tax_totalsFully supported ✅
woocommerce_cart_needs_paymentFully supported ✅
woocommerce_order_classFully supported ✅
woocommerce_checkout_registration_requiredUnknown ❓
woocommerce_privacy_policy_page_idFully supported ✅
woocommerce_get_terms_page_idUnknown ❓
woocommerce_terms_and_conditions_page_idUnknown ❓
woocommerce_cart_contents_countUnknown ❓
woocommerce_country_locale_field_selectorsNot supported ❌
woocommerce_get_return_urlFully supported ✅
woocommerce_cart_hashFully supported ✅
woocommerce_cart_get_fee_taxFully supported ✅
woocommerce_customer_default_location_arrayFully supported ✅
woocommerce_countriesFully supported ✅
woocommerce_sort_countriesNot supported ❌This does not seem to have any effect
woocommerce_countries_allowed_countriesFully supported ✅
woocommerce_customer_default_location_arrayFully supported ✅
woocommerce_customer_get_billing_countryFully supported ✅
woocommerce_customer_get_shipping_countryFully supported ✅
woocommerce_customer_get_billing_stateFully supported ✅
woocommerce_customer_get_shipping_stateFully supported ✅
woocommerce_customer_get_billing_emailFully supported ✅
woocommerce_cart_session_initializeFully supported ✅
woocommerce_get_checkout_page_idFully supported ✅
woocommerce_get_cart_page_idFully supported ✅
woocommerce_is_checkoutFully supported ✅
woocommerce_currencyFully supported ✅
woocommerce_currency_symbolsFully supported ✅
woocommerce_currency_symbolFully supported ✅
woocommerce_price_formatFully supported ✅
woocommerce_coupons_enabledFully supported ✅
woocommerce_get_shop_page_idFully supported ✅
current_theme_supports-woocommerceFully supported ✅
woocommerce_payment_gatewaysPartially supported 🔶Integration with WC Blocks is still required, beyond unsetting gateways, manipulating the payment gateways here may not work in the Cart and Checkout blocks
woocommerce_get_base_locationFully supported ✅
woocommerce_gateway_iconNot supported ❌This hook has no effect since icons are not displayed.
woocommerce_get_image_size_thumbnailFully supported ✅
woocommerce_get_image_size_singleFully supported ✅
woocommerce_product_stock_status_optionsFully supported ✅
woocommerce_cart_item_nameNot supported ❌Use the itemName checkout filter.
woocommerce_product_get_statusFully supported ✅
woocommerce_product_get_priceFully supported ✅
woocommerce_is_purchasableFully supported ✅
woocommerce_cart_item_is_purchasableFully supported ✅
woocommerce_cart_item_data_to_validateFully supported ✅
woocommerce_get_cart_item_from_sessionFully supported ✅
woocommerce_cart_contents_changedFully supported ✅
woocommerce_get_cart_contentsFully supported ✅
woocommerce_stock_amountFully supported ✅
woocommerce_cart_item_remove_linkNot supported ❌Use the showRemoveItemLink checkout filter.
woocommerce_cart_item_quantityNot supported ❌This is possible by modifying the quantity_limits property of the cart item in the woocommerce_store_api_product_quantity_{$value_type} filter.
woocommerce_product_get_imageNot supported ❌Use woocommerce_store_api_cart_item_images (PR Link with example.)
woocommerce_cart_no_shipping_available_htmlNot supported ❌This is not editable
woocommerce_available_payment_gatewaysPartially supported 🔶Integration with WC Blocks is still required, beyond unsetting gateways, manipulating the payment gateways here may not work in the Cart and Checkout blocks
woocommerce_cart_get_totalFully supported ✅
woocommerce_cart_get_fee_taxFully supported ✅
woocommerce_cart_get_cart_contents_taxFully supported ✅
woocommerce_cart_get_shipping_taxFully supported ✅
woocommerce_cart_get_shipping_totalFully supported ✅

购物车 操作

Add item to cart

将商品添加到购物车。

View cart

查看购物车。

Remove item from cart

从购物车中移除商品。

Update item quantity in cart

更新购物车中商品的数量。

Clear cart

清空购物车。

Checkout

结账。

Example:

// Example code
function addItemToCart(item) {
// Add item to the cart
}

Explanation:

  • AddItemToCart 操作用于将商品添加到购物车。
  • ViewCart 操作用于查看购物车中的商品。
  • RemoveItemFromCart 操作用于从购物车中移除商品。
  • UpdateItemQuantityInCart 操作用于更新购物车中商品的数量。
  • ClearCart 操作用于清空购物车。
  • Checkout 操作用于完成购买流程。

Note:

  • %s 是一个占位符,可以替换为任何字符串。
  • %d 是一个占位符,可以替换为任何整数。
  • {name} 是一个占位符,可以替换为任何名称。
  • on 表示设备或功能处于开启状态。

规则:

  1. 翻译所有英文文本,包括标题、段落、列表项
  2. 保持 Markdown 格式不变(#、**、``、 等)
  3. 代码块(...)内的代码不翻译,只翻译代码块外的文本
  4. 保留所有占位符(%s, %d, {name})和 HTML 标签
  5. 保留所有 URL 链接地址不翻译
  6. 函数名、变量名、命令名保留英文原文
  7. 只输出翻译结果,不要输出解释或原文

术语表(必须使用以下译法):

  • Available → 可用
  • Discount → 折扣
  • Position → 位置
  • Quantity → 数量
  • Contents → 内容
  • manually → 手动
  • Shipping → 配送
  • Checkout → 结账
  • Actions → 操作
  • Orders → 订单
  • Button → 按钮
  • Before → 前
  • Manual → 手册
  • Coupon → 优惠券
  • Hooks → 钩子
  • Close → 关闭
  • Field → 字段
  • After → 后
  • Input → 输入
  • check → 检查
Old hookWorks in blocks?Notes
woocommerce_before_cartNot supported ❌没有特定的等效项,也没有可用的 Slot/fill 位置。 也许向 "购物车商品" 块添加一个内部块可以实现,但必须由商家手动进行位置调整。 也可以使用 render_block_{$name} 过滤器,通过 PHP 在前后添加块。
woocommerce_before_cart_tableNot supported ❌没有特定的等效项,也没有可用的 Slot/fill 位置。 也许向 "购物车商品" 块添加一个内部块可以实现,但必须由商家手动进行位置调整。
woocommerce_before_cart_contentsNot supported ❌没有特定的等效项,也没有可用的 Slot/fill 位置。 也许向 "购物车商品" 块添加一个内部块可以实现,但必须由商家手动进行位置调整。
woocommerce_after_cart_item_nameNot supported ❌使用 itemName 结账过滤器
woocommerce_before_quantity_input_fieldNot supported ❌目前没有等效项。
woocommerce_after_quantity_input_fieldNot supported ❌目前没有等效项。
woocommerce_cart_contentsNot supported ❌没有特定的等效项,也没有可用的 Slot/fill 位置。 也许向 "购物车商品" 块添加一个内部块可以实现,但必须由商家手动进行位置调整。
woocommerce_cart_couponNot supported ❌ExperimentalDiscountsMeta slot/fill
woocommerce_cart_actionsNot supported ❌ExperimentalOrderMeta slot/fill
woocommerce_after_cart_contentsNot supported ❌没有特定的等效项,也没有可用的 Slot/fill 位置。 也许向 "购物车商品" 块添加一个内部块可以实现,但必须由商家手动进行位置调整。
woocommerce_after_cart_tableNot supported ❌没有特定的等效项,也没有可用的 Slot/fill 位置。 也许向 "购物车商品" 块添加一个内部块可以实现,但必须由商家手动进行位置调整。
woocommerce_before_cart_collateralsNot supported ❌没有特定的等效项,也没有可用的 Slot/fill 位置。 也许向 "购物车商品" 块添加一个内部块可以实现,但必须由商家手动进行位置调整。
woocommerce_cart_collateralsNot supported ❌没有特定的等效项,也没有可用的 Slot/fill 位置。 也许向 "购物车商品" 块添加一个内部块可以实现,但必须由商家手动进行位置调整。
woocommerce_before_cart_totalsNot supported ❌ExperimentalOrderMeta slot/fill
woocommerce_cart_totals_before_shippingNot supported ❌ExperimentalOrderShippingPackages
woocommerce_after_shipping_rateNot supported ❌ExperimentalOrderShippingPackages
woocommerce_before_shipping_calculatorNot supported ❌没有特定的等效项。 最接近的是 ExperimentalOrderShippingPackages
woocommerce_after_shipping_calculatorNot supported ❌没有特定的等效项。 最接近的是 ExperimentalOrderShippingPackages
woocommerce_cart_totals_after_shippingNot supported ❌没有特定的等效项。 最接近的是 ExperimentalOrderShippingPackages
woocommerce_cart_totals_before_order_totalNot supported ❌没有特定的等效项,但我们有总价部分的过滤器。
woocommerce_cart_totals_after_order_totalNot supported ❌没有特定的等效项,但我们有总价部分的过滤器。
woocommerce_proceed_to_checkoutNot supported ❌没有特定的等效项,但结账和“提交订单”按钮的过滤器可能有效。
woocommerce_after_cart_totalsNot supported ❌ExperimentalOrderMeta slot/fill
woocommerce_after_cartNot supported ❌没有特定的等效项,也没有可用的 Slot/fill 位置。 也许向 "购物车商品" 块添加一个内部块可以实现,但必须由商家手动进行位置调整。

购物车 筛选

本节描述如何使用 筛选 功能来管理购物车中的商品。

购物车筛选功能

购物车筛选功能允许用户根据各种条件过滤购物车中的商品,例如:

  • 商品类型: 可以根据商品类型进行筛选,例如:服装、电子产品、书籍等。
  • 价格范围: 可以根据价格范围进行筛选,例如:低于 100 元、100-200 元、高于 200 元等。
  • 品牌: 可以根据品牌进行筛选,例如:Nike、Adidas、Apple 等。
  • 库存: 可以根据库存数量进行筛选,例如:仅显示有库存的商品。

使用方法

要使用购物车筛选功能,请按照以下步骤操作:

  1. 打开购物车页面。
  2. 点击“筛选”按钮。
  3. 在弹出的筛选窗口中,选择要应用的筛选条件。
  4. 点击“应用”按钮。

示例

以下是一个使用购物车筛选功能的示例:

假设您想筛选购物车中所有价格低于 100 元的服装商品。

  1. 打开购物车页面。
  2. 点击“筛选”按钮。
  3. 在筛选窗口中,选择“商品类型”为“服装”,并选择“价格范围”为“低于 100 元”。
  4. 点击“应用”按钮。

购物车页面将只显示价格低于 100 元的服装商品。

高级用法

购物车筛选功能还支持一些高级用法,例如:

  • 多条件筛选: 可以同时应用多个筛选条件。
  • 自定义筛选条件: 可以根据需要自定义筛选条件。

有关高级用法的更多信息,请参考相关文档。

代码示例

以下是一个使用 Python 语言实现购物车筛选功能的示例代码:

def filter_cart(cart, filters):
"""
Filters a cart based on the given filters.

Args:
cart: A list of items in the cart.
filters: A dictionary of filters.

Returns:
A list of items in the cart that match the filters.
"""
filtered_cart = []
for item in cart:
match = True
for key, value in filters.items():
if key == "type" and item["type"] != value:
match = False
elif key == "price" and item["price"] > value:
match = False
if match:
filtered_cart.append(item)
return filtered_cart

# Example usage
cart = [
{"name": "T-shirt", "type": "clothing", "price": 20},
{"name": "Jeans", "type": "clothing", "price": 50},
{"name": "Laptop", "type": "electronics", "price": 1000},
]

filters = {"type": "clothing", "price": 50}

filtered_cart = filter_cart(cart, filters)

print(filtered_cart)

错误处理

如果在应用筛选功能时出现错误,请检查以下事项:

  • 确保已正确配置筛选条件。
  • 确保购物车中存在符合筛选条件的商品。
  • 如果问题仍然存在,请联系技术支持。

相关链接

希望本节内容对您有所帮助。如有任何疑问,请随时联系我们。

规则说明

本文档提供了关于 WooCommerce 块的 Hook 支持情况说明。

旧的 Hook是否支持块?备注
woocommerce_cart_item_product_id不支持 ❌似乎没有任何效果。
woocommerce_cart_item_visible不支持 ❌似乎没有任何效果。
woocommerce_get_remove_url不支持 ❌购物车块中的商品移除操作是异步处理的。
woocommerce_cart_item_remove_link不支持 ❌购物车块中的商品移除操作是异步处理的。
woocommerce_cart_item_thumbnail不支持 ❌这种方式修改缩略图不受支持。 请参阅 woocommerce_store_api_cart_item_images (PR Link 附带示例)。
woocommerce_cart_product_price不支持 ❌
woocommerce_cart_item_price不支持 ❌
woocommerce_quantity_input_classes不支持 ❌
woocommerce_quantity_input_max不支持 ❌可以通过修改购物车项的 quantity_limits 属性,在 woocommerce_store_api_product_quantity_{$value_type} 过滤器中实现。
woocommerce_quantity_input_min不支持 ❌可以通过修改购物车项的 quantity_limits 属性,在 woocommerce_store_api_product_quantity_{$value_type} 过滤器中实现。
woocommerce_quantity_input_step不支持 ❌可以通过修改购物车项的 quantity_limits 属性,在 woocommerce_store_api_product_quantity_{$value_type} 过滤器中实现。
woocommerce_quantity_input_pattern不支持 ❌可以通过修改购物车项的 quantity_limits 属性,在 woocommerce_store_api_product_quantity_{$value_type} 过滤器中实现。
woocommerce_quantity_input_inputmode不支持 ❌可以通过修改购物车项的 quantity_limits 属性,在 woocommerce_store_api_product_quantity_{$value_type} 过滤器中实现。
woocommerce_quantity_input_placeholder不支持 ❌可以通过修改购物车项的 quantity_limits 属性,在 woocommerce_store_api_product_quantity_{$value_type} 过滤器中实现。
woocommerce_quantity_input_autocomplete不支持 ❌可以通过修改购物车项的 quantity_limits 属性,在 woocommerce_store_api_product_quantity_{$value_type} 过滤器中实现。
woocommerce_quantity_input_args不支持 ❌可以通过修改购物车项的 quantity_limits 属性,在 woocommerce_store_api_product_quantity_{$value_type} 过滤器中实现。
woocommerce_quantity_input_type不支持 ❌可以通过修改购物车项的 quantity_limits 属性,在 woocommerce_store_api_product_quantity_{$value_type} 过滤器中实现。
woocommerce_cart_item_quantity不支持 ❌可以通过修改购物车项的 quantity_limits 属性,在 woocommerce_store_api_product_quantity_{$value_type} 过滤器中实现。
woocommerce_cart_product_subtotal不支持 ❌
woocommerce_cart_item_subtotal不支持 ❌
woocommerce_cross_sells_columns不支持 ❌推荐商品以内部块的形式呈现。
woocommerce_cross_sells_orderby不支持 ❌推荐商品以内部块的形式呈现。
woocommerce_cross_sells_order不支持 ❌推荐商品以内部块的形式呈现。
woocommerce_cross_sells_total不支持 ❌推荐商品以内部块的形式呈现。
woocommerce_product_cross_sells_products_heading不支持 ❌可以在编辑器中进行修改。
woocommerce_is_downloadable不支持 ❌在购物车/结账块中似乎没有任何效果。
woocommerce_loop_product_link不支持 ❌在购物车块中,修改产品链接不受支持。
woocommerce_product_loop_title_classes不支持 ❌在购物车块中,修改产品标题的类名不受支持。
woocommerce_product_add_to_cart_aria_describedby不支持 ❌在购物车块中,修改产品 aria-describedby 属性不受支持。
woocommerce_sale_flash不支持 ❌可以使用 saleBadgePriceFormat 结账过滤器 进行修改。
woocommerce_cart_subtotal不支持 ❌在购物车/结账块中,修改购物车小计的显示不受支持。
oocommerce_shipping_package_details_array不支持 ❌Hook 在购物车块渲染过程中不会被触发。
woocommerce_shipping_show_shipping_calculator不支持 ❌仅在渲染购物车模板时使用,而块不使用模板。
woocommerce_cart_shipping_method_full_label不支持 ❌Hook 在购物车/结账块中无效。
woocommerce_get_shipping_tax不支持 ❌此过滤器在购物车/结账块中未使用。
woocommerce_shipping_calculator_enable_country不支持 ❌此过滤器在购物车/结账块中未使用。
woocommerce_shipping_calculator_enable_state不支持 ❌此过滤器在购物车/结账块中未使用。
woocommerce_shipping_calculator_enable_city不支持 ❌此过滤器在购物车/结账块中未使用。
woocommerce_shipping_calculator_enable_postcode不支持 ❌此过滤器在购物车/结账块中未使用。
woocommerce_cart_totals_fee_html不支持 ❌在购物车/结账块中,修改费用显示不受支持。
woocommerce_countries_estimated_for_prefix不支持 ❌
woocommerce_cart_total不支持 ❌在购物车/结账块中,使用此 Hook 修改购物车总额不受支持。
woocommerce_cart_totals_order_total_html不支持 ❌在购物车/结账块中,使用此 Hook 修改购物车总额不受支持。

术语表:

  • Link: 链接
  • Hook: 钩子
  • API: API
  • Placeholder: 占位符
  • Rendering: 渲染
  • Templates: 模板
  • Download: 下载
  • Quantity: 数量
  • Subtotal: 小计
  • Property: 属性
  • Shipping: 配送
  • Checkout: 结账
  • Details: 详情
  • Display: 显示
  • Heading: 标题
  • Product: 产品
  • Enable: 启用
  • Remove: 移除
  • Images: 图像
  • Format: 格式

结账 操作

本节描述与结账相关的操作。

重要提示: 在执行以下操作之前,请确保您已正确配置您的 Git 环境。

以下是一些常见的结账操作:

  • git checkout <commit>: 切换到指定的提交。
  • git checkout <branch>: 切换到指定的分支。
  • git checkout -b <new_branch>: 创建一个新的分支并切换到该分支。
  • git checkout -- <file>: 取消对指定文件的修改,恢复到暂存区或 HEAD 的版本。
  • git checkout .: 取消所有文件的修改,恢复到暂存区或 HEAD 的版本。

示例:

git checkout commit_id

说明:

  • <commit>: 要切换到的提交的哈希值或引用。
  • <branch>: 要切换到的分支的名称。
  • <new_branch>: 要创建的新分支的名称。
  • <file>: 要取消修改的文件名。

警告:

  • 结账操作可能会覆盖您本地的未提交更改。 在执行结账操作之前,请务必备份您的工作。
  • 如果您的工作目录中有未提交的更改,并且您尝试切换到另一个分支,Git 可能会阻止您。 您可以使用 git stash 命令来暂存您的更改,然后执行结账操作。

更多信息:

有关 git checkout 命令的更多信息,请参阅 git help checkout

git checkout 命令的常用选项:

  • -n--no-ornament:不要在输出中显示装饰。
  • --detach:创建一个新的分支,但不要自动切换到该分支。
  • -f--force:强制执行结账操作,即使存在冲突。

git checkout 命令的常见错误:

  • error: Your local changes to the following files would be overwritten by checkout:: 您的本地更改会覆盖以下文件。
  • error: Could not checkout '<branch>'. Branch '<branch>' does not exist.: 无法切换到 ''。 分支 '' 不存在。

git checkout 命令的替代方案:

  • git switch:一个更现代的切换分支的命令。
  • git reset:一个用于重置工作目录和暂存区的命令。

总结:

git checkout 是一个强大的命令,可以用于在不同的提交、分支和工作目录状态之间进行切换。 但是,在使用 git checkout 命令时,请务必小心,因为它可能会覆盖您本地的未提交更改。

钩子兼容性列表

本表列出了 WooCommerce 钩子及其在 Cart/Checkout 块中的兼容性。

旧钩子是否支持块?备注
woocommerce_before_checkout_form_cart_notices不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_before_checkout_form不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_checkout_before_customer_details不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_checkout_billing不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_before_checkout_billing_form不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_after_checkout_billing_form不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_checkout_shipping不支持 ❌由于位置限制,没有直接对应的功能,但是 ExperimentalOrderShippingPackages 也许可以实现,或者可以在 "配送" 块中添加一个内部块。
woocommerce_before_checkout_shipping_form不支持 ❌由于位置限制,没有直接对应的功能,但是 ExperimentalOrderShippingPackages 也许可以实现,或者可以在 "配送" 块中添加一个内部块。
woocommerce_after_checkout_shipping_form不支持 ❌由于位置限制,没有直接对应的功能,但是 ExperimentalOrderShippingPackages 也许可以实现,或者可以在 "配送" 块中添加一个内部块。
woocommerce_before_order_notes不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_after_order_notes不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_checkout_after_customer_details不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_checkout_before_order_review_heading不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账订单摘要" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_checkout_before_order_review不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账订单摘要" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_checkout_order_review不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账订单摘要" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_review_order_before_cart_contents不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账订单摘要" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_review_order_after_cart_contents不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账订单摘要" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_review_order_before_shipping不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账订单摘要" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_after_shipping_rate不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账订单摘要" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_review_order_after_shipping不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账订单摘要" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_review_order_before_order_total不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账订单摘要" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_review_order_after_order_total不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账订单摘要" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_review_order_before_payment不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块或 "支付" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_checkout_before_terms_and_conditions不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_checkout_terms_and_conditions不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_checkout_after_terms_and_conditions不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_review_order_before_submit不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_review_order_after_submit不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_review_order_after_payment不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块或 "支付" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_checkout_after_order_review不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账订单摘要" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_after_checkout_form不支持 ❌没有直接对应的功能,也没有可用的 Slot/fill 区域。 也许可以在 "结账字段" 块中添加一个内部块来实现,但位置需要由商家手动调整。
woocommerce_checkout_update_order_review不支持 ❌此类 AJAX 事件在使用 Cart/Checkout 块时不会发生。

结账 筛选器

本节描述如何使用 筛选器 进行结账操作。

重要提示: 确保已正确配置您的系统,以便支持 筛选器

以下是一些常见的结账流程:

  1. 准备商品: 确保所有需要结账的商品已添加到购物车。

  2. 应用筛选器: 使用 apply_filter() 函数应用所需的 筛选器。例如:

    def apply_filter(filter_name, data):
    # 应用筛选器逻辑
    return data
  3. 计算总价: 使用 calculate_total() 函数计算最终价格。

    def calculate_total(items, discounts):
    # 计算总价逻辑
    return total_price
  4. 确认订单: 确认订单信息,包括商品列表、总价和收货地址。

  5. 支付: 选择支付方式并完成支付。

  6. 结账: 完成结账流程。

示例:

# 示例代码
items = [
{"name": "商品A", "price": 10},
{"name": "商品B", "price": 20}
]
discounts = [
{"type": "优惠券", "amount": 5}
]

total_price = calculate_total(items, discounts)
print(f"总价: {total_price}")

常见问题:

  • 如何添加新的 筛选器 请参考 筛选器 开发文档。
  • 为什么我的 筛选器 没有生效? 请检查 筛选器 的配置是否正确,以及是否已正确应用。
  • 如何调试 筛选器 可以使用 debug_filter() 函数进行调试。

更多信息:

请使用 check 功能检查系统状态。

钩子支持状态

旧钩子是否支持块?备注
woocommerce_add_notice部分支持 🔶这些通知仅在“购物车”/“结账”块的页面加载时添加。如果在 API 请求期间发生的通知会被存储并在下一次完整的页面加载时显示。
woocommerce_checkout_coupon_message不支持 ❌无法使用此过滤器自定义优惠券消息。
woocommerce_form_field_args不支持 ❌尚未支持修改核心表单字段。
woocommerce_form_field_text不支持 ❌尚未支持修改核心表单字段。
woocommerce_form_field不支持 ❌尚未支持修改核心表单字段。
woocommerce_form_field_country不支持 ❌尚未支持修改核心表单字段。
woocommerce_form_field_state不支持 ❌尚未支持修改核心表单字段。
woocommerce_form_field_tel不支持 ❌尚未支持修改核心表单字段。
woocommerce_form_field_email不支持 ❌尚未支持修改核心表单字段。
woocommerce_cart_needs_shipping_address不支持 ❌应使用 wc/store/cart 数据存储 来控制此项。
woocommerce_ship_to_different_address_checked不支持 ❌应使用 wc/store/checkout 数据存储 来检查是否勾选了此复选框。
woocommerce_enable_order_notes_field不支持 ❌此块必须在编辑器中删除,此过滤器不会影响其在“结账”块中的显示。
woocommerce_form_field_textarea不支持 ❌尚未支持修改核心表单字段。
woocommerce_checkout_cart_item_visible不支持 ❌无法从“购物车”/“结账”块的订单摘要中过滤购物车项目。
woocommerce_cart_item_class不支持 ❌无法向“购物车”/“结账”块的订单摘要中的特定购物车项目添加类。
woocommerce_checkout_cart_item_quantity不支持 ❌无法修改“购物车”/“结账”块的订单摘要中购物车项目的数量显示。
woocommerce_cart_product_subtotal不支持 ❌不支持在“购物车”/“结账”块中修改产品小计的显示。
woocommerce_cart_item_subtotal不支持 ❌不支持在“购物车”/“结账”块中修改产品小计的显示。
woocommerce_cart_subtotal不支持 ❌不支持在“购物车”/“结账”块中修改购物车小计的显示。
woocommerce_cart_shipping_method_full_label不支持 ❌不支持在“购物车”/“结账”块中修改配送方式的显示。
woocommerce_get_shipping_tax不支持 ❌此过滤器不用于“购物车”/“结账”块。
woocommerce_cart_totals_fee_html不支持 ❌不支持在“购物车”/“结账”块中修改费用显示。
woocommerce_cart_total不支持 ❌不支持使用此钩子修改“购物车”/“结账”块中的购物车总计。
woocommerce_cart_totals_order_total_html不支持 ❌不支持使用此钩子修改“购物车”/“结账”块中的订单总计 HTML - 请在编辑器中进行修改。
woocommerce_order_button_text不支持 ❌不支持使用此钩子修改“购物车”/“结账”块中的订单按钮 HTML - 请在编辑器中进行修改。
woocommerce_gateway_title不支持 ❌这些内容可以包含在与“购物车”/“结账”块注册的网关渲染的“内容”中,但无法通过 PHP 过滤器控制此项。
woocommerce_gateway_icon不支持 ❌不显示网关图标。 它们可以包含在与“购物车”/“结账”块注册的网关渲染的“内容”中,但无法通过 PHP 过滤器控制此项。
woocommerce_gateway_description不支持 ❌无法通过这种方式更改网关,网关需要使用 JavaScript 与“购物车”/“结账”块注册,并且第三方扩展无法修改这些前端详细信息。
woocommerce_checkout_show_terms不支持 ❌此项无法通过此过滤器控制。 可以在编辑器中删除此块。
woocommerce_get_privacy_policy_text不支持 ❌不支持使用此过滤器修改隐私政策文本。 可以在编辑器中进行修改。
woocommerce_order_button_html不支持 ❌不支持使用此钩子修改“购物车”/“结账”块中的订单按钮 HTML - 请在编辑器中进行修改。
woocommerce_update_order_review_fragments不支持 ❌“购物车”/“结账”块不使用片段。

重要说明:

  • Page: 网页
  • Hook: 钩子
  • API: API
  • Privacy Policy: 隐私政策
  • Form fields: 表单字段
  • Extensions: 扩展
  • Registered: 注册
  • Checkbox: 复选框
  • Quantity: 数量
  • Register: 注册
  • Subtotal: 小计
  • Message: 信息
  • Requests: 请求
  • Shipping: 配送
  • Checkout: 结账
  • Address: 地址
  • Details: 详情
  • Privacy: 隐私
  • Summary: 摘要
  • Display: 显示

提交订单 操作

本部分正在进行中。

提交订单 筛选器

本部分正在进行中。

订单摘要 操作

本部分正在进行中。

订单摘要 筛选器

本部分正在进行中。

常见的扩展方式

购物车和结账模块

完全支持,无需修改

  • 修改购物车中的费用和其他价格 (woocommerce_cart_calculate_fees)
  • 在 WC 生命周期事件期间更新客户信息,例如在 woocommerce_init 中,更新客户的地址会反映在购物车/结账模块中。
  • 添加付款方式
  • 使用 woocommerce_product_get_name 更改产品名称

部分支持或通过替代方法支持

  • 修改布局,例如在特定部分(如配送、付款以及账单/配送地址)之前/之后插入内容。例如:在各种位置输出内容,例如在打印购物车内容之前 (woocommerce_cart_contents) 和在显示运费之前 (woocommerce_before_shipping_rate):
    • 这通过我们的 Slot/Fill 功能以及通过将内部块注册为要显示的块的子块来实现。
  • 添加付款方式
    • 仅仅添加付款方式是不够的,开发者还需要注册一个 React 组件,以便在付款模块中显示。
  • 添加本地自提的配送方式
    • 应该可以正常工作,但需要该方式支持本地自提功能。
  • 修改购物车中商品的名称
    • 需要开发者使用 JavaScript 筛选器,而不是 PHP 筛选器。

完全不支持

  • 修改单个购物车项目以添加或更改内容/更改 HTML(超出我们结账筛选器提供的范围)。

结账模块

完全支持,无需修改

  • 预填充表单中的值(一些商家通过覆盖其值来实现,使用 woocommerce_checkout_get_value
    • 只要购物车/客户对象中设置的值,我们都会尊重这些值。 woocommerce_checkout_get_value 不在结账模块中支持,但这是该钩子的唯一用例。

部分支持或通过替代方法实现

  • 添加新的结账字段(通常通过 woocommerce_checkout_fields 钩子实现)
    • 通过附加结账字段 API 支持,但目前并非所有字段类型都受支持。
  • 使用钩子(如 woocommerce_checkout_process)进行自定义字段验证
    • 通过钩入支付处理事件支持,验证发生在点击“提交订单”时,这仅适用于付款方式。其他扩展可以在点击按钮之前将验证错误添加到数据存储中,从而阻止结账,但这并不是一个好的解决方案,尤其是在验证检查成本较高的情况下。
  • 在订单提交之前/期间/之后更新订单/客户(通过钩子实现)
    • 我们有一些在结账过程中触发的钩子:
      • woocommerce_store_api_checkout_order_processed - 订单已提交时触发。
      • woocommerce_store_api_checkout_update_customer_from_request - 当客户数据从结账请求中更新时触发。
      • woocommerce_store_api_checkout_update_order_meta - 当订单元数据从请求中更新时触发。
    • 任何其他在短代码过程中触发的 WC 钩子(例如 woocommerce_checkout_order_processed)不会在块中的 Store API 请求中触发。
  • 使用 woocommerce_order_button_text 自定义“提交订单”按钮文本
    • 不支持通过钩子实现,但可以在编辑器中实现。目前没有编程方式可以实现此功能。

完全不支持

  • 修改现有的核心字段(通常通过 woocommerce_checkout_fields 钩子实现)
  • 移除账单/配送地址(使用上述方法)
  • 在订单最终确定之前执行操作 (woocommerce_checkout_create_order)

购物车块

完全支持,无需任何修改

  • 无,这些内容已经在“两个块”部分中涵盖。

部分支持或通过替代方法实现

  • 限制购物车中商品的数量选择,例如设置最小值、最大值或步长值。 以前,这通常通过过滤器(如 woocommerce_quantity_input_step)来实现。
    • 可以通过修改购物车项的 quantity_limits 属性,在 woocommerce_store_api_product_quantity_{$value_type} 过滤器中实现。

完全不支持

  • 通过修改模板文件来修改布局(除非使用块主题和自定义模板)