获取所有 woocommerce 购物车项目名称

Get ALL woocommerce cart item names

我正在尝试将 woocommerce 购物车商品发送到第三方工具。我需要项目名称。

$items = $woocommerce->cart->get_cart();
  foreach($items as $item => $values) { 

   $_product = $values['data']->post; 
     echo $_product->post_title; 
}

我可以获得这样的购物车商品名称。但由于以下原因,我不能这样做。我需要将购物车商品名称存​​储在一个数组中,该数组将发送到我之前提到的第三方工具。

$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,我希望将这些项目存储在主题数组键中。

我不能在数组中使用 foreach 循环,也不能回显内容(我认为)

基本上我的问题是: 有没有办法做到这一点:

$items = $woocommerce->cart->get_cart();
  foreach($items as $item => $values) { 

   $_product = $values['data']->post; 
     echo $_product->post_title; 
}

在数组中作为数组键值,因此该值将 return 所有购物车项目名称。

我的尝试:

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

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

然后我在数组中这样调用它

'subject' =>$_product->post_title

这有效,但只适用于一种产品,所以如果我有 2 种产品,它只有 return 我一个。

$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]],

);

现在,创建一个 returns 购物车产品列表的方法。

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; 
}

因为@Vasim 方法是正确的,我只想指出你需要在 $product_name 之后加双括号来添加数组中的每一项,否则只会添加最后一项。

 $product_name[]= $_product->post_title;

如果你想要一个字符串而不是数组

 $product_name .= $_product->post_title;

所以您需要一组购物车商品名称,您将把它们发送到第三方工具。所以代码会非常紧凑:

// initializing the array:
$items_names = array();

// iterating through each items of cart
foreach(WC()->cart->get_cart() as $cart_item)
    $items_names[] = $cart_item['data']->post->post_title;

现在您可以在 $items_names 变量中获取数组,您可以在 API

中使用该数组

此代码已经过测试并有效。

To display a string of coma separated values from the array you can use implode() function this way:

  $string_cart_item_names = implode( ', ', $items_names );

希望对你也有帮助。