当购物车中只有特定产品时,移除 WooCommerce 结账页面的条款和条件
P粉245003607
P粉245003607 2023-07-30 10:35:54
0
1
454

我在https://development.pittsburghconcertsociety.org上销售活动门票并接受捐款。当有人购买门票时,他们必须同意COVID政策。但是当有人只“购买”捐款,即他们只将捐款产品放入购物车时,他们不需要同意COVID政策。WooCommerce支持聊天机器人提供了以下代码,但是它不起作用:

function hide_terms_for_specific_product( $woocommerce_checkout_fields ) { // Check if the specific product is the only item in the cart if ( WC()->cart ) { $cart_items = WC()->cart->get_cart(); $specific_product_found = false; foreach ( $cart_items as $cart_item ) { // Replace '123' with the ID of the specific product if ( $cart_item['product_id'] == 551 ) { $specific_product_found = true; break; } } // Hide terms and conditions for the specific product if ( $specific_product_found ) { unset( $woocommerce_checkout_fields['terms'] ); } } return $woocommerce_checkout_fields; } add_filter( 'woocommerce_checkout_fields', 'hide_terms_for_specific_product' );

捐款产品的ID是551)。总结一下,如果购物车中有门票和捐款产品,我确实希望有T&C复选框/要求,但如果购物车中只有捐款产品,则不需要T&C。在这种情况下,仅仅隐藏T&C是不够的,它还必须不是必填项。

此外,如果我们销售商品,能够添加多个产品ID将会很好。


P粉245003607
P粉245003607

全部回复 (1)
P粉344355715

下面的代码将在购物车中只有特定产品时完全移除T&C要求:

add_filter( 'woocommerce_checkout_show_terms', 'remove_terms_and_conditions_for_specific_unique_item' ); function remove_terms_and_conditions_for_specific_unique_item( $show_terms ) { // Replace "123" with the desired product ID $targeted_id = 15; $cart_items = WC()->cart->get_cart(); // get cart items // Check if there is only one item in cart if( count($cart_items) > 2 ) { return $show_terms; } // Check if the targeted product ID is the only item in cart if ( reset($cart_items)['product_id'] == $targeted_id ) { return false; // Remove terms and conditions field } return $show_terms; }

代码应该放置在活动子主题的functions.php文件中,或者放置在插件中。已经进行了测试并确认可以正常工作。

    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责声明 Sitemap
    PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!