【问题标题】:Display in WooCommerce thankyou a custom field value based on a custom table在 WooCommerce 中显示基于自定义表的自定义字段值
【发布时间】:2021-05-14 02:06:48
【问题描述】:

我有一个代码用于在感谢页面上显示自定义字段值,但它不显示该值。但是我能够在后端管理页面上显示该值。以下是我的代码:

add_action( 'woocommerce_thankyou_order_received_text', 'additionalnote_order_data_in_admin', 10, 1    );
function additionalnote_order_data_in_admin( $order ){
    global $wpdb;
    global $post_id;
    
    $order = new WC_Order( $post_id );

    $table_name = $wpdb->prefix . 'woocommerce_custom_fields';

    $result = $wpdb->get_results("SELECT * FROM $table_name ");

    if(!empty($result)){

        foreach($result as $query){
            $name = $query->name;
            $label = $query->label;
            $get_order = $order->get_meta('_'.$name);

            echo '<p><strong>'.__('Delivery Date').':</strong> ' . $get_order . '</p>';
        }
    }
}

【问题讨论】:

    标签: php wordpress woocommerce custom-fields orders


    【解决方案1】:

    您的代码中有一些错误……请改用以下内容:

    add_action( 'woocommerce_thankyou_order_received_text', 'additional_note_order_data_in_admin', 10, 1    );
    function additional_note_order_data_in_admin( $order_received_text, $order ){
        global $wpdb;
    
        $order_id = $order->get_id(); // Get order Id (For info, If needed)
    
        $results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}woocommerce_custom_fields");
    
        if ( ! empty($results) ) {
            foreach ( $results as $result ) {
                if ( $meta_value = $order->get_meta( '_'.$result->name ) ) {
                    $order_received_text .= '</p><p><strong>' . __('Delivery Date') . ':</strong> ' . $meta_value;
                }
            }
        }
        return $order_received_text;
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。它应该更好地工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      • 2018-02-25
      • 2014-02-11
      相关资源
      最近更新 更多