Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WordPress PHP将对象添加到数组_Php_Wordpress - Fatal编程技术网

WordPress PHP将对象添加到数组

WordPress PHP将对象添加到数组,php,wordpress,Php,Wordpress,我正在开发一个插件,使用一个基本的身份验证头将商业订单数据发送到RESTAPI 至于WooCommerce订单行,API文档告诉我将每个订单行作为对象添加到“lines”数组中。我正在努力设置它 有关更多说明,请参见以下代码: $items = array(); foreach ( $order->get_items() as $item_id => $item_data ) { $product = $item_data->get_product(); $prod

我正在开发一个插件,使用一个基本的身份验证头将商业订单数据发送到RESTAPI

至于WooCommerce订单行,API文档告诉我将每个订单行作为对象添加到“lines”数组中。我正在努力设置它

有关更多说明,请参见以下代码:

$items = array();

foreach ( $order->get_items() as $item_id => $item_data ) {

  $product = $item_data->get_product();
  $product_name = $product->get_name();

  $item_quantity = $item_data->get_quantity();
  $item_total = $item_data->get_total();

  $items[] = array(
     'unit' => $product_name,
     'quantity' => $item_quantity,
     'price' => $item_total
  );

}

$response = wp_remote_post( $url, array(
  'method' => 'POST',
  'timeout' => 20,
  'headers' => array(
     'username' => '***',
     'password' => '***',
     'company' => '***'
   ),
   'body' => array(
     'employee' => 1,
     'debtor' => 1,
     'deliveryMethod' => 1,
     'deliveryAddress' => array(
       'address' => $order->get_billing_address_1(),
       'postcode' => $order->get_billing_postcode(),
       'city' => $order->get_billing_city(),
       'country' => 'NL'
     ),
     'lines' => array(
       'unit' => $items['unit'],
       'quantity' => $items['quantity'],
       'price' => $items['price']
     )
   )
) );

WordPress调试日志告诉我它找不到“unit”、“quantity”和“price”的索引。

因此数组变量看起来类似于以下内容(根据示例代码):

//打印($items)
排列(
[0]=>[“单位”=>,“数量”=>,“价格”=>,
[1] =>[“单位”=>'',“数量”=>'',“价格”='',
[2] =>[“单位”=>'',“数量”=>'',“价格”='',
//等等。。。
)
空字符串将只包含您的数据

您正在将数组添加到数组中,因此如果要通过
$items
数组进行解析,可以使用
foreach

foreach($items作为$item){
打印(项目);
}
该输出应类似于:

Array
(
    [unit] => '',
    [quantity] => '',
    [price] => ''
)
但是它会在
$items
中的每个数组上循环,所以在我的例子中,使用上面的代码,它会
打印三次

另一种可视化方法是:

打印$items[0]['unit'];
打印$items[1]['unit'];
打印$items[2]['unit'];
每个数组都有索引(除非您定义了一个键),因此
[0]
将是
$items
数组中的第一个数组,然后您将引用
['unit']

这里有一个链接,可以帮助您了解更多:


希望这有帮助

你的数组看起来很正常,除了一些与文档不同的东西。 首先使用“行项目”而不是“行”

现在,每个订单可能有一个或多个产品,因此您必须将“行项目”与产品关联起来。您试图获取与这些产品相关的属性。您出现错误,因为您没有根据文档将数量和价格与产品id关联

'line_items' => [
    [
        'product_id' => 123,
        'quantity' => 2,
        'price' => 100
    ]
]
单位是不一样的。单位可以从meta获得。我猜你指的是尺寸单位。要获得meta,您可以像这样使用它

'line_items' => [
    [
        'product_id' => 123,
        'quantity' => 2,
        'price' => 100,
        'meta' => [
            "key" => "dimension_unit",
            "label" => "Dimension Unit",
            "value" => "in"
        ]
    ]
]
这里我使用了所有样本数据。您可以按自己的方式分配所需数据

希望这对您和您的API现在都能正常工作有所帮助