简体   繁体   English

如何获取woocommerce购物车商品名称作为数组

[英]How to get woocommerce cart item names as array

I was wondering how to get all the items in my cart as an array. 我想知道如何以阵列的形式获取购物车中的所有物品。 I need this because I have to use array_intersect , this way I can compare them. 我需要这个,因为我必须使用array_intersect ,这样我才能比较它们。

I haven't found anything about this on the internet. 我没有在互联网上找到任何有关此的信息。

You can access the cart contents through the WC()->cart object, like so: 您可以通过WC()->cart对象访问购物车内容,如下所示:

<?php
$cart_contents = WC()->cart->cart_contents;
$cart_item_names = array();
foreach($cart_contents as $item) {
    array_push($cart_item_names, $item['data']->post->post_title);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM