3 votes Vote Hi, I would like to access the Linked Variations via the WP REST API. But unfortunately I see that this custom Linked Variations are not exposed. How can we CRUD them via the API? Started Category: Linked Variations Peter Dogger shared this idea We would also like to access Linked Variations via REST API. We are writing code to add new & update products and would like to create Linked Variations at the same time without having to create a csv file and use WP All Import. Reply Yep, I’m at this very point myself where I’m facing the management of hundreds, potentially thousands of linked variations and the only feasible way would be to do so via the WP REST API. Reply I didn’t see there was a feature request as I’ve been trying to expose the information today. I’m almost up to a 100 linked variations and I will probably add 300 more. It’s not really feasable to generate a csv and import it when building integration tools. The info below is only sufficient for a GET. This shouldn’t be too comlicated to implement. add_filter( ‘register_post_type_args’, ‘my_post_type_args’, 10, 2 ); function my_post_type_args( $args, $post_type ) { if ( ‘cpt_iconic_wlv’ === $post_type ) { $args[‘show_in_rest’] = true; // Optionally customize the rest_base or rest_controller_class $args['rest_base'] = 'cpt_iconic_wlv'; $args['rest_controller_class'] = 'WP_REST_Posts_Controller'; } return $args; } I used the query below for some other code that might be useable for registering the field from the custom table. $wlvquery = "SELECT wlv.product_ids FROM wp_iconic_woo_linked_variations AS wlv INNER JOIN wp_posts AS p ON p.ID = wlv.post_id WHERE product_ids LIKE ‘%".$postid."%’ AND p.post_status = ‘publish’"; $retrieve_data = $wpdb->get_var($wlvquery); if (isset($retrieve_data) && !empty($retrieve_data)) { $strDB = maybe_unserialize( $retrieve_data ); $postlist = implode(‘,’, $strDB); .. Reply Here is an updated piece of code I used to get the data with rest. I will probably create a callback function for testing myself if there is no update. add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 ); function my_post_type_args( $args, $post_type ) { if ( 'cpt_iconic_wlv' === $post_type ) { $args['show_in_rest'] = true; // Optionally customize the rest_base or rest_controller_class $args['rest_base'] = 'cpt_iconic_wlv'; $args['rest_controller_class'] = 'WP_REST_Posts_Controller'; } return $args; } add_action( 'rest_api_init', 'adding_variations_wlv_rest' ); function adding_variations_wlv_rest() { register_rest_field( 'cpt_iconic_wlv', 'variation', array( 'get_callback' => 'wlv_meta_callback', 'update_callback' => null, 'schema' => null, ) ); } function wlv_meta_callback( $post, $field_name, $request) { global $wpdb; $postid=$post['id']; $retrieve_data = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_iconic_woo_linked_variations as wlv WHERE post_id = %s;", $postid)); if (isset($retrieve_data) && !empty($retrieve_data)) { $variation = array( 'wlv_id' => $retrieve_data[0]->id, 'product_ids' => maybe_unserialize($retrieve_data[0]->product_ids), 'attributes' => maybe_unserialize($retrieve_data[0]->attributes), 'show_image' => $retrieve_data[0]->show_image, 'post_id' => $retrieve_data[0]->post_id ); } return $variation; } Reply Did you ever complete this? I would so love to have a working in and out api for this. Reply ← All Requests Don't have an account? Register Already have an account? Login Username Email / Username Password Repeat Password Login Register Top Requests Dokan multivendor support for pickup and delivery slots 25 Votes Select delivery date and time slot visibility by product category 20 Votes Different angles 17 Votes Integration with WooCommerce Subscriptions 16 Votes Let the customer input their own text 13 Votes Categories Account Pages Attribute Swatches Bundled Products Custom Fields for Variations Delivery Slots Flux Checkout General Image Swap Linked Variations Product Configurator Quickview Sales Booster Show Single Variations Wishlists WooThumbs
3 votes Vote Hi, I would like to access the Linked Variations via the WP REST API. But unfortunately I see that this custom Linked Variations are not exposed. How can we CRUD them via the API? Started Category: Linked Variations Peter Dogger shared this idea We would also like to access Linked Variations via REST API. We are writing code to add new & update products and would like to create Linked Variations at the same time without having to create a csv file and use WP All Import. Reply Yep, I’m at this very point myself where I’m facing the management of hundreds, potentially thousands of linked variations and the only feasible way would be to do so via the WP REST API. Reply I didn’t see there was a feature request as I’ve been trying to expose the information today. I’m almost up to a 100 linked variations and I will probably add 300 more. It’s not really feasable to generate a csv and import it when building integration tools. The info below is only sufficient for a GET. This shouldn’t be too comlicated to implement. add_filter( ‘register_post_type_args’, ‘my_post_type_args’, 10, 2 ); function my_post_type_args( $args, $post_type ) { if ( ‘cpt_iconic_wlv’ === $post_type ) { $args[‘show_in_rest’] = true; // Optionally customize the rest_base or rest_controller_class $args['rest_base'] = 'cpt_iconic_wlv'; $args['rest_controller_class'] = 'WP_REST_Posts_Controller'; } return $args; } I used the query below for some other code that might be useable for registering the field from the custom table. $wlvquery = "SELECT wlv.product_ids FROM wp_iconic_woo_linked_variations AS wlv INNER JOIN wp_posts AS p ON p.ID = wlv.post_id WHERE product_ids LIKE ‘%".$postid."%’ AND p.post_status = ‘publish’"; $retrieve_data = $wpdb->get_var($wlvquery); if (isset($retrieve_data) && !empty($retrieve_data)) { $strDB = maybe_unserialize( $retrieve_data ); $postlist = implode(‘,’, $strDB); .. Reply Here is an updated piece of code I used to get the data with rest. I will probably create a callback function for testing myself if there is no update. add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 ); function my_post_type_args( $args, $post_type ) { if ( 'cpt_iconic_wlv' === $post_type ) { $args['show_in_rest'] = true; // Optionally customize the rest_base or rest_controller_class $args['rest_base'] = 'cpt_iconic_wlv'; $args['rest_controller_class'] = 'WP_REST_Posts_Controller'; } return $args; } add_action( 'rest_api_init', 'adding_variations_wlv_rest' ); function adding_variations_wlv_rest() { register_rest_field( 'cpt_iconic_wlv', 'variation', array( 'get_callback' => 'wlv_meta_callback', 'update_callback' => null, 'schema' => null, ) ); } function wlv_meta_callback( $post, $field_name, $request) { global $wpdb; $postid=$post['id']; $retrieve_data = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_iconic_woo_linked_variations as wlv WHERE post_id = %s;", $postid)); if (isset($retrieve_data) && !empty($retrieve_data)) { $variation = array( 'wlv_id' => $retrieve_data[0]->id, 'product_ids' => maybe_unserialize($retrieve_data[0]->product_ids), 'attributes' => maybe_unserialize($retrieve_data[0]->attributes), 'show_image' => $retrieve_data[0]->show_image, 'post_id' => $retrieve_data[0]->post_id ); } return $variation; } Reply Did you ever complete this? I would so love to have a working in and out api for this. Reply
We would also like to access Linked Variations via REST API. We are writing code to add new & update products and would like to create Linked Variations at the same time without having to create a csv file and use WP All Import. Reply
Yep, I’m at this very point myself where I’m facing the management of hundreds, potentially thousands of linked variations and the only feasible way would be to do so via the WP REST API. Reply
I didn’t see there was a feature request as I’ve been trying to expose the information today. I’m almost up to a 100 linked variations and I will probably add 300 more. It’s not really feasable to generate a csv and import it when building integration tools. The info below is only sufficient for a GET. This shouldn’t be too comlicated to implement. add_filter( ‘register_post_type_args’, ‘my_post_type_args’, 10, 2 ); function my_post_type_args( $args, $post_type ) { if ( ‘cpt_iconic_wlv’ === $post_type ) { $args[‘show_in_rest’] = true; // Optionally customize the rest_base or rest_controller_class $args['rest_base'] = 'cpt_iconic_wlv'; $args['rest_controller_class'] = 'WP_REST_Posts_Controller'; } return $args; } I used the query below for some other code that might be useable for registering the field from the custom table. $wlvquery = "SELECT wlv.product_ids FROM wp_iconic_woo_linked_variations AS wlv INNER JOIN wp_posts AS p ON p.ID = wlv.post_id WHERE product_ids LIKE ‘%".$postid."%’ AND p.post_status = ‘publish’"; $retrieve_data = $wpdb->get_var($wlvquery); if (isset($retrieve_data) && !empty($retrieve_data)) { $strDB = maybe_unserialize( $retrieve_data ); $postlist = implode(‘,’, $strDB); .. Reply
Here is an updated piece of code I used to get the data with rest. I will probably create a callback function for testing myself if there is no update. add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 ); function my_post_type_args( $args, $post_type ) { if ( 'cpt_iconic_wlv' === $post_type ) { $args['show_in_rest'] = true; // Optionally customize the rest_base or rest_controller_class $args['rest_base'] = 'cpt_iconic_wlv'; $args['rest_controller_class'] = 'WP_REST_Posts_Controller'; } return $args; } add_action( 'rest_api_init', 'adding_variations_wlv_rest' ); function adding_variations_wlv_rest() { register_rest_field( 'cpt_iconic_wlv', 'variation', array( 'get_callback' => 'wlv_meta_callback', 'update_callback' => null, 'schema' => null, ) ); } function wlv_meta_callback( $post, $field_name, $request) { global $wpdb; $postid=$post['id']; $retrieve_data = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_iconic_woo_linked_variations as wlv WHERE post_id = %s;", $postid)); if (isset($retrieve_data) && !empty($retrieve_data)) { $variation = array( 'wlv_id' => $retrieve_data[0]->id, 'product_ids' => maybe_unserialize($retrieve_data[0]->product_ids), 'attributes' => maybe_unserialize($retrieve_data[0]->attributes), 'show_image' => $retrieve_data[0]->show_image, 'post_id' => $retrieve_data[0]->post_id ); } return $variation; } Reply