如何针对特定产品隐藏 WooCommerce 的优惠券字段?
P粉138871485
2023-07-28 11:22:20
<p>我正在尝试在WooCommerce购物车和结账页面中隐藏某些产品的优惠券字段。在谷歌上搜索后,我找到了一些代码可以隐藏优惠券字段,但只适用于一个产品。</p><p>我该如何在这段代码中处理多个产品:</p><p><br /></p>
<pre class="brush:php;toolbar:false;">// hide coupon field on the checkout page
function disable_coupon_field_on_checkout( $enabled ) {
if ( is_checkout() ) {
$product_id = 240790;
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( $product_in_cart === $product_id ) $in_cart = true;
}
if ( $in_cart === true )
{
$enabled = false;
}
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'disable_coupon_field_on_checkout' );
// hide coupon field on the cart page
function disable_coupon_field_on_cart( $enabled ) {
if ( is_cart() ) {
$product_id = 240790;
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( $product_in_cart === $product_id ) $in_cart = true;
}
if ( $in_cart === true )
{
$enabled = false;
}
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'disable_coupon_field_on_cart' );</pre>
下面的代码将处理多个产品ID和/或变体ID,用于购物车和结账页面,禁用这些产品的优惠券字段。
应该有用