【问题标题】:Display cart item count in Woocommerce cart widget在 Woocommerce 购物车小部件中显示购物车项目数
【发布时间】:2018-05-30 14:31:13
【问题描述】:

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

我研究了 SO 并发现了以下 answer,但它的添加单个小计的目标略有不同。我已经尝试修改它,通过传入WC()->cart->get_cart_contents_count() 而不是 WC()->cart->get_cart() 来查看我是否可以获得购物车项目数,但它没有显示......有什么建议吗?

我正在尝试做的示例:

我根据批准的答案修改了代码:

<?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' ); ?>

【问题讨论】:

    标签: php wordpress woocommerce cart hook-woocommerce


    【解决方案1】:

    如果您想在迷你购物车小部件中显示购物车商品数量,您可以使用挂钩:

    1) 在迷你购物车内容之前:

    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) 在按钮之前的底部:

    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 文件中。经过测试并且可以工作。


    WC()-&gt;cart-&gt;get_cart_contents_count(); 将为您提供包括数量在内的商品总数。

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

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

    【讨论】:

    • Thanks@LoicTheAztec - 所以顶部钩子可以在内容之前显示它 - 但是有没有办法将它内联显示(在购物车小部件标题旁边),如屏幕截图所示?你在第二个钩子函数中也有错字。已更正。 :)
    【解决方案2】:

    您没有在任何地方回显 WC()->cart->get_cart_contents_count(),所以不,它不会显示。

    无论您需要在代码中的何处显示计数,都需要使用

    echo WC()->cart->get_cart_contents_count();
    

    【讨论】:

      猜你喜欢
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 2015-02-21
      • 1970-01-01
      • 2017-04-27
      • 2022-11-24
      • 2013-07-05
      • 2016-03-30
      相关资源
      最近更新 更多