【问题标题】:How to replicate a field data for all of the existing orders in Woocommerce如何复制 Woocommerce 中所有现有订单的字段数据
【发布时间】:2022-01-25 15:00:49
【问题描述】:

我的 Woocommerce 中有一个 CIF 字段,我有 1000 个订单,此字段已完成。显示此字段是因为我使用了插件。 ID 字段为 billing_cifnif。

现在我需要将此插件更改为其他插件,并且我需要将旧字段复制到新字段 ID billing_nif 中。

我需要用新值填充这些1000 orders

我有这个代码:

add_action('woocommerce_thankyou', 'replicate_billing_email', 100, 1);

function replicate_billing_email($order_id){
    $order = new WC_Order($order_id); 
    $billing_cifnif = $order->get_billing_cifnif(); 
    update_post_meta($order_id, 'billing_nif', $billing_cifnif);
    update_post_meta($order_id, '_billing_nif', $billing_cifnif);
}

它只适用于新订单,我需要将它用于旧订单。

谢谢

【问题讨论】:

  • $billing_cifnif 变量是否返回任何值?你能确认一下吗?
  • 你好!是的有一个价值

标签: php wordpress woocommerce orders wordpress-admin


【解决方案1】:

“只适用于新订单,我需要将它用于旧订单。”

如果您想更新现有订单,请运行以下 sn-p ONCE。运行完成后,将其从functions.php文件中删除:

add_action('wp_head', 'updating_orders_metadata');

function updating_orders_metadata()
{
    $orders = wc_get_orders(array('numberposts' => -1));

    if ($orders) 
    {
        foreach ($orders as $order) 
        {
            $billing_cifnif = $order->get_billing_cifnif();

            if ($billing_cifnif) 
            {
                update_post_meta($order->get_id(), 'billing_nif', $billing_cifnif);
                update_post_meta($order->get_id(), '_billing_nif', $billing_cifnif);
            }
        }
        echo "<div style='background-color: lightgreen; font-size: 20px; text-align: center;'>Done updating the orders! You may now delete the script from the functions.php file</div>";
    }
};

注意:

  • 为了进行更新,您需要刷新您的网站主页ONCE。在您的网站主页加载完成并看到绿色通知后,返回functions.php 文件并删除脚本!

  • 我不确定您是否需要billing_nif_billing_nif,所以我将两者都包括在内!请随意排除/删除您认为合适的任何一个!

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 2015-10-18
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 2013-11-17
    • 2017-09-17
    相关资源
    最近更新 更多