How can I fire a custom function just once in Woocommerce












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;










share|improve this question

























  • 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
















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;










share|improve this question

























  • 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














1












1








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;










share|improve this question
















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;







php wordpress woocommerce product custom-taxonomy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 16:20







Marvin

















asked Nov 13 '18 at 15:19









MarvinMarvin

612




612













  • 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



















  • 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

















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







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







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





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





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





@gui that looks too complicated for me.

– Marvin
Nov 13 '18 at 16:55












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284128%2fhow-can-i-fire-a-custom-function-just-once-in-woocommerce%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284128%2fhow-can-i-fire-a-custom-function-just-once-in-woocommerce%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine