从Woocommerce的购物车页面中排除代码



在我的Woocommerce商店中,我使用以下代码在存档页面上的添加到购物车按钮旁边添加了一个数量框:

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<a rel="nofollow" data-product_id="'. $product->id .'" onclick="addToCartLink(this,' . $product->id .')" class="add_to_cart_button product_type_simple button primary is-flat mb-0 is-small">הוסף לסל</a>';
// $html = '<a onclick="addToCartLink(this,'. $product->id .')">הוסף לסל</a>';
$html .= '<div class="quantity buttons_added">';
$html .= '<input type="button"  value="-"  class="minus button is-form">';
$html .= '<input type="number" id="quantity_'. $product->id .'" class="input-text qty text" step="1" min="0" max="9999" name="quantity" value="1" title="כמות" size="4" pattern="[0-9]*" inputmode="numeric" >';
$html .= '<input type="button" value="+" class="plus button is-form">';
$html .= '</div>';
}
return $html;
}

在购物车页面中,我创建了一个自定义交叉销售产品轮播。所以我需要避免此代码在购物车页面中工作,因此添加到购物车的交叉销售产品按钮将是默认的 woocommerce

如何在代码中排除购物车页面?

您只需要在现有的if语句中添加此条件:! is_cart()

所以你的代码将是:

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() && ! is_cart() ) {
$html = '<a rel="nofollow" data-product_id="'. $product->id .'" onclick="addToCartLink(this,' . $product->id .')" class="add_to_cart_button product_type_simple button primary is-flat mb-0 is-small">הוסף לסל</a>';
//$html = '<a onclick="addToCartLink(this,'. $product->id .')">הוסף לסל</a>';
$html .= '<div class="quantity buttons_added">';
$html .= '<input type="button"  value="-"  class="minus button is-form">';
$html .= '<input type="number" id="quantity_'. $product->id .'" class="input-text qty text" step="1" min="0" max="9999" name="quantity" value="1" title="כמות" size="4" pattern="[0-9]*" inputmode="numeric" >';
$html .= '<input type="button" value="+" class="plus button is-form">';
$html .= '</div>';
}
return $html;
}

这应该可以解决您的问题。

相关官方文档: WooCommerce 条件标签 |is_cart()

最新更新