在电子邮件订单中从 WooCommerce 访问产品元数据-items.php
Accessing product meta data from WooComerce in email-order-items.php
我在尝试过滤客户下订单后发送到电子邮件中的一些数据时遇到了真正的困难。问题是每个订单项都显示运输信息(取货地点),即使它与订单末尾的最终取货地点相同。
我已经能够查看给出的 item_meta 数据,但我不确定如何过滤它以从根本上删除具有“Pickup Location”元键的数组部分,然后仍然打印其余部分。这是生成数据的 email-order-items.php 文件的区域(不是自定义的……这是 WooCommerce 的标准):
if ( ! empty( $item_meta->meta ) ) {
echo '<br/><small>' . nl2br( $item_meta->display( true, true, '_', "\n" ) ) . '</small>';
}
这是我通过简单的操作就能看到的:
foreach ($item_meta as $value) {
print_r($value);
}
结果:
Cookie → Cookie (One-Time)Array ( [_qty] => Array ( [0] => 1 )
[_tax_class] => Array ( [0] => ) [_product_id] => Array ( [0] => 5807
) [_variation_id] => Array ( [0] => 0 ) [_line_subtotal] => Array (
[0] => 2.5 ) [_line_total] => Array ( [0] => 0 ) [_line_subtotal_tax]
=> Array ( [0] => 0.15 ) [_line_tax] => Array ( [0] => 0 ) [_line_tax_data] => Array ( [0] =>
a:2:{s:5:"total";a:1:{i:1;s:1:"0";}s:8:"subtotal";a:1:{i:1;s:4:"0.15";}}
) [_shipping_item_id] => Array ( [0] => 28795 ) [Pickup Location] =>
Array ( [0] => PICKUP ADDRESS HERE, City, State, Zip ) )
WC_Product_Simple Object ( [id] => 5807 [post] => WP_Post Object (
[ID] => 5807 [post_author] => 596 [post_date] => 2016-07-23 17:55:10
[post_date_gmt] => 2016-07-23 21:55:10 [post_content] => [post_title]
=> Power Protein Cookie (One-Time) [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed
[post_password] => [post_name] => power-protein-cookie-one-time
[to_ping] => [pinged] => [post_modified] => 2016-07-23 19:39:18
[post_modified_gmt] => 2016-07-23 23:39:18 [post_content_filtered] =>
[post_parent] => 5806 [menu_order] => 1 [post_type] => product
[post_mime_type] => [comment_count] => 0 [filter] => raw )
[product_type] => simple [shipping_class:protected] =>
[shipping_class_id:protected] => 0 ) ]
如何遍历 item_meta 数据,取消设置(我认为是这样做的方法)取件位置数据,然后仍然显示其余数据?
更新:我进去并尝试了以下代码:
if(is_array($item_meta->meta))
{
foreach($item_meta->meta as $key => $value)
{
echo $key . '<br />' . $value;
}
}
这给了我以下内容:
Cookie → Cookie(一次性)_qty
Array_tax_class
Array_product_id
Array_variation_id
Array_line_subtotal
Array_line_total
Array_line_subtotal_税
Array_line_tax
Array_line_tax_数据
Array_shipping_item_id
数组拾取位置
数组
我觉得我已经很接近了,但不太了解 item_meta 内部的结构...任何帮助将不胜感激。
@update 3 - 操纵 php object
(未测试):
这是我最后一次尝试:
// Cloning the base object:
$custom_meta = clone $item_meta;
// OR
// $custom_meta = new stdClass();
// $custom_meta = $item_meta;
// saving the custom array in a temporary variable
$length = sizeof($custom_meta->meta) - 1;
$meta_temp = array_slice( $custom_meta->meta, 0, $length );
// Removing the property (array) from the object
unset($custom_meta->meta);
// Recreating the same property and setting the changed array Key/Values to it
$custom_meta->meta = $meta_temp;
// Displaying your custom object (for test)
echo print_r($custom_meta);
// Using it
$custom_meta->display();
基于此线程:Is it possible to delete an object's property in PHP?
@update 2 - 备选方案:
1) 而不是使用 unset()
函数使用 array_pop()
函数(因为我们必须删除最后一个数组中的项目):
$item_meta_meta_arr = array_pop( $item_meta->meta );
// you will use now $item_meta_meta_arr
print_r( $item_meta_meta_arr );
2) 而不是使用 unset()
函数使用 array_slice()
函数(因为我们必须删除最后一个数组中的项目):
$length = sizeof($item_meta->meta) - 1;
$item_meta_meta_arr = array_slice( $item_meta->meta, 0, $length );
// you will use now $item_meta_meta_arr
print_r( $item_meta_meta_arr );
But you will not use $item_meta->meta
as $item_meta
is an object and it's not possible to remove the data from $item_meta->meta directly
@Update (与作者评论相关)
Removing a key / value
in an array, where the key
is Pickup Location
.
首先我们检查它是我们要删除的正确的key / value
echo $item_meta->meta['Pickup Location'] // should display right "Pickup Location" value.
一旦获得正确的值,就可以使用unset()
php函数。但首先我们要通过 array
(keys
/values
) 到 新数组 。这是过程:
// Copying the data to a new array.
$item_meta_meta_arr = $item_meta->meta;
unset($item_meta_meta_arr['Pickup Location']);
// Checking that it has correctly be unset from the array:
foreach($item_meta_meta_arr as $key => $value)
{
echo 'Key: '. $key . ' / Value: ' . $value . '<br />';
}
参考文献:- Removing key => value pairs from an array, but its not removing them
原回答
要遍历多个数组/对象,你可以使用这个(看看你得到什么进行测试):
if(!is_string($item_meta->meta) )
{
foreach($item_meta->meta as $key1 => $value1)
{
if (is_string($value1)) echo $key1 . $value1 . ' (level 1 - string)<br />';
elseif ( is_array($value1) || is_object($value1) )
{
echo $key1 . ' (level 1 - array)<br />';
foreach($value1 as $key2 => $value2) {
{
if (is_string($value2)) echo $key2 . $value2 . ' (level 2 - string)<br />';
elseif ( is_array($value2) || is_object($value2) )
{
echo $key2 . ' (level 2 - array)<br />';
foreach($value2 as $key3 => $value3) {
{
if (is_string($value3)) echo $key3 . $value3 . ' (level 3 - string)<br />';
}
}
}
}
}
}
要使用 print_r()
函数查看结构化数据,您可以使用 var_dump()
函数:
echo var_dump( $item_meta );
// Or
foreach ($item_meta as $value)
echo var_dump( $value );
// Or
foreach ($item_meta->meta as $value)
echo var_dump( $value );
我在尝试过滤客户下订单后发送到电子邮件中的一些数据时遇到了真正的困难。问题是每个订单项都显示运输信息(取货地点),即使它与订单末尾的最终取货地点相同。
我已经能够查看给出的 item_meta 数据,但我不确定如何过滤它以从根本上删除具有“Pickup Location”元键的数组部分,然后仍然打印其余部分。这是生成数据的 email-order-items.php 文件的区域(不是自定义的……这是 WooCommerce 的标准):
if ( ! empty( $item_meta->meta ) ) {
echo '<br/><small>' . nl2br( $item_meta->display( true, true, '_', "\n" ) ) . '</small>';
}
这是我通过简单的操作就能看到的:
foreach ($item_meta as $value) {
print_r($value);
}
结果:
Cookie → Cookie (One-Time)Array ( [_qty] => Array ( [0] => 1 ) [_tax_class] => Array ( [0] => ) [_product_id] => Array ( [0] => 5807 ) [_variation_id] => Array ( [0] => 0 ) [_line_subtotal] => Array ( [0] => 2.5 ) [_line_total] => Array ( [0] => 0 ) [_line_subtotal_tax] => Array ( [0] => 0.15 ) [_line_tax] => Array ( [0] => 0 ) [_line_tax_data] => Array ( [0] => a:2:{s:5:"total";a:1:{i:1;s:1:"0";}s:8:"subtotal";a:1:{i:1;s:4:"0.15";}} ) [_shipping_item_id] => Array ( [0] => 28795 ) [Pickup Location] => Array ( [0] => PICKUP ADDRESS HERE, City, State, Zip ) ) WC_Product_Simple Object ( [id] => 5807 [post] => WP_Post Object ( [ID] => 5807 [post_author] => 596 [post_date] => 2016-07-23 17:55:10 [post_date_gmt] => 2016-07-23 21:55:10 [post_content] => [post_title] => Power Protein Cookie (One-Time) [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => power-protein-cookie-one-time [to_ping] => [pinged] => [post_modified] => 2016-07-23 19:39:18 [post_modified_gmt] => 2016-07-23 23:39:18 [post_content_filtered] => [post_parent] => 5806 [menu_order] => 1 [post_type] => product [post_mime_type] => [comment_count] => 0 [filter] => raw ) [product_type] => simple [shipping_class:protected] => [shipping_class_id:protected] => 0 ) ]
如何遍历 item_meta 数据,取消设置(我认为是这样做的方法)取件位置数据,然后仍然显示其余数据?
更新:我进去并尝试了以下代码:
if(is_array($item_meta->meta))
{
foreach($item_meta->meta as $key => $value)
{
echo $key . '<br />' . $value;
}
}
这给了我以下内容:
Cookie → Cookie(一次性)_qty Array_tax_class Array_product_id Array_variation_id Array_line_subtotal Array_line_total Array_line_subtotal_税 Array_line_tax Array_line_tax_数据 Array_shipping_item_id 数组拾取位置 数组
我觉得我已经很接近了,但不太了解 item_meta 内部的结构...任何帮助将不胜感激。
@update 3 - 操纵 php object
(未测试):
这是我最后一次尝试:
// Cloning the base object:
$custom_meta = clone $item_meta;
// OR
// $custom_meta = new stdClass();
// $custom_meta = $item_meta;
// saving the custom array in a temporary variable
$length = sizeof($custom_meta->meta) - 1;
$meta_temp = array_slice( $custom_meta->meta, 0, $length );
// Removing the property (array) from the object
unset($custom_meta->meta);
// Recreating the same property and setting the changed array Key/Values to it
$custom_meta->meta = $meta_temp;
// Displaying your custom object (for test)
echo print_r($custom_meta);
// Using it
$custom_meta->display();
基于此线程:Is it possible to delete an object's property in PHP?
@update 2 - 备选方案:
1) 而不是使用 unset()
函数使用 array_pop()
函数(因为我们必须删除最后一个数组中的项目):
$item_meta_meta_arr = array_pop( $item_meta->meta );
// you will use now $item_meta_meta_arr
print_r( $item_meta_meta_arr );
2) 而不是使用 unset()
函数使用 array_slice()
函数(因为我们必须删除最后一个数组中的项目):
$length = sizeof($item_meta->meta) - 1;
$item_meta_meta_arr = array_slice( $item_meta->meta, 0, $length );
// you will use now $item_meta_meta_arr
print_r( $item_meta_meta_arr );
But you will not use
$item_meta->meta
as$item_meta
is an object and it's not possible to remove the data from$item_meta->meta directly
@Update (与作者评论相关)
Removing a
key / value
in an array, where thekey
isPickup Location
.
首先我们检查它是我们要删除的正确的key / value
echo $item_meta->meta['Pickup Location'] // should display right "Pickup Location" value.
一旦获得正确的值,就可以使用unset()
php函数。但首先我们要通过 array
(keys
/values
) 到 新数组 。这是过程:
// Copying the data to a new array.
$item_meta_meta_arr = $item_meta->meta;
unset($item_meta_meta_arr['Pickup Location']);
// Checking that it has correctly be unset from the array:
foreach($item_meta_meta_arr as $key => $value)
{
echo 'Key: '. $key . ' / Value: ' . $value . '<br />';
}
参考文献:- Removing key => value pairs from an array, but its not removing them
原回答
要遍历多个数组/对象,你可以使用这个(看看你得到什么进行测试):
if(!is_string($item_meta->meta) )
{
foreach($item_meta->meta as $key1 => $value1)
{
if (is_string($value1)) echo $key1 . $value1 . ' (level 1 - string)<br />';
elseif ( is_array($value1) || is_object($value1) )
{
echo $key1 . ' (level 1 - array)<br />';
foreach($value1 as $key2 => $value2) {
{
if (is_string($value2)) echo $key2 . $value2 . ' (level 2 - string)<br />';
elseif ( is_array($value2) || is_object($value2) )
{
echo $key2 . ' (level 2 - array)<br />';
foreach($value2 as $key3 => $value3) {
{
if (is_string($value3)) echo $key3 . $value3 . ' (level 3 - string)<br />';
}
}
}
}
}
}
要使用 print_r()
函数查看结构化数据,您可以使用 var_dump()
函数:
echo var_dump( $item_meta );
// Or
foreach ($item_meta as $value)
echo var_dump( $value );
// Or
foreach ($item_meta->meta as $value)
echo var_dump( $value );