沃梦达 / 编程问答 / php问题 / 正文

在WooCommerce前端、后端和电子邮件通知的结账页面上显示自定义递送字段的结果(&A)

Show the result of custom delivery field on the checkout page in WooCommerce frontend, backend amp; email notifications(在WooCommerce前端、后端和电子邮件通知的结账页面上显示自定义递送字段的结果(A))

本文介绍了在WooCommerce前端、后端和电子邮件通知的结账页面上显示自定义递送字段的结果(&A)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我使用在结账页面上显示自定义域的代码。填写此字段并由客户下订单后,数据将显示在编辑订单时的"谢谢"页面和电子邮件通知中。

// Add the delivery custom field to the checkout
add_action( 'woocommerce_before_order_notes', 'my_delivery_custom_checkout_field' );
function my_delivery_custom_checkout_field( $checkout ) {

    echo '<div><h3>' . __('Custom Delivery') . '</h3>';

    woocommerce_form_field( 'my_custom_delivery', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('My Custom Delivery'),
        'placeholder'   => __(''),
        ), $checkout->get_value( 'my_custom_delivery' ));

    echo '</div>';
}

// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_delivery_checkout_field_update_order_meta' );
function my_custom_delivery_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['my_custom_delivery'] ) ) {
        update_post_meta( $order_id, 'my_custom_delivery', sanitize_text_field( $_POST['my_custom_delivery'] ) );
    }
}

// Display custom delivery field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_delivery_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_delivery_checkout_field_display_admin_order_meta($order){
    echo '<div><strong>'.__('Custom Delivery').':</strong> ' . get_post_meta( $order->id, 'my_custom_delivery', true ) . '</div>';
}

// Display custom delivery field in "Order received" and "Order view" pages (frontend)
add_action( 'woocommerce_order_details_after_order_table', 'my_custom_delivery_data_in_orders', 10 );
function my_custom_delivery_data_in_orders( $order ) {
    $my_custom_delivery = $order->get_meta( 'my_custom_delivery' );
    echo '<div><span>'.__('Custom Delivery').':</span> ' . $my_custom_delivery . '</div>';
}

// Display custom delivery field in Email notifications
add_filter( 'woocommerce_email_order_meta_fields', 'my_custom_delivery_data_in_emails', 10, 3 );
function my_custom_delivery_data_in_emails( $fields, $sent_to_admin, $order ) {
    $fields['Custom Delivery'] = array(
        'label' => __( 'Custom Delivery' ),
        'value' => $order->get_meta( 'my_custom_delivery' ),
    );
    return $fields;
}

但我想解决两个问题:

  1. 即使客户未填写字段名称"Custom Delivery",该字段也会持续显示在订单编辑页面和电子邮件通知中。

如果该字段未由客户端填写,我需要隐藏这些页面上的"自定义送达"名称。

  1. "自定义发货"字段的数据显示在包含订单数据的表之后。

我需要在"谢谢"页的订单表和电子邮件通知中显示此字段的数据。

如果您能帮忙,我会很高兴的!

推荐答案

  1. 字段名称"Custom Delivery"经常显示在订单编辑页面和电子邮件通知中,即使此字段为 不是由客户填写的。
我需要隐藏这些页面上的名称"Custom Delivery"(自定义递送),如果该字段 不是由客户端填写的。

替换

$my_custom_delivery = $order->get_meta( 'my_custom_delivery' );
echo '<div><span>'.__('Custom Delivery').':</span> ' . $my_custom_delivery . '</div>';

$my_custom_delivery = get_post_meta( $order->id, 'my_custom_delivery', true );

if ( !empty( $my_custom_delivery ) ) {
    echo '<div><strong>'.__('Custom Delivery').':</strong> ' . $my_custom_delivery . '</div>';      
}

  1. "自定义发货"字段的数据显示在包含订单数据的表之后。
我需要在"谢谢"上的订单表中显示此字段的数据 您"页面和电子邮件通知中。

删除此

