隐藏WooCommerce中购物车中的项目和数量输入(&Q)(&Q) [英] Hide "remove item" and "quantity input" from cart in WooCommerce

查看:98
本文介绍了隐藏WooCommerce中购物车中的项目和数量输入(&Q)(&Q)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce购物车页面上,我想隐藏:

  • 删除项目按钮
  • (&Q)
  • 数量输入和字段
  • (&Q)

对于将出现在以下代码的// HIDE REMOVE BUTTON & QUNATITY OF THESE ITEMS注释中的项目

if (is_cart()){
    foreach ( $cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];

        $original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;
        
        if ($original_name == "Build Your Own"){
            $meta = wc_get_formatted_cart_item_data( $cart_item, true );
            $sMeta = substr($meta, -7);
            $new_name = $original_name . ' - ' . $sMeta;
            
            if( method_exists( $product, 'set_name' ) )
                $product->set_name( $new_name );
            else
                $product->post->post_title = $new_name;
        }else{
            $meta = wc_get_formatted_cart_item_data( $cart_item, true );
            
            if (!empty($meta)){
              
              // HIDE REMOVE BUTTON & QUANTITY OF THESE ITEMS.
              
            }
        }
    }
}

有人能教我如何做到这一点吗?

推荐答案

第一个选项是通过jQuery和css隐藏这些字段。您可以在购物车页面上使用各种挂钩,如woocommerce_before_calculate_totals操作挂钩。

注意:由于shop_table的所有表行都包含相同的类,因此会根据产品ID隐藏正确的按钮/字段。

  • 通过代码中添加的注释标签进行解释
function action_woocommerce_before_calculate_totals( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

   if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Only cart
    if( ! is_cart() )
        return;

    // If cart is NOT empty
    if ( ! $cart->is_empty() ) {
    
        // Loop
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
            // Get an instance of the WC_Product object
            $product = $cart_item['data'];
            
            // Get product id
            $product_id = $cart_item['product_id'];

            // Get name
            $original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;
            
            // Compare
            if ( $original_name == 'Build Your Own') {
                $meta = wc_get_formatted_cart_item_data( $cart_item, true );
                $sMeta = substr( $meta, -7 );
                $new_name = $original_name . ' - ' . $sMeta;
                
                // Set name
                if( method_exists( $product, 'set_name' ) ) {
                    $product->set_name( $new_name );
                } else {
                    $product->post->post_title = $new_name;
                }
            } else {
                $meta = wc_get_formatted_cart_item_data( $cart_item, true );
            
                // NOT empty
                if ( ! empty( $meta ) ) {
                    // Hide remove button & quantity fields
                    ?>
                    <script>
                        jQuery( document ).ready( function($) {
                            // Selector (product remove)
                            var product_selector = '[data-product_id="<?php echo $product_id; ?>"]';
                            
                            // Hide 'remove item'
                            $( product_selector ).css( 'display', 'none' );
                            
                            // Hide 'quantity input' (starting from the product remove selector)
                            $( product_selector ).parent().siblings( '.product-quantity' ).find( '.quantity' ).css( 'display', 'none' );
                        });
                    </script>
                    <?php
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );

另一种选择是通过woocommerce_cart_item_remove_link筛选器挂钩隐藏删除按钮。因此,这取决于您的首选项和最终目标,然后您可以对数量字段应用类似的内容。

function filter_woocommerce_cart_item_remove_link( $link, $cart_item_key ) {
    // Returns true on the cart page.
    if ( is_cart() ) {
        // Get cart
        $cart = WC()->cart;
        
        // If cart is NOT empty
        if ( ! $cart->is_empty() ) {
        
            // Loop
            foreach ( $cart->get_cart() as $cart_item ) {
                // Get an instance of the WC_Product object
                $product = $cart_item['data'];

                // Get name
                $original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;
                
                // Compare
                if ( $original_name == 'Build Your Own') {
                    $meta = wc_get_formatted_cart_item_data( $cart_item, true );
                    $sMeta = substr( $meta, -7 );
                    $new_name = $original_name . ' - ' . $sMeta;
                    
                    // Set name
                    if( method_exists( $product, 'set_name' ) ) {
                        $product->set_name( $new_name );
                    } else {
                        $product->post->post_title = $new_name;
                    }
                } else {
                    $meta = wc_get_formatted_cart_item_data( $cart_item, true );
                    
                    // NOT empty
                    if ( ! empty( $meta ) ) {
                        // Hide remove button
                        $link = '';
                    }
                }
            }
        }
    }

    return $link;
}
add_filter( 'woocommerce_cart_item_remove_link', 'filter_woocommerce_cart_item_remove_link', 10, 2 );

这篇关于隐藏WooCommerce中购物车中的项目和数量输入(&Q)(&Q)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