dongmin4990 2016-09-25 14:06
浏览 73
已采纳

限制迷你购物车小部件中显示的产品数量

I am using WooCommerce mini cart widget and I want to limit the number of products shown in the mini cart. If a user adds 10 products to the cart, the mini cart widget shows all 10.

I would like to limit the number lets say to 5 products.

Is there a way to achieve this?

Thanks.

  • 写回答

1条回答 默认 最新

  • dongwubao4785 2016-09-25 19:13
    关注

    You can achieve this overriding cart/mini-cart.php WooCommerce template via your active theme, ( see this related docs: Template Structure + Overriding Templates via a Theme ).

    Briefly (if not done yet), you will need to copy from woocommerce plugin folder, a subfolder named templates to your active child theme (or theme) and to rename it woocommerce..

    After that you find inside that new woocommerce folder in cart subfolder a template named mini-cart.php.

    Open/edit mini-cart.php template and replace the code by this to limit cart to 5 items:

    <?php
    /**
     * Mini-cart
     *
     * Contains the markup for the mini-cart, used by the cart widget.
     *
     * This template can be overridden by copying it to yourtheme/woocommerce/cart/mini-cart.php.
     *
     * HOWEVER, on occasion WooCommerce will need to update template files and you
     * (the theme developer) will need to copy the new files to your theme to
     * maintain compatibility. We try to do this as little as possible, but it does
     * happen. When this occurs the version of the template file will be bumped and
     * the readme will list any important changes.
     *
     * @see     https://docs.woocommerce.com/document/template-structure/
     * @author  WooThemes
     * @package WooCommerce/Templates
     * @version 2.5.0
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }
    
    ?>
    
    <?php do_action( 'woocommerce_before_mini_cart' ); ?>
    
    <ul class="cart_list product_list_widget <?php echo $args['list_class']; ?>">
    
        <?php if ( ! WC()->cart->is_empty() ) : ?>
    
            <?php
                // Define HERE the number of items
                $number_of_items = 5;
                $loop_count = 0;
    
                foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                    // Limiting number of items displayed in mini cart
                    if($loop_count < $number_of_items) {
                        // The counter
                        $loop_count++;
                        $_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
                        $product_id   = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
    
                        if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
                            $product_name      = apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key );
                            $thumbnail         = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
                            $product_price     = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                            $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
                            ?>
                            <li class="<?php echo esc_attr( apply_filters( 'woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key ) ); ?>">
                                <?php
                                echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
                                    '<a href="%s" class="remove" title="%s" data-product_id="%s" data-product_sku="%s">&times;</a>',
                                    esc_url( WC()->cart->get_remove_url( $cart_item_key ) ),
                                    __( 'Remove this item', 'woocommerce' ),
                                    esc_attr( $product_id ),
                                    esc_attr( $_product->get_sku() )
                                ), $cart_item_key );
                                ?>
                                <?php if ( ! $_product->is_visible() ) : ?>
                                    <?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . '&nbsp;'; ?>
                                <?php else : ?>
                                    <a href="<?php echo esc_url( $product_permalink ); ?>">
                                        <?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . '&nbsp;'; ?>
                                    </a>
                                <?php endif; ?>
                                <?php echo WC()->cart->get_item_data( $cart_item ); ?>
    
                                <?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s &times; %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
                            </li>
                            <?php
                        }
                    }
                }
            ?>
    
        <?php else : ?>
    
            <li class="empty"><?php _e( 'No products in the cart.', 'woocommerce' ); ?></li>
    
        <?php endif; ?>
    
    </ul><!-- end product list -->
    
    <?php if ( ! WC()->cart->is_empty() ) : ?>
    
        <p class="total"><strong><?php _e( 'Subtotal', 'woocommerce' ); ?>:</strong> <?php echo WC()->cart->get_cart_subtotal(); ?></p>
    
        <?php do_action( 'woocommerce_widget_shopping_cart_before_buttons' ); ?>
    
        <p class="buttons">
            <a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class="button wc-forward"><?php _e( 'View Cart', 'woocommerce' ); ?></a>
            <a href="<?php echo esc_url( wc_get_checkout_url() ); ?>" class="button checkout wc-forward"><?php _e( 'Checkout', 'woocommerce' ); ?></a>
        </p>
    
    <?php endif; ?>
    
    <?php do_action( 'woocommerce_after_mini_cart' ); ?>
    

    Here we count the number of items in the foreach loop and we limit them to a specified number in variable $number_of_items.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64