0
我正在爲一個foreach中的每個項目的變量內建一個數組。每個項目在變量'quantity'
內都有一個名爲'quantity'
的值。如果數量大於1
那麼我需要創建許多數組。重複數組,如果變量大於1
我已經省略了很多代碼中的變量,使其更簡單,但一切都在這裏。如果數量大於1,那麼$ postData需要重複多次,所以我會爲每個數量獲取一個數組。
<?php
foreach($order->get_items() as $item_key => $item_values):
$quantity = $item_data['quantity'];
if (has_term($settingsDigital, 'product_cat', $product_id)) {
$product_type = "VIRTUALCARD";
$postData['bundles'][] = ['type' => $product_type, 'items' => [['bom' => [['type' => 'CARD', 'stockId' => $prod_sku, 'quantity' => $item_data['quantity'], 'metadata' => ['programme' => $settingsProgramme, 'denomination' => $item_data['total'], 'currency' => '826', 'mergeFields' => ['msg' => $giftMessage, 'giftAmount' => $formattedAmount, 'from' => $order_data['billing']['first_name'], 'to' => $theirName]], ]]]], 'delivery' => ['method' => 'EMAIL', 'recipientName' => $theirName, 'emailAddress' => $theirEmail]];
} else if (has_term($settingsPhysical, 'product_cat', $product_id)) {
$product_type = "PICKANDPACK";
$postData['bundles'][] = ['type' => $product_type, 'items' => [['bom' => [['type' => 'CARD', 'stockId' => $prod_sku, 'quantity' => $item_data['quantity'], 'metadata' => ['denomination' => $item_data['total'], 'currency' => '826']], ['type' => 'CARRIER', 'quantity' => 1, 'stockId' => $patt_carrier, 'metadata' => ['template' => $patt_template, 'mergeFields' => [['to' => '', 'msg' => '', 'giftAmount' => '', 'from' => '']]]], ['type' => "ENVELOPE", 'quantity' => 1, 'stockId' => 'ENV01']], ]], 'delivery' => ['method' => $shipping_method_name, 'shippingAddress' => ['firstname' => $fName, 'lastname' => $lName, 'addressLine1' => $addressLine1, 'addressLine2' => $addressLine2, 'town' => $town, 'county' => $county, 'postcode' => $postcode]]];
}
endforeach;
只是一個友好僅供參考,不最佳實踐速記循環,除非您選擇outputing成一個模板。 – DevDonkey
謝謝你的建議,我會牢記這一點! –