• Resolved Peshmerge Morad

    (@peshmerge)


    Hi,

    I have looked everywhere on the internet and in this forum but I couldn’t find a proper solution for my problem.

    I have this action

        add_action('save_post_product', 'add_brand_to_product_when_creating_a_product', 10, 3);
        function add_brand_to_product_when_creating_a_product( $post_id, $post, $update ) {
             if ( get_post_type( $post_id ) == 'product' ) { // we've been editing a product
                    $product = wc_get_product( $post_id );
                    add_brand_to_product_if_missing($post_id);
                }
        }

    This code works when I add the product using the WordPress dashboard (admin area), but it doesn’t work when I create/add a product using the endpoint POST /wp-json/wc/v3/products

    Which action/hook do I have to use to trigger a specific code after adding/creating a product using the REST API?

    The custom code add_brand_to_product_if_missing I am trying to execute is working with terms. It actually calls the function
    wp_set_object_terms($product_id, $productBrand,'product_brand');

    What I am trying to do is the following:
    I have desktop application which call WooCommerce API to create products. The POST request has the product data including a product attribute “pa_brand”. For the brands in WooCommerce, I use the plugin “WooCommerce Brands“. Instead of assigning the brands manually to each product, I want to trigger a code which automatically takes the value of the “pa_brand” and save it as a brand (product_brand) and assign it to the product. As I have already mentioned, the code above works fine, when a product is added from withing the wordpress dashboard, but it doesn’t work when the product is added via REST API!

    I hope I could explain my situation well 🙂

    Thank you very much for your time and attention 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi @peshmerge 👋

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers 🙂

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    We’ve not seen any activity on this thread for a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Trigger custom code after inserting a new product using the REST API’ is closed to new replies.