• Resolved asafmoraz

    (@asafmoraz)


    I'm trying to update custom taxonimies "brands" created with jetengine plugin through rest api remotely and i've added the following snippet:



    // Register the 'brands' field for the 'product' post type in the REST API

    add_action('rest_api_init', function() {

    ? ? register_rest_field('product', 'brands', array(

    ? ? ? ? 'get_callback' => function($product) {

    ? ? ? ? ? ? return wp_get_post_terms($product['id'], 'brands');

    ? ? ? ? },

    ? ? ? ? 'update_callback' => function($meta, $product) {

    ? ? ? ? ? ? if (!empty($meta)) {

    ? ? ? ? ? ? ? ? $term_ids = array();

    ? ? ? ? ? ? ? ? foreach ($meta as $term) {

    ? ? ? ? ? ? ? ? ? ? if (isset($term['term_id']) && is_numeric($term['term_id'])) {

    ? ? ? ? ? ? ? ? ? ? ? ? $term_ids[] = (int) $term['term_id'];

    ? ? ? ? ? ? ? ? ? ? }

    ? ? ? ? ? ? ? ? }

    ? ? ? ? ? ? ? ? if (!empty($term_ids)) {

    ? ? ? ? ? ? ? ? ? ? wp_set_post_terms($product->ID, $term_ids, 'brands');

    ? ? ? ? ? ? ? ? }

    ? ? ? ? ? ? }

    ? ? ? ? ? ? return true; // Return true to indicate the update was successful

    ? ? ? ? },

    ? ? ? ? 'schema' => null,

    ? ? ));

    });

    // Handle PUT request to update the 'brands' taxonomy via API

    add_action('rest_api_init', function() {

    ? ? register_rest_route('my_custom_namespace/v1', '/product/(?P<id>\d+)/brands', array(

    ? ? ? ? 'methods' => 'PUT',

    ? ? ? ? 'callback' => function($request) {

    ? ? ? ? ? ? $product_id = (int) $request['id'];

    ? ? ? ? ? ? $terms = $request->get_param('brands');

    ? ? ? ? ? ? if (!empty($terms) && is_array($terms)) {

    ? ? ? ? ? ? ? ? $term_ids = array();

    ? ? ? ? ? ? ? ? foreach ($terms as $term) {

    ? ? ? ? ? ? ? ? ? ? if (is_numeric($term)) {

    ? ? ? ? ? ? ? ? ? ? ? ? $term_ids[] = (int) $term;

    ? ? ? ? ? ? ? ? ? ? } else {

    ? ? ? ? ? ? ? ? ? ? ? ? $term_obj = get_term_by('name', $term, 'brands');

    ? ? ? ? ? ? ? ? ? ? ? ? if ($term_obj) {

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? $term_ids[] = $term_obj->term_id;

    ? ? ? ? ? ? ? ? ? ? ? ? }

    ? ? ? ? ? ? ? ? ? ? }

    ? ? ? ? ? ? ? ? }

    ? ? ? ? ? ? ? ? if (!empty($term_ids)) {

    ? ? ? ? ? ? ? ? ? ? wp_set_post_terms($product_id, $term_ids, 'brands');

    ? ? ? ? ? ? ? ? ? ? return new WP_REST_Response('Brands updated successfully', 200);

    ? ? ? ? ? ? ? ? }

    ? ? ? ? ? ? }

    ? ? ? ? ? ? return new WP_Error('no_terms', 'No valid terms provided', array('status' => 400));

    ? ? ? ? },

    ? ? ? ? 'permission_callback' => function() {

    ? ? ? ? ? ? return current_user_can('edit_posts');

    ? ? ? ? },

    ? ? ));

    });

    // Remote taxonomy update using wp_remote_post

    $response = wp_remote_post( $api_url, array(

    ? ? 'method' => 'PUT',

    ? ? 'headers' => array(

    ? ? ? ? 'Authorization' => 'Basic ' . base64_encode( $api_key . ':' . $api_secret ),

    ? ? ? ? 'Content-Type' => 'application/json',

    ? ? ),

    ? ? 'body' => json_encode( array(

    ? ? ? ? 'brands' => array( 'term1', 'term2' ) // Replace with your terms

    ? ? ) ),

    ) );



    I can see the taxonimies but i cannot update them, can you help?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hey there, @asafmoraz! Thanks for contacting us.

    Please note that this forum is exclusive to the WooCommerce core plugin and its features.

    We are not able to assist with anything related to third-party plugins or custom code.

    I suggest reaching out to Jetengine support so they can assist you further ??

    Please let us know if there’s anything else we can do to help or if you have any questions.

    Have a wonderful day!

    Thread Starter asafmoraz

    (@asafmoraz)

    Hi, and thank you for the quick response.

    I actually tried reaching out to Crocoblock first regarding this issue, but they claimed that anything related to the REST API is connected to WooCommerce and that I should contact you. That’s why I reached out to you. If it’s indeed their responsibility to support this matter, I’ll definitely get back to them.

    Thank you.

    Plugin Support Zubair Zahid (woo-hc)

    (@doublezed2)

    Hello asafmoraz,

    Thank you for your reply.

    I understand you’ve created custom taxonomies using the Jet Engine plugin and have written some custom code to update them.

    As mentioned earlier, our forum guidelines prevent us from assisting with custom code or third-party plugins.

    If the Jet Engine plugin support isn’t providing the help you need, it might be a good idea to consult a web developer for further assistance. You can find one at WooExperts or Codeable.

    I hope this points you in the right direction!

    Best regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.