在 WooCommerce 订单电子邮件通知中显示使用过的优惠券

我有一个显示已用优惠券的代码:


add_action( 'woocommerce_email_after_order_table', 'add_payment_method_to_admin_new_order', 15, 2 );

function add_payment_method_to_admin_new_order( $order, $is_admin_email ) {


        if( $order->get_used_coupons() ) {


            $coupons_count = count( $order->get_used_coupons() );

            $i = 1;

            $coupons_list = '';

            foreach( $order->get_used_coupons() as $coupon) {

                $coupons_list .=  $coupon;

                if( $i < $coupons_count )

                    $coupons_list .= ', ';

                $i++;

            }

            echo '<p></p>';

            echo '<p><strong>Купон:</strong> ' . $coupons_list . '</p>';


        } // endif get_used_coupons

}

以及在 WooCommerce 表行中显示信息的代码:


add_filter( 'woocommerce_get_order_item_totals', 'bbloomer_add_recurring_row_email', 10, 2 );

function bbloomer_add_recurring_row_email( $total_rows, $myorder_obj ) {

    $total_rows['recurr_not'] = array(

    'label' => __( 'Купон:', 'woocommerce' ),

    'value'   => 'blabla'

    );


return $total_rows;

}

如何将使用过的优惠券转移到价值字段?像这样 'value' => 'used_coupon'


当年话下
浏览 127回答 1
1回答

MMMHUHU

由于您可以通过钩子访问该$order对象woocommerce_get_order_item_totals,因此您可以以相同的方式应用它。所以你得到:// After order tablefunction action_woocommerce_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {&nbsp; &nbsp; // Get used coupons&nbsp; &nbsp; if ( $order->get_used_coupons() ) {&nbsp; &nbsp; &nbsp; &nbsp; // Total&nbsp; &nbsp; &nbsp; &nbsp; $coupons_count = count( $order->get_used_coupons() );&nbsp; &nbsp; &nbsp; &nbsp; // Initialize&nbsp; &nbsp; &nbsp; &nbsp; $i = 1;&nbsp; &nbsp; &nbsp; &nbsp; $coupons_list = '';&nbsp; &nbsp; &nbsp; &nbsp; // Loop through&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $order->get_used_coupons() as $coupon ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Append&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $coupons_list .=&nbsp; $coupon;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $i < $coupons_count )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $coupons_list .= ', ';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Output&nbsp; &nbsp; &nbsp; &nbsp; echo '<p><strong>Купон:</strong> ' . $coupons_list . '</p>';&nbsp; &nbsp; }}add_action( 'woocommerce_email_after_order_table', 'action_woocommerce_email_after_order_table', 10, 4 );// Display on customer orders and email notificationsfunction filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {&nbsp; &nbsp; // Get used coupons&nbsp; &nbsp; if ( $order->get_used_coupons() ) {&nbsp; &nbsp; &nbsp; &nbsp; // Total&nbsp; &nbsp; &nbsp; &nbsp; $coupons_count = count( $order->get_used_coupons() );&nbsp; &nbsp; &nbsp; &nbsp; // Initialize&nbsp; &nbsp; &nbsp; &nbsp; $i = 1;&nbsp; &nbsp; &nbsp; &nbsp; $coupons_list = '';&nbsp; &nbsp; &nbsp; &nbsp; // Loop through&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $order->get_used_coupons() as $coupon ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Append&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $coupons_list .=&nbsp; $coupon;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $i < $coupons_count )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $coupons_list .= ', ';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Output&nbsp; &nbsp; &nbsp; &nbsp; $total_rows['recurr_not'] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label'&nbsp; &nbsp; &nbsp;=> __( 'Купон:', 'woocommerce' ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value'&nbsp; &nbsp; &nbsp;=> $coupons_list,&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }&nbsp; &nbsp; return $total_rows;}add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );
打开App,查看更多内容
随时随地看视频慕课网APP