Kevin.a
Kevin.a

Reputation: 4286

Get ALL woocommerce cart item names

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.

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.

I cannot use a foreach loop inside an array nor can i echo stuff ( i think)

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.

My attempts:

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

This worked but only for one product so if i have 2 products it only returns me one.

Upvotes: 0

Views: 8208

Answers (3)

Vasim Shaikh
Vasim Shaikh

Reputation: 4512

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

Upvotes: 3

LoicTheAztec
LoicTheAztec

Reputation: 253784

So you need an array of cart item names that you will send to a third party tool. So the code is going to be very compact:

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

Now you get you array in the $items_names variable that you can use in your API

This code is tested and work.

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

I hope it will help too.

Upvotes: 2

Benoti
Benoti

Reputation: 2200

As @Vasim method is the right one, I only want to point that you need double bracket after $product_name to add each items in the array, otherwise only the last item will be add.

 $product_name[]= $_product->post_title;

If you want a string instead of an array

 $product_name .= $_product->post_title;

Upvotes: 1

Related Questions