How to show total price of items in Woocommerce Ca

2019-09-07 06:37发布

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! :)!

1条回答
迷人小祖宗
2楼-- · 2019-09-07 07:15

Modify the woocommerce plugin to achieve this in following manner :

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

So now it will look something like wp-content > themes > your-theme > woocommerce > cart.

Now go to your plugin directory and follow below path :

wp-content > plugins > woocommerce > templates > cart

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 > themes > your-theme > woocommerce > cart > mini-cart.php.

Now its time to modify the code in wp-content > themes > your-theme > woocommerce > cart > 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.

查看更多
登录 后发表回答