如何解决Wordpress Rest API 部分显示 rest_no_route 错误
我为我的自定义插件创建了一个自定义 API。当我添加更多 if 条件如下
if($product_maximum_qty < $input_weight_amt){
return new WP_Error("invalid_data",__('Product Not Found'),array( 'status' => 400 )); }
我收到 {"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}} 错误。
当我删除上面的代码时。我收到了成功响应。
add_action( 'rest_api_init','woo_weight_based_pricing_rest_api_init',10,0 );
function woo_weight_based_pricing_rest_api_init() {
$namespace = 'woo-weight-based-pricing-api';
register_rest_route($namespace,'getproductweightprices',array('args'=>array(),array('methods' => 'POST','callback' => getproductweightprice() ),));
}
function getproductweightprice(){
$json = file_get_contents('php://input');
$params = json_decode($json,TRUE);
$product_id=trim( $params['product_id'] );
$input_weight_amt=trim( $params['input_weight_amt'] );
if(!isset($product_id) || !$product_id){ return new WP_Error( "invalid_data",__('product_id is required'),array( 'status' => 400 ) ); }
$product_data = get_post_meta($product_id);
if(!$product_data){ return new WP_Error("invalid_data",array( 'status' => 400 )); }
$product_maximum_qty=isset($product_data ['wwbp_max_qty'][0]) && $product_data ['wwbp_max_qty'][0] > 0 ? $product_data ['wwbp_max_qty'][0] : 0;
if (!isset($input_weight_amt) || $input_weight_amt <= 0) { return new WP_Error( "invalid_data",__('Please Enter Valid Weight'),array( 'status' => 400 ) ); }
if($product_maximum_qty < $input_weight_amt){
return new WP_Error("invalid_data",array( 'status' => 400 ));
}
$product = wc_get_product($product_id);
$regular_price = $product->get_price();
$wastage = isset($product_data['wwbp_wastage_percentage_val'][0]) && $product_data ['wwbp_wastage_percentage_val'][0] > 0 ? $product_data ['wwbp_wastage_percentage_val'][0] : 0;
$product_weight= (float)$input_weight_amt;
$exact_westage= ($wastage/100)*$product_weight;
$actual_weight= $product_weight - $exact_westage;
$actual_price=$regular_price*$product_weight;
$actual_weight_format=number_format($actual_weight,2,'.','');
$actual_price_format= wc_price($actual_price,array('decimals' =>2));
wp_send_json_success(array('actual_weight' => $actual_weight_format,'actual_price'=>$actual_price_format));
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。