如何在Woocommerce购物车小工具中显示商品的总价? [英] How to show total price of items in Woocommerce Cart Widget?

查看:113
本文介绍了如何在Woocommerce购物车小工具中显示商品的总价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我的Woocommerce购物车小部件看起来是这样的(没有商品的总价):

Currently my Woocommerce cart widget look like this (witout total price of items):


img1     Coffee
         2 x $5
-------------------
img2     Biscuit
         1 x $10
-------------------
img3     Cheese
         3 x $10
-------------------
Subtotal: $50

我想将其更改为(带有数量项的总价):

I want to change it to (with total price of quantity items):


img1     Coffee
         2 x $5        $10
---------------------------
img2     Biscuit
         1 x $10       $10
---------------------------
img3     Cheese
         3 x $10       $30
---------------------------
Subtotal:              $50

您可以看到购物车小部件的第二个示例显示了商品的总价。

As you can see the second example of Cart Widget shows the total price of the items.

有人可以帮我吗?
提前谢谢! :)!

Can someone please help me out? Thanks in advance! :)!

推荐答案

修改woocommerce插件以以下方式实现此目的:

Modify the woocommerce plugin to achieve this in following manner :

在主题文件夹中创建一个文件夹,并将其命名为 woocommerce ,在新创建的woocommerce文件夹中,创建另一个具有 cart 名称的文件夹。

Make one folder in your theme folder and name it woocommerce and in new created woocommerce folder, create another folder with cart name.

所以现在看起来像 wp-content>主题>您的主题> woocommerce>购物车

现在转到您的插件目录,并遵循以下路径:

Now go to your plugin directory and follow below path :

wp-content>插件> woocommerce>模板>购物车

在上述路径中,您会找到一个名为 mini-cart.php的文件。复制该文件并将其粘贴到我们在该答案顶部创建的目录中。

When you go in above path, you will find one file named as mini-cart.php. Copy that file and paste it to the directory which we created at top of that answer.

wp-content>主题>您的主题> woocommerce>购物车> mini-cart.php

现在是时候修改 wp-content>主题>您的主题> woocommerce中的代码了>购物车> mini-cart.php

只需用以下代码替换所有代码:

Simply replace all code with following code:

    <?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 ( sizeof( WC()->cart->get_cart() ) > 0 ) : ?>

            <?php
                foreach ( WC()->cart->get_cart() 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 ( sizeof( WC()->cart->get_cart() ) > 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' ); ?>

让我知道您是否有任何疑问。

Let me know if you have any doubts.

这篇关于如何在Woocommerce购物车小工具中显示商品的总价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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