How can I display the total number of current items in my WooCommerce cart using the Elementor editor in Wordpress? We will be using the Elementor Icon List element, and this method will work for most other elements as well.
![图片[1]-使用Elementor在WooCommerce显示购物车商品总数的详细指南-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2024/05/2024052814381942.png)
Here is the effect we are trying to achieve:
![图片[2]-使用Elementor在WooCommerce显示购物车商品总数的详细指南-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2024/05/2024052813195641.png)
With this tutorial, you will learn how to display the total number of items in your cart and the quantity will be updated automatically when items are added or removed.
First, add the WooCommerce Shopping Cart Totals short code snippet.
To get started, the following code needs to be added to the new PHP code snippet. Either use the Code Snippets plugin, or add it directly to the child theme's functions.php Documentation.
To locate and edit the functions.php file, you can follow the steps below:
- Login to WordPress DashboardThe
- Navigate to "Appearance" > "Theme File Editor".The
- In the left menu, click "Appearance" and then select "Theme File Editor".
![图片[3]-使用Elementor在WooCommerce显示购物车商品总数的详细指南-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2024/05/2024052813493943.png)
- Select SubtopicThe
- Select your child theme from the "Select a theme to edit" drop-down menu in the upper right corner.
![图片[4]-使用Elementor在WooCommerce显示购物车商品总数的详细指南-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2024/05/2024052813513482.png)
- locate
functions.phpfileThe- In the list of files on the right, find and click on the
functions.phpDocumentation.
- In the list of files on the right, find and click on the
![图片[5]-使用Elementor在WooCommerce显示购物车商品总数的详细指南-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2024/05/2024052813531963.png)
exist functions.php Add the code at the end of the file and click "Updated documents" button to save the changes.
// Display the number of items in the cart
function display_cart_count() {
$cart_count = WC()->cart->get_cart_contents_count();
$cart_url = wc_get_cart_url(); ?
? >
<a class="menu-item cart-contents" href="/en/</?php echo $cart_url; ?>" title="View your shopping cart">
<i class="fas fa-shopping-cart"></i>
<span class="cart-contents-count"><?php echo $cart_count; ?></span>
</a>
<?php
}
// 使用短代码
add_shortcode('cart_count', 'display_cart_count');
// Ajax 刷新购物车商品数量
function refresh_cart_count($fragments) {
ob_start();
$cart_count = WC()->cart->get_cart_contents_count();
?>
<a class="menu-item cart-contents" href="/en/</?php echo wc_get_cart_url(); ?>" title="View your shopping cart">
<i class="fas fa-shopping-cart"></i>
<span class="cart-contents-count"><?php echo $cart_count; ?></span>
</a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
add_filter('woocommerce_add_to_cart_fragments', 'refresh_cart_count');
Next, go to Elementor's title template.
Open the Elementor header template (or any other location) where you wish to display the total number of items in your WooCommerce cart and add an icon list element. Follow the steps below:
- Navigate to template::
- In the left menu, click on "Templates".
- Then select "Theme Builder" (or "Theme Builder").
![图片[6]-使用Elementor在WooCommerce显示购物车商品总数的详细指南-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2024/05/2024052814044364.png)
- Select the template to edit::
- On the Theme Builder page, you will see all the templates such as header, footer, single post, archive page, etc.
- To edit the header template, find and click on the "Header" template (or "Header").
![图片[7]-使用Elementor在WooCommerce显示购物车商品总数的详细指南-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2024/05/2024052814125933.png)
- Edit Template::
- Find the title template you want to edit and click the "Edit with Elementor" button (or "Edit with Elementor").
- Adding an icon list element::
- In the Elementor editor, locate the "Icon List" element (or "Icon List") in the left panel.
- Drag the Icon List element to the location where you want to display the total number of items in your WooCommerce cart.
![图片[8]-使用Elementor在WooCommerce显示购物车商品总数的详细指南-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2024/05/2024052815275786.png)
- Configure the icon list::
In the icon list, select one of the icon items, click on the icon, and select the shopping cart icon.
In the text field for this item, enter the following:
![图片[9]-使用Elementor在WooCommerce显示购物车商品总数的详细指南-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2024/05/2024052814192217.png)
[cart_item_count]
- Saving and updating::
- Once the configuration is complete, click on the bottom of the page "update" button to save the changes.
styling
Let's make it look a little nicer, otherwise it'll look like this:
![图片[10]-使用Elementor在WooCommerce显示购物车商品总数的详细指南-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2024/05/2024052814235071.png)
Adding custom CSS::
- Select the icon list element.
- In the left panel, click on "high level"Tab.
- Find and click on the "Customize CSS" section.
- Add the following CSS code to the custom CSS input box:
.elementor-icon-list-item .cart-contents {
position: relative;
display: inline-block;
}
.elementor-icon-list-item .cart-contents-count {
position: absolute; top: -10px; }
top: -10px; right: -10px;
right: -10px; }
background: red; color: white; cart-contents-count { position: absolute; top: -10px; right: -10px; background: red; color: white; cart-contents-count
color: white;
background: red; color: white; border-radius: 50%; padding: 2px 6px;
padding: 2px 6px; font-size: 12px; font-size: 6px
font-size: 12px; }
}
You will see this section twice in CSS:
:nth-child(3)
Adjust both to match your own icons.
For example, if your shopping cart icon is the first in the list of icons, change to:nth-child(1)rather than:nth-child(3)The
The rest of the CSS, adjusted as needed to position and style the count.
Summary.
By adding custom PHP code snippets and integrating them into the child theme's functions.php file to create a short code to display the number of cart items. It also demonstrates how to add an icon list element to the Elementor header template and use that shortcode to display the total number of cart items on the page. Finally, use the Elementor icon to see the number of items in your WooCommerce cart in real-time! A better shopping experience for users.
Link to this article:https://www.361sale.com/en/10708/The article is copyrighted and must be reproduced with attribution.





















![表情[wozuimei]-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/themes/zibll/img/smilies/wozuimei.gif)
![表情[baoquan]-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/themes/zibll/img/smilies/baoquan.gif)

No comments