【问题标题】:Add a custom meta field value to WooCommerce order if status changes to custom status如果状态更改为自定义状态,则向 WooCommerce 订单添加自定义元字段值
【发布时间】:2021-08-16 15:16:03
【问题描述】:

我已经在整个互联网上进行了搜索。我正在寻找的是创建一个自定义的 woocommerce 订单字段,当订单状态更改为 wc-kurzuhradena 时,该字段将自动添加到订单中,该自定义订单状态为当前月份和年份。示例值:May 2021

到目前为止,我有这段代码添加了一个自定义字段,但我需要找到一个解决方案来解决此状态已更新的日期。

function add_date_field_shipped() {
  global $woocommerce, $post;
  $order = new WC_Order($post->ID);

   if ( empty(get_post_meta( $post->ID, 'shipped', true)) && ('kurzuhrada' == $order->status)) {
    update_post_meta( $post->ID, 'shipped', 'value here',true);
  }
}
add_action( 'pre_get_posts', 'add_date_field_shipped' );

提前感谢您的帮助。

【问题讨论】:

    标签: php wordpress woocommerce orders


    【解决方案1】:

    对于自定义订单状态,您可以使用 woocommerce_order_status_{$status_transition[to]} 复合操作挂钩,您将在其中将 {$status_transition[to]} 替换为自定义状态段。

    所以你得到:

    function action_woocommerce_order_status_kurzuhradena( $order_id, $order ) {
        // Set your default time zone (http://php.net/manual/en/timezones.php)
        // Set your locale information (https://www.php.net/manual/en/function.setlocale.php)
        date_default_timezone_set( 'Europe/Brussels' );
        setlocale( LC_ALL, 'nl_BE' );
        
        // Get current month & year
        $month = strftime( '%B' );
        $year = strftime( '%Y' );
        
        // Update meta
        $order->update_meta_data( 'shipped_date', $month . ' ' . $year );
        
        $order->save();
    }
    add_action( 'woocommerce_order_status_kurzuhradena', 'action_woocommerce_order_status_kurzuhradena', 10, 2 );
    

    要只允许一次,当更改为自定义订单状态时,请使用:

    function action_woocommerce_order_status_kurzuhradena( $order_id, $order ) {
        // Set your default time zone (http://php.net/manual/en/timezones.php)
        // Set your locale information (https://www.php.net/manual/en/function.setlocale.php)
        date_default_timezone_set( 'Europe/Brussels' );
        setlocale( LC_ALL, 'nl_BE' );
        
        // Get meta (flag)
        $flag = $order->get_meta( 'shipped_date_flag' );
    
        // NOT true
        if ( ! $flag ) {
            // Set flag
            $flag = true;
            
            // Update meta
            $order->update_meta_data( 'shipped_date_flag', $flag );
            
            // Get current month & year
            $month = strftime( '%B' );
            $year = strftime( '%Y' );
            
            // Update meta
            $order->update_meta_data( 'shipped_date', $month . ' ' . $year );       
        }
        
        // Save
        $order->save();
    }
    add_action( 'woocommerce_order_status_kurzuhradena', 'action_woocommerce_order_status_kurzuhradena', 10, 2 );
    

    【讨论】:

    • 奇怪的是它适用于您的 nl_BE 但不适用于 sk_SK
    • setlocale(LC_ALL, "sk_SK.utf8", "Slovak_Slovakia.65001", "Slovak");这做到了:-D
    猜你喜欢
    • 2022-12-15
    • 1970-01-01
    • 2018-08-30
    • 2015-06-29
    • 1970-01-01
    • 2022-01-19
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多