1

In WooCommerce on this website, I have 2 Local Pickup shipping methods:

  • AM pickup: shipping_method_0_local_pickup31
  • PM pickup: shipping_method_0_local_pickup32

Unfortunately this shipping method does not show up on Admin Order edit pages

Is is possible to use:

add_action('woocommerce_admin_order_data_after_shipping_address','cwn_add_pickup_to_order_item_meta', 1, 2);

function cwn_add_pickup_to_order_item_meta($shipping_method) {
    echo "<h4>Pickup Time</h4>";
    echo '<p><strong>' . __('AM pickup') . ':</strong><br> ' .   get_post_meta($shipping_method->id, '_shipping_method_0_local_pickup31', true) . '</p>';
    echo '<p><strong>' . __('PM pickup') . ':</strong><br> ' . get_post_meta($shipping_method->id, '_shipping_method_0_local_pickup32', true) . '</p>';
}
starball
  • 20,030
  • 7
  • 43
  • 238
ClintN
  • 115
  • 1
  • 3
  • 9

2 Answers2

4

There is 2 similar ways to get the Shipping methods data in the WC_Order object.

1) Using WC_Abstract_Order get_shipping_methods() method. 2) Using WC_Abstract_Order get_items( 'shipping' ) method.

This will output an array of WC_Order_Item_Shipping objects. So you will need a foreach loop to use WC_Order_Item_Shipping methods to get the data, this way (Where $order is an instance of the WC_Order object):

foreach($order->get_items( 'shipping' ) as $shipping_method ){
    $method_id = $shipping_method->get_method_id().'<br>';
    $shipping_method_name = $shipping_method->get_name().'<br>';
    $shipping_method_title = $shipping_method->get_method_title().'<br>';
    $shipping_method_total = $shipping_method->get_total().'<br>';
    $shipping_method_total_tax = $shipping_method->get_total_tax().'<br>';
}

//Or:

foreach($order->get_shipping_methods() as $shipping_method ){
    $method_id = $shipping_method->get_method_id().'<br>';
    $shipping_method_name = $shipping_method->get_name().'<br>';
    $shipping_method_title = $shipping_method->get_method_title().'<br>';
    $shipping_method_total = $shipping_method->get_total().'<br>';
    $shipping_method_total_tax = $shipping_method->get_total_tax().'<br>';
}

In your code, the $shipping_method hooked function argument is wrong as it should be $order (an instance of WC_Order object.

So now you can use it in your hooked function, this way for example:

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'cwn_add_pickup_to_order_item_meta', 10, 1 );
function cwn_add_pickup_to_order_item_meta( $order ){
    echo '<div>';
    foreach($order->get_shipping_methods() as $shipping_method ){
        echo '<p><strong>Shipping Method ID:</strong> '. $shipping_method->get_method_id().'<br>
        <strong>Shipping Method name:</strong> '. $shipping_method->get_name().'<br>
        <strong>Shipping Method title:</strong> '. $shipping_method->get_method_title().'<br>
        <strong>Shipping Method Total:</strong> '. $shipping_method->get_total().'<br>
        <strong>Shipping Method Total tax:</strong> '. $shipping_method->get_total_tax().'</p><br>';
    }
    echo '</div>';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file

Tested and works on WooCommerce 3+

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
0

I was looking for a way to execute a command when finalizing a purchase and the shipping method selected was different from "LOCAL PICKUP".

Your last code gave me the light I needed. My code worked.

Follow my code to help whoever needs it:

function showpopup($order_id){
    $order = wc_get_order($order_id);
    $order->get_shipping_methods(); 
    if( ! $order->has_shipping_method('local_pickup') ) {
    
        echo '
<script>
function myFunction() {
  alert("Retirada no local NÃO SELECIONADA!");
}
myFunction()
</script>
        ';
    
    }
}   
add_action( 'woocommerce_thankyou', 'showpopup', 10, 1 );

Thank you.

lgdelai
  • 51
  • 6