【问题标题】:Save value from a custom row added after the order table and display it in WooCommerce orders and emails保存订单表后添加的自定义行中的值并将其显示在 WooCommerce 订单和电子邮件中
【发布时间】:2022-01-08 20:42:46
【问题描述】:

我编写的这段代码在 WooCommerce 结帐页面上显示个性化信息。

add_action( 'woocommerce_cart_totals_after_order_total', 'show_total_discount_cart_checkout', 9999 );
add_action( 'woocommerce_review_order_after_order_total', 'show_total_discount_cart_checkout', 9999 );
 
function show_total_discount_cart_checkout() {
    
   $discount_total = 0;
    
    
   foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {     
   $product = $cart_item['data'];
       
   $subtotal = WC()->cart->get_product_subtotal( $product, $cart_item['quantity'] );
        
   $total = WC()->cart->total;
   $pctm = 90.00;
   $valor_descontado = $total - ($total / 100 * $pctm); 
        
   $sale_price = '10%'; 
   $discount = ( WC()->cart->total - $valor_descontado );
   $discount_total = $discount;
   
}                
    if ( $discount_total > 0 ) {
      echo '<tr><th>VOCÊ RECEBERÁ DE CASHBACK:</th><td data-title="You">' . wc_price( $discount_total + WC()->cart->get_discount_total() ) .'</td></tr>';
    }
  
} 

结果:


我需要此信息也显示在 WooCommerce 订单和电子邮件中。我相信我可以自己想出一个解决方案来在其他几个页面上显示这个值,但是有人可以先告诉我如何保存/存储这个计算的值吗?

【问题讨论】:

    标签: wordpress woocommerce checkout orders email-notifications


    【解决方案1】:

    首先,我重写了您现有的代码,原因如下:

    • 请求小计和总计最好在foreach循环之外完成,否则每次都会覆盖这些值
    • $subtotal 的结果不会在您的代码中的任何地方使用
    • 由于$subtotal 的结果无论如何都没有被使用,循环购物车似乎是不必要的
    • $sale_price 也不会在您的代码中的任何地方使用
    • 由于$discount_total = $discount,不需要使用新变量
    • 创建/添加了会话变量

    您现有的代码,但已优化:

    function action_woocommerce_after_order_total() {
        // WC Cart NOT null
        if ( ! is_null( WC()->cart ) ) {
            // Get cart
            $cart = WC()->cart;
            
            // Getters
            $cart_total = $cart->total;
            $cart_discount_total = $cart->get_discount_total();
            
            // Settings
            $pctm = 90;
            
            // Calculations
            $discounted_value = $cart_total - ( $cart_total / 100 * $pctm );
            $discount_total = $cart_total - $discounted_value;
            
            // Greater than
            if ( $discount_total > 0 ) {
                // Result
                $result = $discount_total + $cart_discount_total;
                
                // The Output
                echo '<tr class="my-class">
                        <th>' . __( 'VOCÊ RECEBERÁ DE CASHBACK', 'woocommerce' ) . '</th>
                        <td data-title="You">' . wc_price( $result ) . '</td>
                      </tr>';
                
                // Set session
                WC()->session->set( 'session_result', $result );
            }
        }
    }
    add_action( 'woocommerce_cart_totals_after_order_total', 'action_woocommerce_after_order_total', 10 );
    add_action( 'woocommerce_review_order_after_order_total', 'action_woocommerce_after_order_total', 10 );
    

    回答您的问题:

    第 1 步) 我们从会话变量中获取结果并将其添加为订单数据,以便我们可以通过 $order 对象在任何地方使用/获取此信息

    // Add as custom order meta data and reset WC Session variable
    function action_woocommerce_checkout_create_order( $order, $data ) {    
        // Isset
        if ( WC()->session->__isset( 'session_result' ) ) {
            // Get
            $result = (float) WC()->session->get( 'session_result' );
            
            // Add as meta data
            $order->update_meta_data( 'result', $result );
            
            // Unset
            WC()->session->__unset( 'session_result' );
        }
    }
    add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );
    

    第 2 步) 使用 woocommerce_get_order_item_totals 过滤器挂钩,这将允许您使用 $result 向现有表中添加新行。

    新行将被添加到:

    • 电子邮件通知
    • 订单已收到(感谢页面)
    • 我的账户 -> 查看订单
    function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {    
        // Get meta
        $result = $order->get_meta( 'result' );
        
        // NOT empty
        if ( ! empty ( $result ) ) {
            // Add new row
            $total_rows['total_result']['label'] = __( 'VOCÊ RECEBERÁ DE CASHBACK', 'woocommerce' );
            $total_rows['total_result']['value'] = wc_price( $result );
        }
    
        return $total_rows;
    }
    add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );
    

    【讨论】:

      猜你喜欢
      • 2021-04-11
      • 2020-11-11
      • 2016-03-03
      • 1970-01-01
      • 2018-07-11
      • 2019-09-22
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多