获取所有woocommerce购物车商品名称 [英] Get ALL woocommerce cart item names
问题描述
我正在尝试将woocommerce购物车项目发送给第三方工具。我需要商品名称。
I am trying to send the woocommerce cart items to third party tool. I need the item name.
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo $_product->post_title;
}
我可以得到这样的购物车名称。但由于以下原因,我无法做到这一点。我需要将购物车商品名称存储在一个数组中,该数组将发送到我之前提到的第三方工具中。
I can get the cart item names like that. But i cannot do it like this for the following reason. I need to store the cart item name in an array which will be sent to the third party tool I mentioned earlier.
$sales_payload = array(
'organization_id' => $organization_id,
'contact_id' => $contact_id,
'status' => 'Open',
'subject' => I WANT THIS TO BE ALL THE PRODUCT HNAMES,
'start_date' => date("Y-m-d"), // set start date on today
'expected_closing_date' => date("Y-m-d",strtotime(date("Y-m-d")."+ 14 days")), // set expected closing date 2 weeks from now
'chance_to_score' => '10%',
'expected_revenue' => 0, //set the expected revenue
'note' => $_POST['order_comments'],
'progress' => array(
'id'=>'salesprogress:200a53bf6d2bbbfe' //fill a valid salesprogress id to set proper sales progress
),
"custom_fields" => [["actief_in_duitsland"=>$value]],
);
In my array there is an array key named "subject". I want all the cart item names stored here.
因此,例如,如果我订购了两项,其中一项名为test 1,另一项名为test 2,我想这些项存储在主题数组键中。
So for example if i ordered two items one named test 1 and the other named test 2 I would like these items to be stored in the subject array key.
我不能在数组内部使用foreach循环,也不能回显内容(我认为)
I cannot use a foreach loop inside an array nor can i echo stuff ( i think)
Soo基本上我的问题是:
有没有办法做到这一点:
Soo basically my question is: Is there a way to do this:
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo $_product->post_title;
}
在数组内部作为数组键值,这样该值将返回所有购物车商品名称。
Inside an array as an array key value so the value would return all cart item names.
我的尝试:
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
}
然后我在数组中这样称呼它
then i call it like this in the array
'subject' =>$_product->post_title
这有效,但仅适用于一种产品,因此,如果我有2种产品,它只会返回我一种产品。
This worked but only for one product so if i have 2 products it only returns me one.
推荐答案
$sales_payload = array(
'organization_id' => $organization_id,
'contact_id' => $contact_id,
'status' => 'Open',
'subject' => getproductList(), //<<<<<Method called here
'start_date' => date("Y-m-d"), // set start date on today
'expected_closing_date' => date("Y-m-d",strtotime(date("Y-m-d")."+ 14 days")), // set expected closing date 2 weeks from now
'chance_to_score' => '10%',
'expected_revenue' => 0, //set the expected revenue
'note' => $_POST['order_comments'],
'progress' => array(
'id'=>'salesprogress:200a53bf6d2bbbfe' //fill a valid salesprogress id to set proper sales progress
),
"custom_fields" => [["actief_in_duitsland"=>$value]],
);
现在,创建一个返回购物车产品列表的方法。
Now,Create a method which returns list of cart product.
function getproductList()
{
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$product_names=array();
foreach($items as $item => $values) {
$_product = $values['data']->post;
$product_names[]=$_product->post_title;
}
/*
// if you want string then use
$allproductname=implode("",$product_names);
return $allproductname;
*/
return $product_names;
}
这篇关于获取所有woocommerce购物车商品名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!