Woocommerce 仅当购物车包含类别时才显示电子商务签出模板
我们有一个多步骤结账。结账的一个步骤是保险,但此保险仅适用于租赁类别 希望仅在购物车中有租赁产品时显示保险步骤。在上找到一篇很棒的文章,但当我们放置add_操作片段以加载签出步骤时,代码没有给出预期的结果: 下面是自定义模板的add_操作调用( 这是SkyVerge代码中的代码:Woocommerce 仅当购物车包含类别时才显示电子商务签出模板,woocommerce,checkout,Woocommerce,Checkout,我们有一个多步骤结账。结账的一个步骤是保险,但此保险仅适用于租赁类别 希望仅在购物车中有租赁产品时显示保险步骤。在上找到一篇很棒的文章,但当我们放置add_操作片段以加载签出步骤时,代码没有给出预期的结果: 下面是自定义模板的add_操作调用( 这是SkyVerge代码中的代码: // set our flag to be false until we find a product in that category $cat_check = false; // check each cart
// set our flag to be false until we find a product in that category
$cat_check = false;
// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
// replace 'membership' with your category's slug
if ( has_term( 'rentals', 'product_cat', $product->id ) ) {
$cat_check = true;
// break because we only need one "true" to matter here
break;
}
}
// if a product in the cart is in our category, do something
if ( $cat_check ) {
// we have the category, do what we want
add_action('woocommerce_multistep_checkout_after_shipping', 'add_my_insurance_step_with_new_field');
function add_my_insurance_step_with_new_field( $checkout ) {
wc_get_template( 'checkout/insurance.php', array( 'checkout' => $checkout ) );
}
}
太接近了!不确定会出什么问题
谢谢你的帮助,
-L这就解决了问题。将
$cat_check
设置为false
,运行购物车中的每个产品,检查购物车中是否有我们的分类slug(租赁
),如果为true,我们将跳出并运行下一个if
语句
获取模板文件并在签出订单信息之前加载它(woocommerce\u multistep\u checkout\u before\u order\u info
)
希望这能帮到别人。**或者如果我的代码有什么问题,请告诉我。谢谢!我也在上找到了这篇优秀的文章。
// set our flag to be false until we find a product in that category
$cat_check = false;
// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
// replace 'membership' with your category's slug
if ( has_term( 'rentals', 'product_cat', $product->id ) ) {
$cat_check = true;
// break because we only need one "true" to matter here
break;
}
}
// if a product in the cart is in our category, do something
if ( $cat_check ) {
// we have the category, do what we want
add_action('woocommerce_multistep_checkout_after_shipping', 'add_my_insurance_step_with_new_field');
function add_my_insurance_step_with_new_field( $checkout ) {
wc_get_template( 'checkout/insurance.php', array( 'checkout' => $checkout ) );
}
}
function cclever_show_insurance_step_if_rentals() {
if ( function_exists( 'wc_checkout_add_ons' ) ) {
// set to false first
$cat_check = false;
// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
// replace 'rentals' with your category's slug
if ( has_term( 'rentals', 'product_cat', $product->id ) ) {
$cat_check = true;
// we only need one "true" to leave
break;
}
}
// if a product in the cart is in our category, remove the add-ons
if ( $cat_check ) {
add_action('woocommerce_multistep_checkout_before_order_info', 'cclever_add_insurance_custom_step');
function cclever_add_insurance_custom_step( $checkout ) {
wc_get_template( 'checkout/insurance.php', array( 'checkout' => $checkout ) );
}
}
}
}
add_action( 'woocommerce_before_checkout_form', 'cclever_show_insurance_step_if_rentals' );