在Woocommerce购物车小部件中显示购物车项目计数 [英] Display cart item count in Woocommerce cart widget

查看:72
本文介绍了在Woocommerce购物车小部件中显示购物车项目计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Woocommerce购物车小部件的底部或顶部显示购物车中物品的数量.似乎默认情况下,在侧栏中时,购物车小部件不会执行此操作.

I am trying to display the count of the items in the cart at the bottom or top of the Woocommerce cart widget. It seems that by default behaviour the cart widget does not do this when in the sidebar.

我对SO进行了研究,发现了以下 answer ,但添加单个小计的目标稍有不同.我尝试通过传入WC()->cart->get_cart_contents_count() 而不是 WC()->cart->get_cart()来进行修改,以查看是否可以获取购物车商品计数,但未显示任何建议?

I have researched SO and found the following answer, but it has a slightly different goal of adding individual subtotals. I have tried modifying it, by passing in WC()->cart->get_cart_contents_count() instead of WC()->cart->get_cart() to see if I can get the cart item count, but it's not displaying...any suggestions?

我要执行的操作示例:

我已从批准的答案中修改了代码:

<?php
/**
 * Mini-cart
 *
 * Contains the markup for the mini-cart, used by the cart widget
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.1.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->get_cart_contents_count() ) > 0 ) : ?>

        <?php
            foreach ( WC()->cart->get_cart_contents_count() as $cart_item_key => $cart_item ) {
                $_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 );
                    echo "<p>".$product_price."</p>";
                    ?>
                    <li>
                    <?php if ( ! $_product->is_visible() ) { ?>
                        <?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name; ?>
                    <?php } else { ?>
                        <a href="<?php echo get_permalink( $product_id ); ?>">
                            <?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name; ?>
                        </a>
                    <?php } ?>
                        <?php echo WC()->cart->get_item_data( $cart_item ); ?>
                        <?php   $new_product_price_array = explode ( get_woocommerce_currency_symbol(), $product_price); 
                                $new_product_price = number_format((float)$new_product_price_array[1] * $cart_item['quantity'], 2, '.', '');

                        ?>
                        <?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s &times; %s=%s%s', $cart_item['quantity'], $product_price,get_woocommerce_currency_symbol(), $new_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->get_cart_contents_count() ) > 0 ) : ?>

    <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 WC()->cart->get_cart_url(); ?>" class="button wc-forward"><?php _e( 'View Cart', 'woocommerce' ); ?></a>
        <a href="<?php echo WC()->cart->get_checkout_url(); ?>" class="button checkout wc-forward"><?php _e( 'Checkout', 'woocommerce' ); ?></a>
    </p>

<?php endif; ?>

<?php do_action( 'woocommerce_after_mini_cart' ); ?>

推荐答案

如果要在迷你购物车小部件中显示购物车计数,则可以使用钩子:

If you want to display the cart item count in the mini cart widget you can use hooks:

1)在微型车内容之前:

1) On top before minicart content:

add_action( 'woocommerce_before_mini_cart', 'minicart_count_after_content' );
function minicart_count_after_content() {
    $items_count = WC()->cart->get_cart_contents_count();
    $text_label  = _n( 'Item', 'Items', $items_count, 'woocommerce' );
    ?>
        <p class="total item-count"><strong><?php echo $text_label; ?>:</strong> <?php echo $items_count; ?></p>
    <?php
}

2)在按钮之前的底部:

2) On the bottom before the buttons:

add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'minicart_count_before_content' );
function minicart_count_before_content() {
    $items_count = WC()->cart->get_cart_contents_count();
    $text_label  = _n( 'Item', 'Items', $items_count, 'woocommerce' );
    ?>
        <p class="total item-count"><strong><?php echo $text_label; ?>:</strong> <?php echo $items_count; ?></p>
    <?php
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

WC()->cart->get_cart_contents_count();将为您提供项目总数,包括数量.

WC()->cart->get_cart_contents_count(); will give you the total items count including quantities.

如果要获取购物车中的物品数量,您将使用:

If you want to get the number of items in cart you will use instead:

$items_count = sizeof( WC()->cart->get_cart() );

这篇关于在Woocommerce购物车小部件中显示购物车项目计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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