Kevin.a
Kevin.a

Reputation: 4296

return returns the word array

I have this code here:

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

Its a function that returns an array. Whenever i call the function it returns the word "Array" i used print_r and it gave me nothing.

I call this function like this:

// prepare the sales payload
$sales_payload = array(
    'organization_id' => $getOrg['data']['0']['id'],
    'contact_id' => $contact_id,
    'status' => 'Open',
    'subject' => " ".str_replace($strToRemove, "", $_POST['billing_myfield12'])." - ".getproductList(),
    '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]],
);

Notice I called it here:

'subject' => " ".str_replace($strToRemove, "", $_POST['billing_myfield12'])." - ".getproductList(),

This used to work when i make a string of it

$allproductname=implode(" + ",$product_names);
return $allproductname;

Now i just want the array and its items. How do i do this?

Upvotes: 0

Views: 174

Answers (1)

Wax Cage
Wax Cage

Reputation: 788

By calling this line

str_replace($strToRemove, "", $_POST['billing_myfield12'])." - ".getproductList()

you are converting your array with product names to string.

Default array to string conversion is string "Array".

Use function implode instead, to join all product names into one string.

Or if you want an array of product names, use this

'product_names' => getproductList(),

Upvotes: 2

Related Questions