0

I try a foreach in an object like this:

                            $data = '{
                                      "purchase_country": "'.$this->session->data['payment_address']['iso_code_2'].'",
                                              "purchase_currency": "'.$this->session->data['currency'].'",
                                              "locale": "'.$this->session->data['language'].'",
                                              "order_amount": '.$total.',
                                              "order_tax_amount":                       0,

                                              "order_lines": [{

                                                    '. foreach ($this->cart->getProducts() as $product) { .'
                                                    "reference": "19-402",
                                                    "name": "'.$product['name'].'",
                                                    "quantity": 1,
                                                    "unit_price": 210,
                                                    "tax_rate": 0,
                                                    "total_amount": 210,
                                                    "total_tax_amount": 0,
                                                    "image_url": "https://www.exampleobjects.com/logo.png",
                                                    "product_url": "https://www.estore.com/products/f2a8d7e34",
                                                '.}.'

                                              }]
                                             }';

But it seems, that the syntax isnt right. I got a Parse error: syntax error, unexpected 'foreach' (T_FOREACH)

Is a foreach possible in the object?

  • you can't do a foreach inside a variable. You are trying to concatenate a string with a foreach – Sfili_81 Jul 22 '20 at 09:31
  • okay, but how could I fill the order_lines with data? do u have an example? –  Jul 22 '20 at 09:38
  • Build a multidimensional array and use json_encode, it's much easier than concatenating strings like that. – Progrock Jul 22 '20 at 11:38

2 Answers2

0

This should do the trick as you cannot use the foreach loop at this point.

$data = '{
                                      "purchase_country": "'.$this->session->data['payment_address']['iso_code_2'].'",
                                              "purchase_currency": "'.$this->session->data['currency'].'",
                                              "locale": "'.$this->session->data['language'].'",
                                              "order_amount": '.$total.',
                                              "order_tax_amount":                       0,

                                              "order_lines": [{

                                                    ';
foreach ($this->cart->getProducts() as $product) { 
    $data = $data . '"reference": "19-402",
                                                    "name": "'.$product['name'].'",
                                                    "quantity": 1,
                                                    "unit_price": 210,
                                                    "tax_rate": 0,
                                                    "total_amount": 210,
                                                    "total_tax_amount": 0,
                                                    "image_url": "https://www.exampleobjects.com/logo.png",
                                                    "product_url": "https://www.estore.com/products/f2a8d7e34",
                                                ';
}
$data=$data.'                                 }]
                                             }';
jmizv
  • 1,172
  • 2
  • 11
  • 28
0

I tried the same for order_lines in the Javascript, but something is wrong on my syntax. Do u have a hint for me?

$('#button-confirm').on('click', function() {

    var cart_products = <?php echo json_encode($cart_products); ?>;
    data_concat = '';
    

    Klarna.Payments.authorize(
        {
          "purchase_country": "DE",
          "order_tax_amount": 0,

          "order_lines":
          [';

                    $(cart_products).each(function(key, value_data){

                        data_concat = data_concat += '{';
                                                    data_concat += '"name":'+value_data.name+',' ';
                                                    data_concat += '"quantity":'+1+',' ',
                                                    data_concat += '}';

                    });

          data_concat = data_concat + '],

          "customer":
          {
            "date_of_birth": "1970-01-01",
          }
        }, function(res) {

            execute_ajax();
    })

});