【问题标题】:Add shipping class under items in New Order email in WooCommerce?在 WooCommerce 的新订单电子邮件中的项目下添加运输类别?
【发布时间】:2019-06-12 23:21:06
【问题描述】:

我正在尝试将每个产品下的运输类别添加到 WooCommerce 中管理员和客户的新订单电子邮件中。这是我第一次发布问题,如果有格式问题请见谅。

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

这会在购物车中的每个产品下方添加运输类别,但我也希望能够在订单电子邮件中显示它。

我只是不确定要调用哪些对象来获取新订单电子邮件中每个产品的数据。

这是我正在使用的代码:

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>';
}

我希望在“新订单”电子邮件中的每个产品下方列出运输类别,但目前添加此代码会在结帐时返回内部服务器错误。

【问题讨论】:

    标签: php wordpress woocommerce shipping email-notifications


    【解决方案1】:

    以下将在 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;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 感谢您现在尝试并报告
    • 完美运行。谢谢你。最后一个问题,如果我想让它显示为其他电子邮件类型,我是否只需将该电子邮件类型添加到“新订单”旁边的 if 语句中?询问是因为我希望它显示在客户“新订单”电子邮件以及管理员“新订单”电子邮件中。
    • @JasonShinn 没有“客户新订单”通知...“新订单”只与管理员通知有关。
    • 我想我已经明白了,你是一个很棒的帮助。一直在阅读和使用您在此处解决其他问题的许多解决方案。非常感激!再见,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 2021-10-27
    • 2017-09-16
    • 2019-09-26
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    相关资源
    最近更新 更多