【问题标题】:Accessing product meta data from WooComerce in email-order-items.php在 email-order-items.php 中从 WooCommerce 访问产品元数据
【发布时间】:2016-12-22 09:21:45
【问题描述】:

我在尝试过滤客户下订单后发送到电子邮件中的某些数据时遇到了很大的困难。问题是每个订单项都显示运输信息(取货地点),即使它与订单结束时的最终取货地点相同。

我已经能够查看给出的 item_meta 数据,但我不确定如何过滤它以从根本上删除具有“拾取位置”元键的数组部分,然后仍然打印其余部分。这里是生成数据的 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] => 数组 ( [0] => ) [_product_id] => 数组 ( [0] => 5807 ) [_variation_id] => 数组 ( [0] => 0 ) [_line_subtotal] => 数组 ( [0] => 2.5 ) [_line_total] => 数组 ( [0] => 0 ) [_line_subtotal_tax] => 数组 ( [0] => 0.15 ) [_line_tax] => 数组 ( [0] => 0 ) [_line_tax_data] => 数组 ( [0] => a:2:{s:5:"总计";a:1:{i:1;s:1:"0";}s:8:"小计";a:1:{i:1;s: 4:“0.15”;}} ) [_shipping_item_id] => 数组 ([0] => 28795) [取货地点] => 数组([0] => 取件地址,城市,州,邮编)) WC_Product_Simple 对象([id] => 5807 [post] => WP_Post 对象( [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] => 强力蛋白饼干(一次性)[post_excerpt] => [post_status] => 发布 [comment_status] => 关闭 [ping_status] => 关闭 [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] => 产品 [post_mime_type] => [comment_count] => 0 [filter] => raw) [product_type] => 简单 [shipping_class:protected] => [shipping_class_id:protected] => 0 ) ]

如何遍历 item_meta 数据,取消设置(我认为是这样做的方法)Pickup Location 数据,然后仍然显示其余部分?

更新:我进去并尝试了以下代码:

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_tax Array_line_tax Array_line_tax_data Array_shipping_item_id ArrayPickup 位置 数组

我觉得我很接近,但不太了解 item_meta 内部的结构......任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress cookies woocommerce metadata


    【解决方案1】:

    @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 );
    

    但您不会使用$item_meta-&gt;meta,因为$item_meta 是一个对象,无法从$item_meta-&gt;meta directly 中删除数据


    @Update (与作者cmets有关)

    删除数组中的 key / value,其中 keyPickup 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 );
    

    【讨论】:

    • 我使用了您的第一个代码块(有一些更改......一些小错误)。我能够隔离我想要摆脱的部分......它被困在这里: if (is_string($value2)) echo $key2 。 $ 价值 2 。 '(2级)
      ';现在的问题是我如何正确地将其从 item_meta 容器中删除?我试图将 $value2 (这是我想要的)设置为空白,但它没有做任何事情。从 item_meta 对象中消除它的“正确”方法是什么?
    • 基本上,我想要 item_meta 对象中的所有其他内容...数据非常动态,所以我不能说我真正想要的静态字段,因为它可能会因每个部分而改变...他们是变体。例如,一个订单可能包含一个元键,其中包含“蔬菜”,但下一个可能没有蔬菜,只需要碳水化合物,所以它的元键是“碳水化合物”。我可以将数据隔离为:Pickup Location (level 1) 0Fitness Center, 14111 Fitness Drive, Detroit, MI 48000 (level 2) 我希望从 1 级为 Pickup Location 的级别中删除数据。
    • 我只是不知道如何从 item_meta 中删除该特定数据...有没有办法让我这样做?我想我可以自己遍历这些值,但是从每个 item_meta 对象中删除一条数据会容易得多。
    • 你的例子正是我在阅读之前就已经完成的事情......这是我注意到的......如果我首先检查 item_meta-> 对象,我看到有一个'Pickup Location ' 键,其中的值是 'array' 类型。然后我可以按照您的说明创建第二个数组,将 item_meta->meta 对象存储在那里,取消设置“Pickup Location”键,看看它已经消失了,但是当它使用 item_meta->display 打印出来时,它仍然存在。我真的很困惑:
    • 是的,我可能很快就可以了。我刚刚测试了您的其他选项...array_pop 不起作用。它什么也不做。但是,如果我将我们创建的新数组设置为原始 item_meta->meta 对象的副本,那么我实际上可以看到,在我对副本进行 unset() 之后,'Pickup Location' 元素消失了。问题是我实际上无法在下一部分中使用 item_meta_meta_arr->display ......我收到一个错误,阻止了我。也许我可以尝试清空 item_meta->meta,然后将其设置为 unset() 工作的新数组的副本?如何联系您?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2019-08-06
    • 2019-03-08
    • 1970-01-01
    • 2013-08-18
    • 2020-08-20
    相关资源
    最近更新 更多