// Display custom delivery field in "Order received" and "Order view" pages (frontend)
add_action( 'woocommerce_order_details_after_order_table', 'my_custom_delivery_data_in_orders', 10 );
function my_custom_delivery_data_in_orders( $order ) {
    $my_custom_delivery = $order->get_meta( 'my_custom_delivery' );
    echo '<div><span>'.__('Custom Delivery').':</span> ' . $my_custom_delivery . '</div>';
}

// Display custom delivery field in Email notifications
add_filter( 'woocommerce_email_order_meta_fields', 'my_custom_delivery_data_in_emails', 10, 3 );
function my_custom_delivery_data_in_emails( $fields, $sent_to_admin, $order ) {
    $fields['Custom Delivery'] = array(
        'label' => __( 'Custom Delivery' ),
        'value' => $order->get_meta( 'my_custom_delivery' ),
    );
    return $fields;
}

并替换为

// Display the chosen delivery information everywhere on frontend order pages and on email notifications
add_filter( 'woocommerce_get_order_item_totals', 'chosen_delivery_item_order_totals', 10, 3 );
function chosen_delivery_item_order_totals( $total_rows, $order, $tax_display ) {
    $new_total_rows = [];

    // Loop through Order total lines
    foreach($total_rows as $key => $total ){
        // Get the custom delivery
        $my_custom_delivery = $order->get_meta( 'my_custom_delivery' );

        // Display delivery information before payment method
        if( ! empty($my_custom_delivery) && 'payment_method' === $key ){
            $label  = __('Custom Delivery:');
            $value  = $my_custom_delivery;

            // Display 'Custom delivery' line
            $new_total_rows['chosen_delivery'] = array( 'label' => $label, 'value' => $value );
        }
        $new_total_rows[$key] = $total;
    }

    return $new_total_rows;
}

这样您就可以得到

// Add the delivery custom field to the checkout (backend)
add_action( 'woocommerce_before_order_notes', 'my_delivery_custom_checkout_field' );
function my_delivery_custom_checkout_field( $checkout ) {

    echo '<div><h3>' . __('Custom Delivery') . '</h3>';

    woocommerce_form_field( 'my_custom_delivery', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('My Custom Delivery'),
        'placeholder'   => __(''),
        ), $checkout->get_value( 'my_custom_delivery' ));

    echo '</div>';
}

// Update the order meta with field value (backend)
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_delivery_checkout_field_update_order_meta' );
function my_custom_delivery_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['my_custom_delivery'] ) ) {
        update_post_meta( $order_id, 'my_custom_delivery', sanitize_text_field( $_POST['my_custom_delivery'] ) );
    }
}

// Display custom delivery field value on the order edit page (frontend)
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_delivery_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_delivery_checkout_field_display_admin_order_meta($order){
    echo '<div><strong>'.__('Custom Delivery').':</strong> ' . get_post_meta( $order->id, 'my_custom_delivery', true ) . '</div>';
}

// Display the chosen delivery information everywhere on frontend order pages and on email notifications (frontend)
add_filter( 'woocommerce_get_order_item_totals', 'chosen_delivery_item_order_totals', 10, 3 );
function chosen_delivery_item_order_totals( $total_rows, $order, $tax_display ) {
    $new_total_rows = [];

    // Loop through Order total lines
    foreach($total_rows as $key => $total ){
        // Get the custom delivery
        $my_custom_delivery = $order->get_meta( 'my_custom_delivery' );

        // Display delivery information before payment method
        if( ! empty($my_custom_delivery) && 'payment_method' === $key ){
            $label  = __('Custom Delivery:');
            $value  = $my_custom_delivery;

            // Display 'Custom delivery' line
            $new_total_rows['chosen_delivery'] = array( 'label' => $label, 'value' => $value );
        }
        $new_total_rows[$key] = $total;
    }

    return $new_total_rows;
}

这篇关于在WooCommerce前端、后端和电子邮件通知的结账页面上显示自定义递送字段的结果(&A)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在WooCommerce前端、后端和电子邮件通知的结账页面上显示自定义递送字段的结果(&A)

基础教程推荐