繁体   English   中英

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

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

我正在尝试将每个产品下的运输类别添加到 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>';
}

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

以下将显示 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 文件中。 测试和工作。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM