Woocommerce and php guru to help create array from woocommerce order object to be used to connect to external api
Budget: $10 – $30 USD
All the woocommerce item meta is already done and being added to the order via checkout.
// Get order item meta.
$order = wc_get_order( $order_id );
// Get transaction id.
$transaction = $order->get_transaction_id();
// Get more order meta values
// Get via loop order item meta
foreach ( $order->get_items() as $itemId => $item ) {
// Product id.
$productId = $item->get_product_id();
// Product quantity.
$productQt = $item->get_quantity();
// with product id connect to api to get api product data. I have this working and I get 1 array json_encoded [{"Product":"api_id","value1":"1","value2":"2","quantity":"0"}]
$apiproduct = $api_result;
$apiproduct_decoded = json_decode( $apiproduct, true );
$remove_first_array = $apiproduct_decoded[0];
$product_Value1 = $is_product_controller[0]['value1'];
$product_Value2 = $is_product_controller[0]['value2'];
// Now I need to multiply array value 1 and value 2 by the woocommerce product quantity. These values are going to be used later to connect to api and send data.
$sum_value1 = $product_Value1 * $productQt;
$sum_value2 = $product_Value2 * $productQt;
// Now I need to rebuild original api result array with woocommerce quantity
$remove_first_array_change = array_replace($remove_first_array, ['Quantity' => $productQt]);
$rebuild_api_array = $remove_first_array_change;
// Get gift, returns true or false
$gift = wc_get_order_item_meta( $itemId, '_resource', gift );
// Get resource id from woocommerce. With the resource id
$resource = wc_get_order_item_meta( $itemId, '_resource', true );
}
All of the above is done and working. I need help with the last part.
To connect to the external api I need to make a loop one per unique woocommerce item meta $resource with a constructed body.
If I have 3 woocommerce products with the same $resource I need to combine all values. Maybe do this outside the final foreach?
foreach ( $resource as $uniqueresource ) {
// There's another value that comes from the external api the $resource_data. We need to get this value to include in the body for the last call and is based on the unique $resource.
// Since the loop is based on unique $resource I only need to get this value once per different $resource.... We are looping the $resource.
// I already have this call working.
$body = array(
'resource_data' => $resource_data,
'products' => array($rebuild_api_array, $rebuild_api_array, $rebuild_api_array,etc), like this [{"Product":"123","value1":"1","value2":"0","quantity":"7"},{"Product":"456","value1":"2","value2":"1","quantity":"2"},{"Product":"999","value1":"1","value2":"4","quantity":"6"} etc. last array can not have ending seperator coma or api returns error]
'sum_value1' => should be the sum of all $sum_value1,
'sum_value2' => should be the sum of all $sum_value2,
'gift' => if 1 resource $gift returns true then this setting is immediately true for all identical $resource,
);
// connect to api via POST
}
// Get order item meta.
$order = wc_get_order( $order_id );
// Get transaction id.
$transaction = $order->get_transaction_id();
// Get more order meta values
// Get via loop order item meta
foreach ( $order->get_items() as $itemId => $item ) {
// Product id.
$productId = $item->get_product_id();
// Product quantity.
$productQt = $item->get_quantity();
// with product id connect to api to get api product data. I have this working and I get 1 array json_encoded [{"Product":"api_id","value1":"1","value2":"2","quantity":"0"}]
$apiproduct = $api_result;
$apiproduct_decoded = json_decode( $apiproduct, true );
$remove_first_array = $apiproduct_decoded[0];
$product_Value1 = $is_product_controller[0]['value1'];
$product_Value2 = $is_product_controller[0]['value2'];
// Now I need to multiply array value 1 and value 2 by the woocommerce product quantity. These values are going to be used later to connect to api and send data.
$sum_value1 = $product_Value1 * $productQt;
$sum_value2 = $product_Value2 * $productQt;
// Now I need to rebuild original api result array with woocommerce quantity
$remove_first_array_change = array_replace($remove_first_array, ['Quantity' => $productQt]);
$rebuild_api_array = $remove_first_array_change;
// Get gift, returns true or false
$gift = wc_get_order_item_meta( $itemId, '_resource', gift );
// Get resource id from woocommerce. With the resource id
$resource = wc_get_order_item_meta( $itemId, '_resource', true );
}
All of the above is done and working. I need help with the last part.
To connect to the external api I need to make a loop one per unique woocommerce item meta $resource with a constructed body.
If I have 3 woocommerce products with the same $resource I need to combine all values. Maybe do this outside the final foreach?
foreach ( $resource as $uniqueresource ) {
// There's another value that comes from the external api the $resource_data. We need to get this value to include in the body for the last call and is based on the unique $resource.
// Since the loop is based on unique $resource I only need to get this value once per different $resource.... We are looping the $resource.
// I already have this call working.
$body = array(
'resource_data' => $resource_data,
'products' => array($rebuild_api_array, $rebuild_api_array, $rebuild_api_array,etc), like this [{"Product":"123","value1":"1","value2":"0","quantity":"7"},{"Product":"456","value1":"2","value2":"1","quantity":"2"},{"Product":"999","value1":"1","value2":"4","quantity":"6"} etc. last array can not have ending seperator coma or api returns error]
'sum_value1' => should be the sum of all $sum_value1,
'sum_value2' => should be the sum of all $sum_value2,
'gift' => if 1 resource $gift returns true then this setting is immediately true for all identical $resource,
);
// connect to api via POST
}