Zen Cart Logo
Forums / Code Collaboration / Admin order edit, how to extrat prducts as array?

Admin order edit, how to extrat prducts as array?

Views: 2,216

Results 1 to 4 of 4
28 May 2021, 12:06 PM
#1
doghi22 avatar

doghi22

New Zenner

Join Date:
Aug 2018
Posts:
3
Plugin Contributions:
0

Admin order edit, how to extrat prducts as array?

PHP version 7.1
Zencart Version v1.5.7c

Im trying to implement a invoice api from a third partner, all my test code is working good but i dont know how to extrat the ordered products.

I need to have an array with all the data

$data = array(
  'Client[ID]' => $clientDenumire,  (static content provided by api developer)
  'Client[HASH]' => '7250110',    (static content provided by api developer)

  'Content[0][Name]' => 'Product 1 name',
  'Content[0][Price]' => '10', 
  'Content[0][qty]' => '5');

  'Content[1][Name]' => 'Product 2 name',
  'Content[1][Price]' => '12', 
  'Content[1][qty]' => '3');

  'Content[2][Name]' => 'Delivery',
  'Content[2][Price]' => '5', 
  'Content[2][qty]' => '1');

);

I need to extract the ordered products in same array, also the delivery method that user selected

My try :

 for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
     
  $products= [
  'Content['.$i.'][name]' => $order->products[$i]['name'],
  'Content['.$i.'][qty]' => $order->products[$i]['qty'],
  'Content['.$i.'][price]' => $order->products[$i]['price'],
];

 } 

But i dont know how to get close the loop and to extract informations in same array, also to combine it with my static informations

  'Client[ID]' => $clientDenumire,  (static content provided by api developer)
  'Client[HASH]' => '7250110',    (static content provided by api developer)

I also dont know how to extract my delivery method in same array and for each position to count Content[0], Content[1], Content[2] etc.

28 May 2021, 1:35 PM
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Admin order edit, how to extrat prducts as array?

so you can after your for loop do:

$product['Client[ID]'] = $clientDenumire;
$product['Client[HASH]'] = '7250110';

or can create an array that contains those two items and merge it, or can merge it directly such as:

$product = array_merge($product, array('Client[ID]' => $clientDenumire, 'Client[HASH]' => '7250110',));

Delivery method is stored in $orders->info where $orders->info['shipping_method'] and $orders->info['shipping_module_code'] provide the method and module respectively. Some of that information and/or other data may need to be pulled together or further processed to obtain your desired result(s). That information is also available at the "same time" that you are going through your loop. So if you need to put the shipping method with each "product" 'Content[0][shipping_method]' then you could do something like:

for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {

    $products= [
      'Content['.$i.'][name]' => $order->products[$i]['name'],
      'Content['.$i.'][qty]' => $order->products[$i]['qty'],
      'Content['.$i.'][price]' => $order->products[$i]['price'],
      'Content['.$i.'][shipping_method]' => $order->info['shipping_method'],
    ];

}

Unless you have something that offers multiple shipping methods per order and the ability to tie the product with the shipping method which would require more processing and "adding" the information to the array similar to described earlier in this response. Note, that in this for loop it may be necessary to cast $i as an integer as I have sometimes seen where something like: 'Content['.$i.'][name]' may product 'Content[][name]' instead of having a 0... So it may be necessary for those lines to have:
'Content['.(int)$i.'][....]';

I don't otherwise understand the last part of the last sentence, though it may be addressed by the above suggestion(s). E.g. '...and for each position to count Content[0], Content[1], Content[2] etc.'

28 May 2021, 1:51 PM
#3
doghi22 avatar

doghi22

New Zenner

Join Date:
Aug 2018
Posts:
3
Plugin Contributions:
0

Re: Admin order edit, how to extrat prducts as array?

Hello there,

Thx for your answer.

I know i can merge arrays and its the best way to work in this project.

**array1 **= (
$product['Client[ID]'] = $clientDenumire;
$product['Client[HASH]'] = '7250110';)

The problem is that i dont know how to extract ordered products in this form :

**array2 **= (

'Content[0][Name]' => 'Product 1 name',
'Content[0][Price]' => '10',
'Content[0][qty]' => '5');

'Content[1][Name]' => 'Product 2 name',
'Content[1][Price]' => '12',
'Content[1][qty]' => '3');

'Content[2][Name]' => 'Delivery',
'Content[2][Price]' => '5',
'Content[2][qty]' => '1');)

$FINALARRAY = array_merge($array1 , $array2));

**FINALARRAY **= (
'Client[ID]' => $clientDenumire, (static content provided by api developer)
'Client[HASH]' => '7250110', (static content provided by api developer)

'Content[0][Name]' => 'Product 1 name',
'Content[0][Price]' => '10',
'Content[0][qty]' => '5');

'Content[1][Name]' => 'Product 2 name',
'Content[1][Price]' => '12',
'Content[1][qty]' => '3');

'Content[2][Name]' => 'Delivery',
'Content[2][Price]' => '5',
'Content[2][qty]' => '1');
)

But how can i do **array2 **? how to extract the products outside the loop.....

24 Jun 2021, 2:32 PM
#4
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,954
Plugin Contributions:
8

Re: Admin order edit, how to extrat prducts as array?

doghi22:

...
I know i can merge arrays and its the best way to work in this project.

i think not necessarily. i think you need to understand arrays a bit better...

doghi22:

how to extract the products outside the loop.....

i would do something like this:

    require('includes/application_top.php');
    require_once DIR_WS_CLASSES . 'order.php';

    $orderNo = 17575;  // your order number
    $clientDenumire = 'denumire';

    $order = new order($orderNo);

    $finalArray[] = [
        'Client[ID]' => $clientDenumire,
        'Client[HASH]' => '7250110',
    ];

    for ($i = 0, $n = sizeof($order->products); $i < $n; $i++) {
        $finalArray[] = [
            'name' => $order->products[$i]['name'],
            'qty' => $order->products[$i]['qty'],
            'price' => $order->products[$i]['price'],
        ];
    }

    if (in_array('ot_shipping', array_column($order->totals, 'class'))) {
        $finalArray[] = [
            'name' => $order->totals[array_search('ot_shipping', array_column($order->totals, 'class'))]['title'],
            'qty' => 1,
            'price' => $order->totals[array_search('ot_shipping', array_column($order->totals, 'class'))]['value'],
        ];
    }

best.