-1

I have this static code today that works fine, now i need to make the order line dynamic. The code is from a e-commerce store.

$order = [
    "purchase_country" => "SE",
    "purchase_currency" => "SEK",
    "locale" => "sv-SE",
    "order_amount" => 10000,
    "order_tax_amount" => 2000,
    "order_lines" => [
        [
            "type" => "physical",
            "reference" => "123050",
            "name" => "Tomatoes",
            "quantity" => 10,
            "quantity_unit" => "kg",
            "unit_price" => 600,
            "tax_rate" => 2500,
            "total_amount" => 6000,
            "total_tax_amount" => 1200
        ],
        [
            "type" => "physical",
            "reference" => "543670",
            "name" => "Bananas",
            "quantity" => 1,
            "quantity_unit" => "bag",
            "unit_price" => 5000,
            "tax_rate" => 2500,
            "total_amount" => 4000,
            "total_discount_amount" => 1000,
            "total_tax_amount" => 800
        ]
    ],

    "merchant_urls" => [
        "terms" => "https://www.example.com/villkor.php",
        "cancellation_terms" => "https://www.example.com/terms/cancellation.html",
        "checkout" => "https://www.example.com/_script/checkout.php",
        "confirmation" => "https://www.example.com/_script/checkout.php",

        // Callbacks
        "push" => "https://www.example.com/api/push",
        "validation" => "https://www.example.com/api/validation",
        "shipping_option_update" => "https://www.example.com/api/shipment",
        "address_update" => "https://www.example.com/api/address",
        "notification" => "https://www.example.com/api/pending",
        "country_change" => "https://www.example.com/api/country"
    ]
];

Now I need to make order_lines dynamic.

I have tried this code:

$order = [
"purchase_country" => "SE",
"purchase_currency" => "SEK",
"locale" => "sv-SE",
"order_amount" => 10000,
"order_tax_amount" => 2000,
"order_lines" => [

$cartsql="select a, b, c, d from lux_cart WHERE status = 0 ORDER BY a DESC";    
if ($result=mysqli_query($mysqli,$cartsql)){
    while ($cartrow=mysqli_fetch_row($result)){
        
        $itemid = $cartrow[0];
        $prodTitel = $cartrow[1];
        $antalcart = $cartrow[2];
        $prodPris = $cartrow[3];
        
        [
            "type" => "physical",
            "reference" => "$itemid",
            "name" => "$prodTitel",
            "quantity" => $antalcart,
            "unit_price" => $prodPris,
            "tax_rate" => 2500,
            "total_amount" => $prodPris,
            "total_tax_amount" => 1200
        ],
                                


}
}

"merchant_urls" => [
    "terms" => "https://www.example.com/villkor.php",
    "cancellation_terms" => "https://www.example.com/terms/cancellation.html",
    "checkout" => "https://www.example.com/_script/checkout.php",
    "confirmation" => "https://www.example.com/_script/checkout.php",

    // Callbacks
    "push" => "https://www.example.com/api/push",
    "validation" => "https://www.example.com/api/validation",
    "shipping_option_update" => "https://www.example.com/api/shipment",
    "address_update" => "https://www.example.com/api/address",
    "notification" => "https://www.example.com/api/pending",
    "country_change" => "https://www.example.com/api/country"
]
];

But the page broken without any error message. I think I need to make some array ore something to resolve this issue. Can someone help me figure out what I am doing wrong here?

istepaniuk
  • 4,016
  • 2
  • 32
  • 60
  • 1
    You can't add random code blocks in the middle of defining an array. You need to define the array first (the static parts) and after you have your loop where you can [push](https://www.php.net/manual/en/function.array-push.php) data into the array – M. Eriksson Sep 29 '21 at 21:16

1 Answers1

3

You can't execute PHP code inside an array definition like that. That's causing a parse error that's the reason your page is broken with no error. (You can change settings on your development server to show these errors as well.) Instead, define the array with an empty array for order_lines

$order = [
   "purchase_country" => "SE",
   "purchase_currency" => "SEK",
   "locale" => "sv-SE",
   "order_amount" => 10000,
   "order_tax_amount" => 2000,
   "order_lines" => [],

   "merchant_urls" => [
      "terms" => "https://www.example.com/villkor.php",
      "cancellation_terms" => "https://www.example.com/terms/cancellation.html",
      "checkout" => "https://www.example.com/_script/checkout.php",
      "confirmation" => "https://www.example.com/_script/checkout.php",

      // Callbacks
      "push" => "https://www.example.com/api/push",
      "validation" => "https://www.example.com/api/validation",
      "shipping_option_update" => "https://www.example.com/api/shipment",
      "address_update" => "https://www.example.com/api/address",
      "notification" => "https://www.example.com/api/pending",
      "country_change" => "https://www.example.com/api/country"
   ]
];

Then fill that key with values from your query.

$cartsql="select a, b, c, d from lux_cart WHERE status = 0 ORDER BY a DESC";    
if ($result=mysqli_query($mysqli,$cartsql)){
    while ($cartrow=mysqli_fetch_row($result)){
        
        $itemid = $cartrow[0];
        $prodTitel = $cartrow[1];
        $antalcart = $cartrow[2];
        $prodPris = $cartrow[3];
        

        // Use [] to append the rows to the existing key
        $order['order_lines'][] = [
            "type" => "physical",
            "reference" => "$itemid",
            "name" => "$prodTitel",
            "quantity" => $antalcart,
            "unit_price" => $prodPris,
            "tax_rate" => 2500,
            "total_amount" => $prodPris,
            "total_tax_amount" => 1200
        ];
    }
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • 1
    No need for the intermediary scalar variables or double quotes around the value parts of the array so `"reference" => $cartrow[0],` etc would do fine – RiggsFolly Sep 29 '21 at 21:21
  • @RiggsFolly I agree, I just changed as little as possible of the OP code to make it work. I used to try to fix everything in my answers, but I feel like it's often easier for people to follow if it's closer to what they originally wrote. – Don't Panic Sep 29 '21 at 21:26
  • You have a point :) – RiggsFolly Sep 29 '21 at 21:29