【问题标题】:WooCommerce: Remove add to cart buttons on archives but not in cartWooCommerce:删除存档上的添加到购物车按钮,但不在购物车中
【发布时间】:2020-11-03 13:51:57
【问题描述】:

我想删除除购物车本身之外的每个页面上的“添加到购物车”按钮。

我找到了一个用钩子移除购物车按钮的解决方案:

remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

工作正常,但到处都删除了按钮。

我试过这段代码来移除除购物车之外的所有地方的按钮:

if (!is_cart()) :
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
endif;

但这似乎不起作用。

有没有其他不接触模板文件的方法?

【问题讨论】:

    标签: php wordpress loops woocommerce cart


    【解决方案1】:

    下面会解决问题(基于woocommerce_template_loop_add_to_cart()原始功能代码):

    add_action('init', 'remove_add_to_cart_function_callback' );
    function remove_add_to_cart_function_callback() {
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
        add_action( 'woocommerce_after_shop_loop_item', 'custom_template_loop_add_to_cart', 10 );
    }
    
    if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {
    
        function custom_template_loop_add_to_cart( $args = array() ) {
            global $product;
    
            if ( $product && is_cart() ) {
                $defaults = array(
                    'quantity'   => 1,
                    'class'      => implode(
                        ' ',
                        array_filter(
                            array(
                                'button',
                                'product_type_' . $product->get_type(),
                                $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
                                $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
                            )
                        )
                    ),
                    'attributes' => array(
                        'data-product_id'  => $product->get_id(),
                        'data-product_sku' => $product->get_sku(),
                        'aria-label'       => $product->add_to_cart_description(),
                        'rel'              => 'nofollow',
                    ),
                );
    
                $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
    
                if ( isset( $args['attributes']['aria-label'] ) ) {
                    $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
                }
    
                wc_get_template( 'loop/add-to-cart.php', $args );
            }
        }
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    相关:Remove "Add To Card" only Home page in WooCommerce

    【讨论】:

    【解决方案2】:

    如果不是购物车,你试过了吗?

    if (!is_cart()) :
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    endif;
    

    【讨论】:

    • 是的,对不起。那是我的错字。但这也行不通。
    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 2019-08-04
    • 2018-10-30
    • 2018-05-08
    • 1970-01-01
    相关资源
    最近更新 更多