Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

php - Save total order items volume of WooCommerce orders as custom meta data

I need to calculate the volume of single order, and store the totale result in the DB to then retrieve it trough the Rest Api and access this parameter there. i tried to write it down something, but in the checkout i get Internal server error.

This is what I am trying to do (in my imagination):

// 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 );
}

Thanks for you precious help be patient with me. Thank you again

question from:https://stackoverflow.com/questions/65938341/save-total-order-items-volume-of-woocommerce-orders-as-custom-meta-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should suffice, to store the total volume in the wp_postmeta table.

  • $product = $item['data']; is not correct, causing you to get the following error further in the code Uncaught Error: Call to a member function get_length() on null. Use $product = $item->get_product(); instead
  • Additional explanation via comment tags added in the code
// 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 );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...