将优惠券代码名称添加到Woocommerce查看订单详细信息和电子邮件通知

用户2337231

我注意到在“查看订单详细信息”和“电子邮件确认”中,它反映了折扣行,但没有说明所使用的实际折扣代码。此外,如果折扣代码为$ 0.00(有时出于特殊跟踪目的,我们会有$ 0的代码),则根本不会显示该代码。我整天都在努力寻找解决方案-有人可以为此提供一些指导吗?谢谢。

到目前为止,我已经完成了这项工作,以获取实际的优惠券代码:

add_action( 'woocommerce_order_details_after_order_table', 'custom_woocommerce_coupon_line' );
function custom_woocommerce_coupon_line( $order_id ) {
    $order    = wc_get_order( $order_id );

    // An order can have no used coupons or also many used coupons
    $coupons  = $order->get_used_coupons();
    $coupons  = count($coupons) > 0 ? implode(',', $coupons) : '';
    echo $coupons;
 }

但是无法弄清楚如何使其进入“折扣”行...也不为什么当折扣行是使用$ 0代码的$ 0项目时甚至没有出现。

LoicTheAztec

更新-处理零值的折扣

以下代码将在订单总数行的“折扣”行之后,显示该订单应用的优惠券:

add_filter( 'woocommerce_get_order_item_totals', 'add_coupons_codes_line_to_order_totals_lines', 10, 3 );
function add_coupons_codes_line_to_order_totals_lines( $total_rows, $order, $tax_display ) {
    // Exit if there is no coupons applied
    if( sizeof( $order->get_used_coupons() ) == 0 )
        return $total_rows;

    $new_total_rows = []; // Initializing

    foreach($total_rows as $key => $total ){
        $new_total_rows[$key] = $total;

        if( $key == 'discount' ){
            // Get applied coupons
            $applied_coupons = $order->get_used_coupons();
            // Insert applied coupon codes in total lines after discount line
            $new_total_rows['coupon_codes'] = array(
                'label' => __('Applied coupons:', 'woocommerce'),
                'value' => implode( ', ', $applied_coupons ),
            );
        }
    }

    return $new_total_rows;
}

在客户订单视图上显示,并带有2个已应用的优惠券:

在此处输入图片说明


附加代码版本:处理贴现量为零的优惠券时,请改用以下代码:

add_filter( 'woocommerce_get_order_item_totals', 'add_coupons_codes_line_to_order_totals_lines', 10, 3 );
function add_coupons_codes_line_to_order_totals_lines( $total_rows, $order, $tax_display ) {
    $has_used_coupons = sizeof( $order->get_used_coupons() ) > 0 ? true : false;

    // Exit if there is no coupons applied
    if( ! $has_used_coupons )
        return $total_rows;

    $new_total_rows  = []; // Initializing
    $applied_coupons = $order->get_used_coupons(); // Get applied coupons

    foreach($total_rows as $key => $total ){
        $new_total_rows[$key] = $total;

        // Adding the discount line for orders with applied coupons and zero discount amount
        if( ! isset($total_rows['discount']) && $key === 'shipping' ) {
            $new_total_rows['discount'] = array(
                'label' => __( 'Discount:', 'woocommerce' ),
                'value'    => wc_price(0),
            );
        }

        // Adding applied coupon codes line
        if( $key === 'discount' || isset($new_total_rows['discount']) ){
            // Get applied coupons
            $applied_coupons = $order->get_used_coupons();
            // Insert applied coupon codes in total lines after discount line
            $new_total_rows['coupon_codes'] = array(
                'label' => __('Applied coupons:', 'woocommerce'),
                'value' => implode( ', ', $applied_coupons ),
            );
        }
    }

    return $new_total_rows;
}

在带有0折扣的优惠券的电子邮件通知上显示:

在此处输入图片说明


代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将应用的优惠券描述添加到 WooCommerce 管理员电子邮件通知

仅当客户未使用优惠券时,才将优惠券添加到处理订单电子邮件中

在WooCommerce订单上使用优惠券代码时发送电子邮件通知

如何将我的自定义字段添加到 Woocommerce 订单流程(查看表单、订单详细信息和电子邮件流程)

将收件人添加到 Woocommerce 电子邮件中的“客户发票/订单详细信息”

从优惠券代码WooCommerce检索优惠券详细信息

在 WooCommerce 中应用特定优惠券代码时发送电子邮件通知

从WooCommerce处理订单电子邮件通知中删除订单详细信息

Woocommerce电子邮件通知中的样式订单详细信息表

Woocommerce电子邮件订单详细信息中的其他列和项目值

如何将优惠券添加到WooCommerce产品页面

Magento将优惠券代码添加到发票折扣中

自动生成的优惠券未显示在订单电子邮件中

在Woocommerce下订单后,将附件添加到管理员电子邮件通知中

如何在 WooCommerce 中高效地从电子邮件限制中获取优惠券

WooCommerce:手动验证优惠券用户电子邮件

在Woocommerce中成功购买后更改优惠券电子邮件限制

如何在新用户注册电子邮件模板中发送优惠券代码?

将新订单电子邮件通知附件添加到供应商电子邮件中

在WooCommerce电子邮件中查找/编辑订单详细信息元文本

在Woocommerce电子邮件模板中获取订单详细信息

如何在人们提交电子邮件时通过电子邮件发送优惠券代码

如何将产品SKU添加到WooCommerce新订单电子邮件主题

WooCommerce - 将产品简短描述添加到已完成的订单电子邮件

从WooCommerce订单中获取二手优惠券代码和相关折扣金额

在Woocommerce中将订单交易ID添加到管理员新订单电子邮件通知中

如何在 Flutter 中的电子邮件验证后将用户详细信息添加到 Firestore DB?

Firebase 电子邮件身份验证将用户名和详细信息添加到 Firebase 数据库

在 WooCommerce 电子邮件通知中重新排列客户详细信息