• How to avoid POSTing of a product via woocommerce REST API with the name equal or nearly equal to existing product name?
    Idea that pops to mind is to get all the product names on each upload and check they contain name of the product to be uploaded.
    Is it possible to do it via by overriding some filter or action?
    Thanks in advance.

Viewing 1 replies (of 1 total)
  • I have my product creation tied to an event, in this case user registration. If the names are identical, as I’m sure you’ve seen, it doubles up. It might keep the pretty name, but the address does the normal WordPress behavior or concatenating the URL, like “monkey-claw” and “monkey-claw-2.”

    In the REST API documentation samples, they always print out the actual command because it returns a response. When I instead load that response into a variable, I can parse it. This comes in super-handy when I need the product ID because, well, I have no idea what it is because it just got made two seconds ago automatically and they seem like they skip a bit sometimes.

    In my case, I needed to change the author. I don’t think that’s supported by the API but the flipside of that is I don’t think it can be hurt when everything evolves; I don’t think it’ll break in the future. So I just used normal WordPress functions to update the post_author meta (I don’t want people editing other people’s products =)

    I say all this to say…while I don’t know how to interrupt or filter the process itself, perhaps you can do something similar to wait till the product is made normally, unimpeded, nab its ID that gets returned, and then quickly update the name or URL to whatever you need it to be?

    Like this:

    $created_short_sleeve_product_response = $woocommerce->post('products', $short_sleeve_data);
    		$created_short_sleeve_product_response_ID = $created_short_sleeve_product_response['id'];

    Of course those are crazy long variable names, but you get the ID. Now any API or standard WordPress method that needs me to specify an ID, like update_post_meta, can get handed it on a platter, even though it should still be a mystery since it just happened and wasn’t manually assigned.

    Does that help?

Viewing 1 replies (of 1 total)
  • The topic ‘Don’t upload products with the same name via woocommerce RESTAPI’ is closed to new replies.