【问题标题】:Display custom fields on specific WooCommerce email notification在特定 WooCommerce 电子邮件通知上显示自定义字段
【发布时间】:2020-11-05 10:53:35
【问题描述】:

在我基于 WooCommerce 的网站上,我最近添加了一些代码来在“编辑订单”页面上显示每个订单的运输方式和价格。现在,我想尝试将这些相同的字段添加到发送给管理员的“新订单”电子邮件模板中。这是我到目前为止所得到的:

// Capture the available shipping methods, and costs: 
function action_woocommerce_checkout_update_order_meta( $order_id ) {
    // Get shipping packages
    $packages = WC()->shipping()->get_packages();
    
    // Set array
    $rate_labels = array();
    $rate_costs = array();
    
    // Loop through packages
    foreach ( $packages as $key => $package ) {
        // Loop through package rates
        foreach( $package['rates'] as $rate_id => $rate ) {
            // Push to array
            $rate_labels[] = $rate->get_label();
            $rate_costs[] = $rate->get_cost();
        }
    }

    // NOT empty
    if ( ! empty ( $rate_labels ) ) {
        // Update post meta
        update_post_meta( $order_id, '_available_shipping_methods', $rate_labels );
        update_post_meta( $order_id, '_available_shipping_method_cost', $rate_costs );
    }
}
add_action( 'woocommerce_checkout_update_order_meta', 'action_woocommerce_checkout_update_order_meta', 10, 1 ); 

// Make it display on the edit order page:
function action_woocommerce_admin_order_data_after_shipping_address( $order ) {
    // Get meta
    $rate_labels = $order->get_meta( '_available_shipping_methods' );
    $rate_costs = $order->get_meta( '_available_shipping_method_cost' );
    
    $methods = array ( $rate_labels, $rate_costs );
    
    // True
    if ( $rate_labels ) {
        // Loop
        echo '<p><strong>Shipping Methods: </strong>';
        foreach(array_combine($rate_labels, $rate_costs) as $rate_label => $rate_cost) {
             echo '<p>' . $rate_label . ' - $' . $rate_cost . '</p>';
        }
    }
    
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );

除此之外,这就是我一直在努力的工作,但到目前为止还没有运气:

// Add it to the new order email template 
add_filter( 'woocommerce_new_order', 'custom_woocommerce_email_order_meta_fields', 10, 3 );

function custom_woocommerce_email_order_meta_fields( $rate_labels, $sent_to_admin, $order ) {
    
    $rate_labels = $order->get_meta( '_available_shipping_methods' );
    $rate_costs = $order->get_meta( '_available_shipping_method_cost' );
    
    $methods = array ( $rate_labels, $rate_costs );
    
    if ( $rate_labels ) {
        // Loop
        echo '<p><strong>Shipping Methods: </strong>';
        foreach(array_combine($rate_labels, $rate_costs) as $rate_label => $rate_cost) {
             echo '<p>' . $rate_label . ' - $' . $rate_cost . '</p>';
        }
    }
}

【问题讨论】:

    标签: php wordpress woocommerce metadata email-notifications


    【解决方案1】:

    例如,请改用以下内容:

    add_action( 'woocommerce_email_after_order_table', 'wc_email_new_order_custom_meta_data', 10, 4);
    function wc_email_new_order_custom_meta_data( $order, $sent_to_admin, $plain_text, $email ){
        // On "new order" email notifications
        if ( 'new_order' === $email->id ){
            $rate_labels = $order->get_meta( '_available_shipping_methods' );
            $rate_costs = $order->get_meta( '_available_shipping_method_cost' );
            
            $methods = array ( $rate_labels, $rate_costs );
            
            if ( $rate_labels ) {
                // Loop
                echo '<p><strong>Shipping Methods: </strong>';
                foreach(array_combine($rate_labels, $rate_costs) as $rate_label => $rate_cost) {
                     echo '<p>' . $rate_label . ' - $' . $rate_cost . '</p>';
                }
            }
        }
    }
    

    它应该可以工作。

    【讨论】:

    • 找到了一个类似的解决方案,但这也很有效。谢谢!
    猜你喜欢
    • 2018-08-31
    • 2021-02-01
    • 2020-12-07
    • 1970-01-01
    • 2020-09-09
    • 2020-11-26
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    相关资源
    最近更新 更多