【问题标题】:Save total order items volume of WooCommerce orders as custom meta data将 WooCommerce 订单的总订单项目量保存为自定义元数据
【发布时间】:2023-03-15 07:29:01
【问题描述】:

我需要计算单笔订单的数量,并将总结果存储在数据库中,然后通过 Rest Api 检索它并在那里访问此参数。 我试图把它写下来,但在结帐时我得到内部服务器错误。

这就是我想要做的(在我的想象中)

// Store volume in the database
add_action('woocommerce_checkout_update_order_meta', 'woo_add_cart_volume');

function woo_add_cart_volume( $order_id ) {
    $order        = wc_get_order( $order_id ); // <== Was missing
    $total_volume = 0; // Initializing variable

    foreach( $order->get_items() as $item ){
        $product = $item['data'];
        $qty     = $item['quantity'];

        // Get product dimensions  
        $length = $product->get_length();
        $width  = $product->get_width();
        $height = $product->get_height();

        // Calculations a item level
        $total_volume += $length * $width * $height * $qty;
    }
    
    update_post_meta( $order_id, '_item_volume', $total_volume );
}

感谢您对我的宝贵帮助,请耐心等待。再次感谢您

【问题讨论】:

    标签: php wordpress object woocommerce orders


    【解决方案1】:

    这足以将总量存储在wp_postmeta 表中。

    • $product = $item['data']; 不正确,导致您在代码Uncaught Error: Call to a member function get_length() on null 中进一步得到以下错误。请改用$product = $item-&gt;get_product();
    • 通过在代码中添加的注释标签进行附加说明
    // Store total volume in the database (wp_postmeta table)
    function action_woocommerce_checkout_update_order_meta( $order_id ) {
        // Get $order object
        $order = wc_get_order( $order_id );
        
        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            // Initializing variable
            $total_volume = 0;
            
            // Loop through order items
            foreach( $order->get_items() as $item ) {
                // Get product object
                $product = $item->get_product();
                
                // Get quantity
                $qty = $item->get_quantity();
    
                // Get product dimensions  
                $length = $product->get_length();
                $width  = $product->get_width();
                $height = $product->get_height();
    
                // Calculations a item level
                $total_volume += $length * $width * $height * $qty;
            }
            
            // Store in wp_postmeta table
            update_post_meta( $order_id, '_item_volume', $total_volume );
        }
    }
    add_action( 'woocommerce_checkout_update_order_meta', 'action_woocommerce_checkout_update_order_meta', 10, 1 );
    

    【讨论】:

      【解决方案2】:

      您可以先将每个订单商品数量保存为自定义订单商品元数据,使用以下内容:

      add_action('woocommerce_checkout_create_order_line_item', 'woo_order_item_volume', 10, 4 );
      function woo_order_item_volume( $item, $cart_item_key, $values, $order ) {
          $product = $values['data'];
          $qty     = $values['quantity'];
      
          $length = $product->get_length();
          $width  = $product->get_width();
          $height = $product->get_height();
      
          $item->update_meta_data( '_volume', $length * $width * $height * $qty ); // Save item volume
      }
      

      然后您将订单总量保存为订单元数据,如下所示:

      add_action('woocommerce_checkout_create_order', 'woo_order_total_volume');
      function woo_order_total_volume( $order ) {
          $total_volume = 0; // Initializing
      
          // Loop through order items
          foreach( $order->get_items() as $item ){
              $total_volume += $item->get_meta( '_volume' ); // Add item volume to total
          }
          $order->update_meta_data( '_total_volume', $total_volume ); // Save total volume for order
      }
      

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

      【讨论】:

        猜你喜欢
        • 2019-09-16
        • 2019-09-16
        • 1970-01-01
        • 1970-01-01
        • 2021-05-17
        • 2021-05-21
        • 1970-01-01
        • 1970-01-01
        • 2021-04-23
        相关资源
        最近更新 更多