Market API Payment, pass array to the MarketPay

1

Good morning! I'm already 2 weeks trying to solve my problem and I did not find the answer anywhere until I tried this method but it still gives error: MarketPay API

Paid market documentation passes this template:

$preference_data = array(
"items" => array(
    array(
        "title" => "Produto teste",
        "quantity" => 1,
        "currency_id" => "BRL", 
        "unit_price" => 10.00
    )
  )
);

$preference = $mp->create_preference($preference_data);

But I do not know how to pass more than one product added to the cart. I already tried to make an array to pull the items, but without success.

$items = array();

foreach($cartItems as $item){   
    $items[] = array(

            "title" => $item->getProduct()->getName(),
            "quantity" => $item->getQuantity(),
            "currency_id" => "BRL",
            "unit_price" => $item->getProduct()->getPrice()
    );
}

$preference_data = array(
    "items" =>$items
);

$preference = $mp->create_preference($preference_data); 
    
asked by anonymous 04.04.2017 / 16:11

1 answer

0

I ended up solving the problem by doing something like a gambiarra hehe. I passed the names and picked up the units joining in a single array:

foreach($cartItems as $item){
        $array[] = $item->getQuantity().' Und. '.$item->getProduct()->getName(); 
    }
    $nameProducts = implode(" + ",$array);


        $preference_data = array(
            "items" => array(
                array(
                    "title" => $nameProducts,
                    "quantity" => 1,
                    "currency_id" => "BRL", 
                    "unit_price" => $cartTotal
                )
             )
          );
$preference = $mp->create_preference($preference_data); 
    
13.04.2017 / 16:03