【问题标题】:Show Shipping Method data on the Order edit pages in WooCommerce在 WooCommerce 的订单编辑页面上显示运输方式数据
【发布时间】:2018-04-18 09:14:06
【问题描述】:

this website 上的 WooCommerce 中,我有 2 种本地取货运输方式:

  • 上午接机:shipping_method_0_local_pickup31
  • 下午取货:shipping_method_0_local_pickup32

很遗憾,这种送货方式没有显示在管理员订单编辑页面上

可以使用: 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>';
}

【问题讨论】:

    标签: php wordpress woocommerce shipping orders


    【解决方案1】:

    我正在寻找一种在完成购买时执行命令的方法,但选择的运输方式与“本地取货不同强>”。

    你最后的代码给了我我需要的光。 我的代码有效。

    按照我的代码帮助有需要的人:

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

    谢谢。

    【讨论】:

      【解决方案2】:

      有两种类似的方法可以在 WC_Order 对象中获取 Shipping 方法数据。

      1) 使用 WC_Abstract_Order get_shipping_methods() 方法。 2) 使用 WC_Abstract_Order get_items( 'shipping' ) 方法。

      这将输出WC_Order_Item_Shipping 对象的数组。因此,您将需要一个 foreach 循环来使用 WC_Order_Item_Shipping 方法来获取数据,这样 (其中 $order 的一个实例WC_Order 对象)

      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>';
      }
      

      在您的代码中,$shipping_method 挂钩函数参数是错误的,因为它应该是 $orderWC_Order object 的实例>.

      所以现在你可以在你的钩子函数中使用它,例如:

      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>';
      }
      

      代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中

      在 WooCommerce 3+ 上测试并运行

      【讨论】:

      • 像魅力一样工作谢谢 Loic TheAztec
      猜你喜欢
      • 2023-03-24
      • 2020-10-29
      • 2020-10-30
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      相关资源
      最近更新 更多