在Woocommerce REST API中获取表费率送货数据

时间:2017-04-29 07:34:39

标签: rest api shipping

我正在尝试在Woocommerce REST API中获取所有有效的送货方式。我怎样才能做到这一点。我是woocommerce REST API的新手,非常感谢任何帮助。

我想要回复这样的事情 -

{ "shipping_methods": { "method_id": "method_title" } }

1 个答案:

答案 0 :(得分:0)

最后,我想出了解决方案。它可能会帮助别人。 以下是获取所有表格运费数据的完整代码,包括运输区域,按地区划分的运输方式和按地区划分的费率 -

 header('Content-type: application/json');
 $json_file=file_get_contents('php://input');
 $jsonvalue= json_decode($json_file,true);

 global $wpdb;
 global $woocommerce;

 $active_methods   = array();

$shipping_methods = $woocommerce->shipping->load_shipping_methods();
//echo "<pre>";print_r($shipping_methods);

foreach ( $shipping_methods as $id => $shipping_method ) {

 if ( isset( $shipping_method->enabled ) && 'yes' === $shipping_method->enabled ) {

        $data_arr = array( 'title' => $shipping_method->title, 'tax_status' => $shipping_method->tax_status );  

    if($id=='table_rate'){          

     $raw_zones = $wpdb->get_results("SELECT zone_id, zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones order by zone_order ASC;");

   //echo "<pre>";print_r($raw_zones);

    $shipping = array();
    $shippingarr = array();
    foreach ($raw_zones as $raw_zone) {

         $zones = new WC_Shipping_Zone($raw_zone->zone_id);

         $zone_id = $zones->zone_id; 
         $zone_name = $zones->zone_name; 
         $zone_enabled = $zones->zone_enabled; 
         $zone_type = $zones->zone_type; 
         $zone_order = $zones->zone_order; 

         $shipping['zone_id'] = $zone_id;
         $shipping['zone_name'] = $zone_name;
         $shipping['zone_enabled'] = $zone_enabled;
         $shipping['zone_type'] = $zone_type;
         $shipping['zone_order'] = $zone_order;

         $shipping_methods = $zones->shipping_methods; 
         foreach($shipping_methods as $shipping_method){

    $methodid = $shipping_method["number"];
             $raw_rates[$methodid]['rates'] = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}woocommerce_shipping_table_rates WHERE shipping_method_id={$methodid};",ARRAY_A);


         }

     $shipping['shipping_methods'] = $raw_rates;

    $raw_country = $wpdb->get_results("SELECT location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE zone_id={$zone_id};",ARRAY_N);

         $shipping['countries'] = $raw_country;

         $shippingarr[] = $shipping;


    }

        $data_arr['shipping_zones'] = $shippingarr; 

        }

        $active_methods[ $id ] = $data_arr;

    }

  } 


     if(!empty($shippingarr)){

     $result['success']='true';
     $result['error']="0";
     $result['msg']='Shipping methos found.';  
     $result['data']=$active_methods; 

     }else{

    $result['success']='true';
    $result['error']="0";
    $result['msg']='Shipping methos found.';  
    $result['data']= array();    

    } 

    echo json_encode($result); `