gpt4 book ai didi

php - 在 Woocommerce 中获取订单标题

转载 作者:搜寻专家 更新时间:2023-10-31 21:01:37 25 4
gpt4 key购买 nike

我试图通过我的 functions.php 文件循环获取订购产品的名称。这是我的代码:

global $woocommerce;
$items = $woocommerce->cart->get_cart();

foreach($items as $item => $values) {
$_product = $values['data']->post;
}

然后我这样命名标题:

$_product->post_title

这有效,它返回我订购的产品的名称。问题是当我有 2 种或更多产品时,它仍然会返回 1 个名称。我怎样才能让它返 repo 物车中的所有名称。

最佳答案

woocommerce 中与购物车相关的新语法是使用 WC() 制作的 无需调用 global woocommerce;

所以你的代码将是这样的:

$products_in_cart= array();
$products_post_title_in_cart = array();
$products_ids_in_cart= array();

foreach(WC()->cart->get_cart() as $cart_item) {
$products_in_cart[] = $cart_item['data']->post;
$products_post_title_in_cart[] = $cart_item['data']->post->post_title;
$products_ids_in_cart[] = $cart_item['product_id'];
}

// The first product (or item of the cart)
$_product = $products_in_cart[0]; // product post data
$product_id = $products_ids_in_cart[0]; // product ID
$products_post_title_in_cart[0] // product post title

// The Second product (or item of the cart)
$_product = $products_in_cart[1]; // product post data
$product_id = $products_ids_in_cart[1]; // product ID
$products_post_title_in_cart[1] // product post title

// etc … for all other products you increase the key of the arrays to get the correct values

关于php - 在 Woocommerce 中获取订单标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41036813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com