在 WooCommerce 订单和电子邮件通知中显示自定义结帐字段值

问题描述 投票:0回答:1

我有以下代码,它添加一个自定义字段来结帐特定产品,将其添加到新订单页面并将其发送到新订单电子邮件中。

add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field', 10, 1 );
function my_custom_checkout_field( $checkout ) {
    
    $targeted_product_id = 6270; //Global Membership ID

    foreach( WC()->cart->get_cart() as $item ){
        if ( ! in_array($targeted_product_id, [$item['product_id'], $item['variation_id']]) ) {
            return; // Exit if there are other products in cart that the targeted one.
        }
    }

    echo '<div id="my_custom_checkout_field">
    <h2>' . __('Please select your Delivery Partner') . '</h2>';

    woocommerce_form_field( 'delivery_partner', array(
        'type'          => 'select',
        'required'  => 'true',
        'options'     => array(
   '---Please select your Delivery Partner' => __('---Please select your Delivery Partner', 'woocommerce' ),
   'Learners University College (LUC), Dubai' => __('Learners University College (LUC), Dubai', 'woocommerce' ),
   'Elite Training Services (ECS), KSA' => __('Elite Training Services (ECS), KSA ', 'woocommerce' ),
   'MELI, KSA' => __('MELI, KSA', 'woocommerce' ),
   'KLD Management Training, Dubai' => __('KLD Management Training, Dubai', 'woocommerce' )),
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Delivery Partner'),
        ), $checkout->get_value( 'delivery_partner' ));

    echo '</div>';
}

// Save the dropdown custom field selected value as order custom meta data:
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta', 10, 2 );
function my_custom_checkout_field_update_order_meta( $order, $data ) {
    if ( isset($_POST['delivery_partner']) && ! empty($_POST['delivery_partner']) ) {
        $order->update_meta_data( 'Delivery Partner', sanitize_text_field( $_POST['delivery_partner'] ) );
    } 
}

// Display the custom field value on admin order pages after billing adress:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ) {
    echo '<p><strong>'.__('Delivery Partner').':</strong> ' . $order->get_meta('Delivery Partner') . '</p>'; 
}

// Display the custom field value on "New Order" notification:
add_action( 'woocommerce_email_after_order_table', 'custom_woocommerce_email_order_meta_fields', 10, 4 );
function custom_woocommerce_email_order_meta_fields( $order, $sent_to_admin, $plain_text, $email ) {
    if( 'new_order' === $email->id )
        echo '<p><strong>'.__('delivery_partner').':</strong> ' . $order->get_meta('Delivery Partner') . '</p>';
}

我需要的是在付款后将其添加到“感谢您的订单”页面,因为它目前不起作用。

php wordpress woocommerce email-notifications
1个回答
0
投票

有一些错误和遗漏的东西,请使用以下代码替换:

// Utility function: Delivery partner options (array)
function get_delivery_partner_options() {
    return array(
        'luc_dubai'   => __('Learners University College (LUC), Dubai', 'woocommerce' ),
        'ecs_ksa'     => __('Elite Training Services (ECS), KSA ', 'woocommerce' ),
        'meli_ksa'    => __('MELI, KSA', 'woocommerce' ),
        'kldmt_dubai' => __('KLD Management Training, Dubai', 'woocommerce' ),
    );
}

// Utility function: Display the selected Delivery partner label name option
function display_selected_delivery_partner( $meta_value ) {
    $options = get_delivery_partner_options();
    return isset($options[$meta_value]) ? $options[$meta_value] : false;
}

// Display "Delivery Partner" custom checkout field 
add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field', 10, 1 );
function my_custom_checkout_field( $checkout ) {
    
    $targeted_product_id = 6270; //Global Membership ID

    foreach( WC()->cart->get_cart() as $item ){
        if ( ! in_array($targeted_product_id, [$item['product_id'], $item['variation_id']]) ) {
            //return; // Exit if there are other products in cart that the targeted one.
        }
    }

    echo '<div id="my_custom_checkout_field">
    <h2>' . __('Please select your Delivery Partner', 'woocommerce') . '</h2>';

    woocommerce_form_field( 'delivery_partner', array(
        'type'       => 'select',
        'required'   => 'true',
        'options'    => array_merge( 
            array('' => __('---Please select your Delivery Partner', 'woocommerce' )), 
                        get_delivery_partner_options()
        ),
        'class'      => array('my-field-class form-row-wide'),
        'label'      => __('Delivery Partner', 'woocommerce'),
    ), $checkout->get_value( 'delivery_partner' ));

    echo '</div>';
}

// Validate delivery partner field
add_action( 'woocommerce_checkout_process', 'custom_checkout_field_validation' );
function custom_checkout_field_validation() {
    $field_key = 'delivery_partner';
    if ( isset($_POST[$field_key]) && empty($_POST[$field_key]) ) {
        wc_add_notice( __('<strong>Delivery Partner</strong> is a required field.', 'woocommerce'),'error' );
    }
}

// Save the selected delivery partner as order custom metadata:
add_action( 'woocommerce_checkout_create_order', 'save_selected_delivery_partner' );
function save_selected_delivery_partner( $order ) {
    if ( isset($_POST['delivery_partner']) && ! empty($_POST['delivery_partner']) ) {
        $order->update_meta_data( 'delivery_partner', esc_attr($_POST['delivery_partner']) );
    } 
}

// Display the custom field value on admin order pages after billing adress:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_selected_delivery_partner_in_admin_orders', 10, 1 );
function display_selected_delivery_partner_in_admin_orders( $order ) {
    if ( $delivery_partner = display_selected_delivery_partner( $order->get_meta('delivery_partner') ) ) {
        echo '<p><strong>'.__('Delivery Partner', 'woocommerce').':</strong> ' . $delivery_partner . '</p>'; 
    }
}

// Display on customer orders and email notifications
add_filter( 'woocommerce_get_order_item_totals', 'display_delivery_on_order_item_totals', 10, 3 );
function display_delivery_on_order_item_totals( $total_rows, $order, $tax_display ) {
    if ( $delivery_partner = display_selected_delivery_partner( $order->get_meta('delivery_partner') ) ) {
        $new_total_rows = [];

        // Loop through order total rows
        foreach( $total_rows as $key => $values ) {
            // Inserting before payment method
            if( $key === 'payment_method' ) {
                $new_total_rows['gift_order'] = array(
                    'label' =>__('Delivery Partner', 'woocommerce') . ':',
                    'value' => $delivery_partner,
                );
            }
            $new_total_rows[$key] = $values;
        }
        return $new_total_rows;
    }
    return $total_rows;
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.