Fix issues based on feedback. Make sure to use WC_Shipping_Zone's methods when avaiable, fix up some coding standards, and add a new delete_shipping_method to WC_Shipping_Zone
This commit is contained in:
parent
147c18c197
commit
849a208e54
|
@ -93,14 +93,14 @@ class WC_REST_Shipping_Methods_Controller extends WC_REST_Controller {
|
||||||
* @return WP_Error|WP_REST_Response
|
* @return WP_Error|WP_REST_Response
|
||||||
*/
|
*/
|
||||||
public function get_items( $request ) {
|
public function get_items( $request ) {
|
||||||
$wc_shipping = WC_Shipping::instance();
|
$wc_shipping = WC_Shipping::instance();
|
||||||
$response = array();
|
$response = array();
|
||||||
foreach ( $wc_shipping->get_shipping_methods() as $id => $shipping_method ) {
|
foreach ( $wc_shipping->get_shipping_methods() as $id => $shipping_method ) {
|
||||||
$method = $this->prepare_item_for_response( $shipping_method, $request );
|
$method = $this->prepare_item_for_response( $shipping_method, $request );
|
||||||
$method = $this->prepare_response_for_collection( $method );
|
$method = $this->prepare_response_for_collection( $method );
|
||||||
$response[] = $method;
|
$response[] = $method;
|
||||||
}
|
}
|
||||||
return rest_ensure_response( $response );
|
return rest_ensure_response( $response );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -112,7 +112,7 @@ class WC_REST_Shipping_Methods_Controller extends WC_REST_Controller {
|
||||||
public function get_item( $request ) {
|
public function get_item( $request ) {
|
||||||
$wc_shipping = WC_Shipping::instance();
|
$wc_shipping = WC_Shipping::instance();
|
||||||
$methods = $wc_shipping->get_shipping_methods();
|
$methods = $wc_shipping->get_shipping_methods();
|
||||||
if ( empty ( $methods[ $request['id'] ] ) ) {
|
if ( empty( $methods[ $request['id'] ] ) ) {
|
||||||
return new WP_Error( 'woocommerce_rest_shipping_method_invalid', __( "Resource doesn't exist.", 'woocommerce' ), array( 'status' => 404 ) );
|
return new WP_Error( 'woocommerce_rest_shipping_method_invalid', __( "Resource doesn't exist.", 'woocommerce' ), array( 'status' => 404 ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -140,36 +140,9 @@ class WC_REST_Shipping_Zone_Methods_Controller extends WC_REST_Shipping_Zones_Co
|
||||||
return $zone;
|
return $zone;
|
||||||
}
|
}
|
||||||
|
|
||||||
$instance_id = 0;
|
$instance_id = $zone->add_shipping_method( $method_id ) ;
|
||||||
$wc_shipping = WC_Shipping::instance();
|
|
||||||
$allowed_classes = $wc_shipping->get_shipping_method_class_names();
|
|
||||||
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d", $zone->get_id() ) );
|
|
||||||
if ( in_array( $method_id, array_keys( $allowed_classes ) ) ) {
|
|
||||||
$wpdb->insert(
|
|
||||||
$wpdb->prefix . 'woocommerce_shipping_zone_methods',
|
|
||||||
array(
|
|
||||||
'method_id' => $method_id ,
|
|
||||||
'zone_id' => $zone->get_id(),
|
|
||||||
'method_order' => ( $count + 1 )
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'%s',
|
|
||||||
'%d',
|
|
||||||
'%d'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$instance_id = $wpdb->insert_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( $instance_id ) {
|
|
||||||
do_action( 'woocommerce_shipping_zone_method_added', $instance_id, $method_id, $zone->get_id() );
|
|
||||||
}
|
|
||||||
|
|
||||||
WC_Cache_Helper::get_transient_version( 'shipping', true );
|
|
||||||
|
|
||||||
$methods = $zone->get_shipping_methods();
|
$methods = $zone->get_shipping_methods();
|
||||||
$method = false;
|
$method = false;
|
||||||
|
|
||||||
foreach ( $methods as $method_obj ) {
|
foreach ( $methods as $method_obj ) {
|
||||||
if ( $instance_id === $method_obj->instance_id ) {
|
if ( $instance_id === $method_obj->instance_id ) {
|
||||||
$method = $method_obj;
|
$method = $method_obj;
|
||||||
|
@ -178,7 +151,7 @@ class WC_REST_Shipping_Zone_Methods_Controller extends WC_REST_Shipping_Zones_Co
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( false === $method ) {
|
if ( false === $method ) {
|
||||||
return new WP_Error( 'woocommerce_rest_shipping_zone_not_created', __( "Resource cannot be created.", 'woocommerce' ), array( 'status' => 500 ) );
|
return new WP_Error( 'woocommerce_rest_shipping_zone_not_created', __( 'Resource cannot be created.', 'woocommerce' ), array( 'status' => 500 ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
$method = $this->update_fields( $instance_id, $method, $request );
|
$method = $this->update_fields( $instance_id, $method, $request );
|
||||||
|
@ -203,6 +176,7 @@ class WC_REST_Shipping_Zone_Methods_Controller extends WC_REST_Shipping_Zones_Co
|
||||||
|
|
||||||
$instance_id = (int) $request['instance_id'];
|
$instance_id = (int) $request['instance_id'];
|
||||||
$force = $request['force'];
|
$force = $request['force'];
|
||||||
|
|
||||||
$methods = $zone->get_shipping_methods();
|
$methods = $zone->get_shipping_methods();
|
||||||
$method = false;
|
$method = false;
|
||||||
|
|
||||||
|
@ -221,8 +195,9 @@ class WC_REST_Shipping_Zone_Methods_Controller extends WC_REST_Shipping_Zones_Co
|
||||||
$request->set_param( 'context', 'view' );
|
$request->set_param( 'context', 'view' );
|
||||||
$response = $this->prepare_item_for_response( $method, $request );
|
$response = $this->prepare_item_for_response( $method, $request );
|
||||||
|
|
||||||
|
// Actually delete
|
||||||
if ( $force ) {
|
if ( $force ) {
|
||||||
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( 'instance_id' => $instance_id ) );
|
$zone->delete_shipping_method( $instance_id ) ;
|
||||||
} else {
|
} else {
|
||||||
return new WP_Error( 'rest_trash_not_supported', __( 'Shipping methods do not support trashing.' ), array( 'status' => 501 ) );
|
return new WP_Error( 'rest_trash_not_supported', __( 'Shipping methods do not support trashing.' ), array( 'status' => 501 ) );
|
||||||
}
|
}
|
||||||
|
@ -357,12 +332,12 @@ class WC_REST_Shipping_Zone_Methods_Controller extends WC_REST_Shipping_Zones_Co
|
||||||
$data = array(
|
$data = array(
|
||||||
'id' => $id,
|
'id' => $id,
|
||||||
'label' => $field['title'],
|
'label' => $field['title'],
|
||||||
'description' => ( empty( $field['description'] ) ? '' : $field['description'] ),
|
'description' => empty( $field['description'] ) ? '' : $field['description'],
|
||||||
'type' => $field['type'],
|
'type' => $field['type'],
|
||||||
'value' => $item->instance_settings[ $id ],
|
'value' => $item->instance_settings[ $id ],
|
||||||
'default' => ( empty( $field['default'] ) ? '' : $field['default'] ),
|
'default' => empty( $field['default'] ) ? '' : $field['default'],
|
||||||
'tip' => ( empty( $field['description'] ) ? '' : $field['description'] ),
|
'tip' => empty( $field['description'] ) ? '' : $field['description'],
|
||||||
'placeholder' => ( empty( $field['placeholder'] ) ? '' : $field['placeholder'] ),
|
'placeholder' => empty( $field['placeholder'] ) ? '' : $field['placeholder'],
|
||||||
);
|
);
|
||||||
if ( ! empty( $field['options'] ) ) {
|
if ( ! empty( $field['options'] ) ) {
|
||||||
$data['options'] = $field['options'];
|
$data['options'] = $field['options'];
|
||||||
|
|
|
@ -487,4 +487,25 @@ class WC_Shipping_Zone extends WC_Data {
|
||||||
|
|
||||||
return $instance_id;
|
return $instance_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a shipping method from a zone.
|
||||||
|
* @param int $instance_id
|
||||||
|
* @return True on success, false on failure
|
||||||
|
*/
|
||||||
|
public function delete_shipping_method( $instance_id ) {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
if ( null === $this->get_id() ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( 'instance_id' => $instance_id ) );
|
||||||
|
do_action( 'woocommerce_shipping_zone_method_deleted', $instance_id, $this->get_id() );
|
||||||
|
|
||||||
|
WC_Cache_Helper::get_transient_version( 'shipping', true );
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue