1

I need to add variations to my Woocommerce products programmatically and I borrowed the code from this answer thread:
Create programmatically a WooCommerce product variation with new attribute values

It works, but gives me two variations when I pass this data array:

$variation_data =  array(
    'attributes' => array(
        'kidssize'  => '2'
    ),
    'sku'           => '',
    'regular_price' => '120',
    'sale_price'    => '',
    'stock_qty'     => '',
);

My guess is that funcntion fires two times.

Since i am a noob in php and backend in general all I know how to call a function is from some template file and visiting the page.

And to prevent other visitors from triggering it I use

if(isset($_GET['**parameter Only I know**'])) 

… wrap and call it going to the page with set parameter;

I understand that this is a really bad way for doing that, but how do I do it otherwise if I need to call function once and never use it again?

And is even firing twice a problem here or is it something wrong with my array?

EDIT:

Here's a detailed process of what i do:

i put the function from the link above in functions.php,

then put the call in footer.php of my theme with above mentioned wrap (the parameter is irrelevant - it could be anything, because it's just used as a trigger)

and go to the page with said parameter to trigger the call,

load the page only once and look for the result in admin panel.

And it has 2 variations always, even if i add more attributes to an array it will return 2 variatons of the last attributes array item;

Marvin
  • 11
  • 1
  • 2
  • If you are not making changes to the code, dont add it to your question, just keep the link to the answer code. – LoicTheAztec Nov 13 '18 at 16:02
  • Oh ok, noted :) – Marvin Nov 13 '18 at 16:04
  • Your explanations are not enough to reproduce your issue: *"**parameter Only I know**"* … Remember that *"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error **and the shortest code necessary to reproduce it in the question itself**."* – LoicTheAztec Nov 13 '18 at 16:07
  • What about using the Rest API to invoke your code? From what i could understand, you just want to execute a piece of code that's never invoked by WP itself but only from you manually. For that i like to use the API, then a tool like Postman that will simplify the HTTP request. If this is what you're looking for check the register_rest_route function. – Gui Nov 13 '18 at 16:25
  • @gui that looks too complicated for me. – Marvin Nov 13 '18 at 16:55
  • @LoicTheAztec i tried to elaborate, is this better? – Marvin Nov 13 '18 at 16:55
  • Actually your question provided code is not really testable, to be able to reproduce your issue and give you some help. I will try your data array later on to see if I have the same problem. – LoicTheAztec Nov 13 '18 at 17:05
  • No problem, check this gist: https://gist.github.com/gui-acab/5fd760d38e1532f43709adf4d41c562e You would invoke that using http://your-domain.com/wp-json/demo/v1/execute?secret=your-secret-token-that-only-you-know – Gui Nov 13 '18 at 18:57
  • @Gui what does it do? – Marvin Nov 13 '18 at 20:20
  • It allows you to execute a piece of code from the url (using the Rest API). In that gist where you've // Paste your code here is where your could would actually be executed. Test that code, paste it in the functions.php and then visit your website using that url. – Gui Nov 14 '18 at 11:55
  • Also check the WP-Cli. You can evaluate a file and execute it once. – Gui Nov 15 '18 at 12:22

0 Answers0