• Resolved neosan

    (@nreljed)


    Hello ,
    I have this code to display the subtotal item weight for cart and order items

    // Display the cart item weight in cart and checkout pages
    add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
    function display_custom_item_data( $cart_item_data, $cart_item ) {
        if ( $cart_item['data']->get_weight() > 0 ){
            $cart_item_data[] = array(
                'name' => __( 'Weight subtotal', 'woocommerce' ),
                'value' =>  ( $cart_item['quantity'] * $cart_item['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit')
            );
        }
        return $cart_item_data;
    }
    
    // Save and Display the order item weight (everywhere)
    add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );
    function display_order_item_data( $item, $cart_item_key, $values, $order ) {
        if ( $values['data']->get_weight() > 0 ){
            $item->update_meta_data( __( 'Weight subtotal', 'woocommerce' ), ( $cart_item['quantity'] * $cart_item['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit') );
        }
    }
    

    This work great in both cart-page and checkout-page when I click “Place order” I got an “Internal Server Error” something changed in woocommerce 3.8 or the code is deprecated.

    I googled it and found so many codes, nothing works.
    I can’t understand what’s wrong here.
    Is there anyone who understands what’s going on here

    the problematic part is here

    // Save and Display the order item weight (everywhere)
    add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );
    function display_order_item_data( $item, $cart_item_key, $values, $order ) {
        if ( $values['data']->get_weight() > 0 ){
            $item->update_meta_data( __( 'Weight subtotal', 'woocommerce' ), ( $cart_item['quantity'] * $cart_item['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit') );
        }
    }
    • This topic was modified 5 years, 3 months ago by neosan.
    • This topic was modified 5 years, 3 months ago by neosan.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @nreljed!

    Try the following code:

    add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
    function display_custom_item_data( $cart_item_data, $cart_item ) {
        if ( $cart_item['data']->get_weight() > 0 ){
            $cart_item_data[] = array(
                'name' => __( 'Weight', 'woocommerce' ),
                'value' =>  $cart_item['data']->get_weight()  . ' ' . get_option('woocommerce_weight_unit')
            );
        }
        return $cart_item_data;
    }
    
    add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );
    function display_order_item_data( $item, $cart_item_key, $values, $order ) {
        if ( $values['data']->get_weight() > 0 ){
            $item->update_meta_data( __( 'Weight subtotal', 'woocommerce' ), ( $item['quantity'] * $values['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit') );
        }
    }
    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Howdy!

    We haven’t heard back from you in a while, so I’m going to go ahead and mark this thread as resolved. If you have any other questions please start a new thread.

    Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Place order = “Internal Server Error”’ is closed to new replies.