Trouble with PHP Array Mapping for JSON Output in WooCommerce Order Processing

Hello Stack Overflow Community,

I am working on a WooCommerce-based project in PHP and encountering an issue with mapping product details to a specific ‘class’ value in a JSON output. Despite attempting several debugging steps, I can’t seem to resolve the incorrect assignment of the ‘class’ ID in the JSON. I would appreciate any insights or suggestions.

Problem:
I have a PHP function that processes WooCommerce orders. It’s supposed to map each product in an order to a specific ‘class’ based on its plant attribute (‘pa_plant’). The ‘class’ value, along with other details, is then used to create a JSON object for each product. However, the ‘class’ ID in the resulting JSON is consistently incorrect.

Array:

$profitCentre_mapping = [
'38' => '2375',
'28' => '2385',
'34' => '2386',
'34' => '2386',
'35' => '2477'
];
$profitCentre = 'NO PROFIT CENTRE'; // Initialize profitCentre outside the loop
        if (!isset($items_by_depot[$depot_id])) {
            $items_by_depot[$depot_id] = [];
        }
        
        $profitCentre = isset($profitCentre_mapping[$depot_id]) ? $profitCentre_mapping[$depot_id] : 'NO PROFIT CENTRE';

//JSON Snippet:
            "location" => ["id" => $depot_id],
            "class" => ["id" => $profitCentre],
  1. Checked and double-checked the mapping arrays for any inconsistencies or typos.
  2. Added debugging statements to print out values of key variables during processing.
  3. Isolated the problem to ensure that the issue lies within the loop where depot_id and profitCentre are assigned.
  4. Reviewed the conditions for assigning depot_id and profitCentre.