• Dalton

    (@daltoncleveland)


    On the checkout page of my WooCommerce based site, users will have a list of shipping methods to choose from depending on what they are purchasing. Things like “Free Shipping” for orders over a certain price, “Freight Shipping” for certain items, and so on. My goal, is to capture that list of available shipping options for each order, and display it on the “Edit Order / Order Details” page in the Admin view. A small feature that would help us be able to quickly identify what option people are choosing more often, depending on the options that they have available.

    Previously, using custom code I was able to capture the order weight using this code:

    add_action( 'woocommerce_checkout_update_order_meta', 'bbloomer_save_weight_order' );
     
    function bbloomer_save_weight_order( $order_id ) {
        $weight = WC()->cart->get_cart_contents_weight();
        update_post_meta( $order_id, '_cart_weight', $weight );
    }
     
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'bbloomer_delivery_weight_display_admin_order_meta', 10, 1 );
      
    function bbloomer_delivery_weight_display_admin_order_meta( $order ) {    
        echo '<p><strong>Order Weight:</strong> ' . get_post_meta( $order->get_id(), '_cart_weight', true ) . get_option( 'woocommerce_weight_unit' ) . '</p>';
    }

    Following that same idea since it does what I want, what would I need to do for the available shipping methods? This is what I have so far:

    add_action( 'woocommerce_checkout_update_order_meta', 'save_available_shipping_methods' );
     
    function save_available_shipping_methods( $order_id ) {
        $shippingmethods = WC()->cart->get_shipping_methods();
        update_post_meta( $order_id, '_shipping_methods', $shippingmethods );
    }
    
    add_action( 'woocommerce_admin_order_data_after_shipping_address', 'get_available_shipping_methods', 10, 1 );
    
    function get_available_shipping_methods($order){
    	$order = wc_get_order( $order_id );
    	if ( $order ) {
        	echo '<p><strong>'.__('Available Shipping Methods: ').'</strong> ' . get_post_meta($order->get_shipping_methods(), '_shipping_field_value', true ) . '</p>';
    	}
    }
  • The topic ‘Custom Order Details Field’ is closed to new replies.