php 使用woocommerce在wordpress中获取购物车中的商品数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42866573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:43:15  来源:igfitidea点击:

Get the number of items in cart in wordpress using woocommerce

phpwordpresswoocommercecartdisplay

提问by Nemzytch

i'm actually developping a website. But i'm facing an issue. I need to display the number of item that are in the cart but only the number, nothing else i dont want total amount or anything else. Juste the number of items.

我实际上正在开发一个网站。但我正面临一个问题。我需要显示购物车中的商品数量,但只显示数量,我不想要总金额或其他任何东西。Juste 项目的数量。

I aim to display it over my " go to cart " link that is an image with an href. But this is not the main pb. The main pb is how to find a way to get only the number of item in the cart.

我的目标是将它显示在我的“转到购物车”链接上,该链接是带有 href 的图像。但这不是主要的pb。主要的 pb 是如何找到一种方法来仅获取购物车中的商品数量。

I'm using WordPress with Avada installed to be able to customize a bit more WP and get some includes features. But i'm not using the avada menu, I'm using a home made menu, and i want to display in it the number of item in cart.

我正在使用安装了 Avada 的 WordPress,以便能够自定义更多 WP 并获得一些包含功能。但我没有使用 avada 菜单,我使用的是自制菜单,我想在其中显示购物车中的商品数量。

And for the " shopping side " I'm using WooCommerce.

对于“购物方面”,我使用的是 WooCommerce。

I saw many posts of hooks and everything about this but it was about " show number of items and total cart amount, i dont want to display total car amount, I just want the number. a bit like this way : Cart number of items: plein.com website

我看到了很多关于钩子的帖子和关于这个的一切,但它是关于“显示商品数量和购物车总数量,我不想显示总汽车数量,我只想要数字。有点像这样:购物车数量: plein.com 网站

回答by Kodaloid

Hey and welcome to the site, though I am new around here too! The WooCommerce documentation actually has a snippet for exactly this purpose:

嘿,欢迎来到该网站,尽管我也是新来的!WooCommerce 文档实际上有一个专门用于此目的的片段:

https://docs.woocommerce.com/document/show-cart-contents-total/

https://docs.woocommerce.com/document/show-cart-contents-total/

In your case to just get the count, you could do something like this:

在你的情况下只是得到计数,你可以做这样的事情:

Cart Total: <?php echo WC()->cart->get_cart_contents_count(); ?>

Koda

幸田

回答by Jawwad Rizwan

From your given description I understand that you want to show cart count only and nothing else. Here goes the code to do that

从您给出的描述中,我了解到您只想显示购物车数量而不显示其他内容。这是执行此操作的代码

Add This code block In header.php or in that file where you want to display this widget...

在 header.php 或要显示此小部件的文件中添加此代码块...

<a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php WC()->cart->get_cart_contents_count(); ?></a>

And Then Add this code block to functions.php

然后将此代码块添加到functions.php

add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');

function woocommerce_header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;

    ob_start();

    ?>
    <a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php WC()->cart->get_cart_contents_count(); ?></a>

    <?php

    $fragments['a.cart-customlocation'] = ob_get_clean();

    return $fragments;

}

回答by mehul

Try This Code

试试这个代码

global $woocommerce;
print_r(count(WC()->cart->get_cart()));

回答by Aravind Srinivas

From here:

这里

Add this code in your header.php file.

将此代码添加到 header.php 文件中。

<a class="cart-contents" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>

回答by John Fotios

Further to Kodaloid's answer, you can also only show the cart count if it is greater than 0.

除了 Kodaloid 的回答之外,您还可以仅显示大于 0 的购物车计数。

<?php
 $cartcount = WC()->cart->get_cart_contents_count();
 if ($cartcount > 0) { echo $cartcount; }
?>

回答by ale

get_cart_contents_count() show the total quantity of all product items. i.e. you buy 2kg of product A and 1kg of product B, get_cart_contents_count() return 3.

get_cart_contents_count() 显示所有产品的总数量。即您购买 2 公斤产品 A 和 1 公斤产品 B,get_cart_contents_count() 返回 3。

If you need to show how many products are in cart, I did this function, in functions.php:

如果你需要显示购物车中有多少产品,我在functions.php中做了这个功能:

function count_item_in_cart() {
    $count = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $count++;
    }
    return $count;
}

then in your php file you can do that:

然后在你的 php 文件中你可以这样做:

<?php echo count_item_in_cart() . " products"; ?>