Add menu order to categories and attributes

This commit is contained in:
Daniel Hüsken 2016-04-22 19:55:52 +02:00
parent 2ab177a35e
commit 35a1cd4736
2 changed files with 41 additions and 6 deletions

View File

@ -39,16 +39,21 @@ class WC_REST_Product_Attribute_Terms_Controller extends WC_REST_Terms_Controlle
/**
* Prepare a single product attribute term output for response.
*
* @param obj $item Term object.
* @param WP_Term $item Term object.
* @param WP_REST_Request $request
* @return WP_REST_Response $response
*/
public function prepare_item_for_response( $item, $request ) {
// Get term order
$menu_order = get_woocommerce_term_meta( $item->term_id, 'order_' . $this->taxonomy );
$data = array(
'id' => (int) $item->term_id,
'name' => $item->name,
'slug' => $item->slug,
'count' => (int) $item->count,
'id' => (int) $item->term_id,
'name' => $item->name,
'slug' => $item->slug,
'menu_order' => (int) $menu_order,
'count' => (int) $item->count,
);
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
@ -71,6 +76,21 @@ class WC_REST_Product_Attribute_Terms_Controller extends WC_REST_Terms_Controlle
return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );
}
/**
* Update term meta fields.
*
* @param WP_Term $term
* @param WP_REST_Request $request
* @return bool|WP_Error
*/
protected function update_term_meta_fields( $term, $request ) {
$id = (int) $term->term_id;
update_woocommerce_term_meta( $id, 'order_' . $this->taxonomy, $request['menu_order'] );
return true;
}
/**
* Get the Attribute Term's schema, conforming to JSON Schema.
*
@ -104,6 +124,11 @@ class WC_REST_Product_Attribute_Terms_Controller extends WC_REST_Terms_Controlle
'sanitize_callback' => 'sanitize_title',
),
),
'menu_order' => array(
'description' => __( 'Menu order, used to custom sort the resource.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'woocommerce' ),
),
'count' => array(
'description' => __( 'Number of published products for the resource.', 'woocommerce' ),
'type' => 'integer',

View File

@ -46,7 +46,7 @@ class WC_REST_Product_Categories_Controller extends WC_REST_Terms_Controller {
/**
* Prepare a single product category output for response.
*
* @param obj $item Term object.
* @param WP_Term $item Term object.
* @param WP_REST_Request $request
* @return WP_REST_Response $response
*/
@ -60,6 +60,9 @@ class WC_REST_Product_Categories_Controller extends WC_REST_Terms_Controller {
$image = wp_get_attachment_url( $image_id );
}
// Get category order
$menu_order = get_woocommerce_term_meta( $item->term_id, 'order' );
$data = array(
'id' => (int) $item->term_id,
'name' => $item->name,
@ -68,6 +71,7 @@ class WC_REST_Product_Categories_Controller extends WC_REST_Terms_Controller {
'description' => $item->description,
'display' => $display_type ? $display_type : 'default',
'image' => $image ? esc_url( $image ) : '',
'menu_order' => (int) $menu_order,
'count' => (int) $item->count,
);
@ -102,6 +106,7 @@ class WC_REST_Product_Categories_Controller extends WC_REST_Terms_Controller {
$id = (int) $term->term_id;
update_woocommerce_term_meta( $id, 'display_type', $request['display'] );
update_woocommerce_term_meta( $id, 'order', $request['menu_order'] );
if ( ! empty( $request['image'] ) ) {
$upload = wc_rest_upload_image_from_url( esc_url_raw( $request['image'] ) );
@ -180,6 +185,11 @@ class WC_REST_Product_Categories_Controller extends WC_REST_Terms_Controller {
'format' => 'uri',
'context' => array( 'view', 'edit' ),
),
'menu_order' => array(
'description' => __( 'Menu order, used to custom sort the resource.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
),
'count' => array(
'description' => __( 'Number of published products for the resource.', 'woocommerce' ),
'type' => 'integer',