【问题标题】:Exclude code from the cart page in Woocommerce从 Woocommerce 的购物车页面中排除代码
【发布时间】:2018-05-20 19:31:03
【问题描述】:

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

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 按钮 .

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

【问题讨论】:

    标签: php wordpress woocommerce cart product


    【解决方案1】:

    您只需要在现有的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 Conditional tags | is_cart()

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 2021-06-09
    相关资源
    最近更新 更多