简体   繁体   English

在 WooCommerce 的新订单电子邮件中的项目下添加运输类别?

[英]Add shipping class under items in New Order email in WooCommerce?

I am trying to add the shipping class under each product to the new order emails for both admin and customers in WooCommerce.我正在尝试将每个产品下的运输类别添加到 WooCommerce 中管理员和客户的新订单电子邮件中。 This is my first time posting a question so please forgive me if there are formatting issues.这是我第一次发帖提问,如有格式问题请见谅。

I used the code found here: https://wordpress.stackexchange.com/questions/291637/woocommerce-add-shipping-class-below-each-product-in-shopping-cart-page我使用了在这里找到的代码: https : //wordpress.stackexchange.com/questions/291637/woocommerce-add-shipping-class-below-each-product-in-shopping-cart-page

That adds the shipping class below each product in the cart but I want to be able to show it on the order emails also.这会在购物车中的每个产品下方添加运输类别,但我也希望能够在订单电子邮件中显示它。

I am just unsure of which objects to call to grab the data for each product in the new order email.我只是不确定要调用哪些对象来获取新订单电子邮件中每个产品的数据。

Here's the code I'm using:这是我正在使用的代码:

add_action( 'woocommerce_order_item_meta_start', 'ts_order_item_meta_start', 10, 4 );
function ts_order_item_meta_start( $item_id, $item, $order, $plain_text, $item_name ){
$product = $cart_item['data']; // Get the WC_Product object instance
$shipping_class_id = $product->get_shipping_class_id(); // Shipping class ID
$shipping_class_term = get_term( $shipping_class_id, 'product_shipping_class' );

if( empty( $shipping_class_id ) )
    return $item_name; // Return default product title (in case of)

$label = __( 'Shipping class', 'woocommerce' );

return $item_name . '<br>
    <p class="item-shipping_class" style="margin:12px 0 0;">
        <strong>' .$label . ': </strong>' . $shipping_class_term->name . '</p>';
}

I expected the shipping class to be listed below each product in the "New Order" email but currently adding this code returns an internal server error upon checkout.我希望在“新订单”电子邮件中的每个产品下方列出运输类别,但目前添加此代码会在结帐时返回内部服务器错误。

The following will display The product shipping class name in Woocommerce "New Order" Email notification:以下将显示 Woocommerce“新订单”电子邮件通知中的产品运输类别名称:

// Setting the email_is as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email ){
    $GLOBALS['email_id_str'] = $email->id;
}

// Display Items shipping class name in New Order email notification
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 3 );
function custom_order_item_name( $item_name, $item, $is_visible ) {
    // Targeting email notifications only
    if( is_wc_endpoint_url() ) return $item_name;

    // Get the WC_Product object (from order item)
    $product = $item->get_product();

    if( $shipping_class_id = $product->get_shipping_class_id() ){
        // Getting the email ID global variable
        $refNameGlobalsVar = $GLOBALS;
        $email_id = $refNameGlobalsVar['email_id_str'];

        // Only for New Order email notification
        if( ! empty($email_id) && 'new_order' === $email_id ) {
            $shipping_class_name = get_term( $shipping_class_id, 'product_shipping_class' )->name;
            $item_name .= '<br><p class="item-shipping_class" style="margin:12px 0 0;">
            <strong>' . __( 'Shipping class', 'woocommerce' ) . ': </strong>' . $shipping_class_name . '</p>';
        }
    }

    return $item_name;
}

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 新订单 email 通知中添加运输 state - Add shipping state to subject in WooCommerce new order email notification 添加更新或删除 WooCommerce 运输订单项目 - Add update or remove WooCommerce shipping order items 根据 Woocommerce 中的发货项目,向客户的新订单电子邮件添加回复电子邮件 - Add a reply email to customer new order email, based on shipping item in Woocommerce Woocommerce将新订单通知发送到不同的电子邮件(基于运送国家) - Woocommerce new order notification to different email based on shipping country 基于 Woocommerce 新订单通知的配送方式 ID 的电子邮件收件人 - Email recipients based on Shipping method Id for Woocommerce new order notification 在Woocommerce 3中以编程方式向订单添加送货 - Add a shipping to an order programmatically in Woocommerce 3 根据送货方式 ID 在 WooCommerce 新订单电子邮件通知中隐藏送货地址 - Hide shipping address in WooCommerce new order email notification depending on shipping method id 将公司名称添加到 WooCommerce “新订单” email - Add company name to WooCommerce "New Order" email 添加文本到 WooCommerce 客户完成订单 email 基于运输和订单备注 - Add a text to WooCommerce customer complete order email based on shipping and order note 将订单总重量添加到WooCommerce新订单电子邮件通知中 - Add the order total weight to WooCommerce new order email notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM