Add a Contact Form to “Out of Stock” product Variations in WooCommerce
I have successfully added a contact form to a product if it is Out of Stock using
add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
function add_contact_form() {
global $product;
if(!$product->is_in_stock( )) {
echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
}
}
But I also want to add the same form if a products variation is Out of Stock too.
Just unsure as to which hook I can use to tie it in to the Out of Stock message for that variation...
I feel like it may be something to do with line 20 of woocommerce/single-product/add-to-cart/variation.php
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
But am getting a bit lost TBH.
I can change the text that is diaplyed for both simple products and those with variations by using
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('<h4 style="color:#F00;">Coming Soon</h4>', 'woocommerce');
}
return $availability;
}
But if I try and add the Form Shortcode or other HTML, like an iFrame, it is stripped out and does not render ?!
php wordpress woocommerce product variations
add a comment |
I have successfully added a contact form to a product if it is Out of Stock using
add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
function add_contact_form() {
global $product;
if(!$product->is_in_stock( )) {
echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
}
}
But I also want to add the same form if a products variation is Out of Stock too.
Just unsure as to which hook I can use to tie it in to the Out of Stock message for that variation...
I feel like it may be something to do with line 20 of woocommerce/single-product/add-to-cart/variation.php
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
But am getting a bit lost TBH.
I can change the text that is diaplyed for both simple products and those with variations by using
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('<h4 style="color:#F00;">Coming Soon</h4>', 'woocommerce');
}
return $availability;
}
But if I try and add the Form Shortcode or other HTML, like an iFrame, it is stripped out and does not render ?!
php wordpress woocommerce product variations
add a comment |
I have successfully added a contact form to a product if it is Out of Stock using
add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
function add_contact_form() {
global $product;
if(!$product->is_in_stock( )) {
echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
}
}
But I also want to add the same form if a products variation is Out of Stock too.
Just unsure as to which hook I can use to tie it in to the Out of Stock message for that variation...
I feel like it may be something to do with line 20 of woocommerce/single-product/add-to-cart/variation.php
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
But am getting a bit lost TBH.
I can change the text that is diaplyed for both simple products and those with variations by using
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('<h4 style="color:#F00;">Coming Soon</h4>', 'woocommerce');
}
return $availability;
}
But if I try and add the Form Shortcode or other HTML, like an iFrame, it is stripped out and does not render ?!
php wordpress woocommerce product variations
I have successfully added a contact form to a product if it is Out of Stock using
add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
function add_contact_form() {
global $product;
if(!$product->is_in_stock( )) {
echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
}
}
But I also want to add the same form if a products variation is Out of Stock too.
Just unsure as to which hook I can use to tie it in to the Out of Stock message for that variation...
I feel like it may be something to do with line 20 of woocommerce/single-product/add-to-cart/variation.php
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
But am getting a bit lost TBH.
I can change the text that is diaplyed for both simple products and those with variations by using
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('<h4 style="color:#F00;">Coming Soon</h4>', 'woocommerce');
}
return $availability;
}
But if I try and add the Form Shortcode or other HTML, like an iFrame, it is stripped out and does not render ?!
php wordpress woocommerce product variations
php wordpress woocommerce product variations
edited Nov 22 at 21:09
LoicTheAztec
84k126095
84k126095
asked Nov 22 at 20:22
Andy Ward
204
204
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The following code will add a contact form to a selected out of stock product variation:
add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
if( ! $data['is_in_stock'] )
$data['availability_html'] .= do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
return $data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
So you will need to tweak your existing code as follow:
add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
function add_contact_form() {
global $product;
if( ! $product->is_in_stock( ) && ! $product->is_type('variable') )
echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
}
Code goes in function.php file of your active child theme (or active theme). Tested and works
Amazing - cheers @LoicTheAztec that worked perfectly
– Andy Ward
Nov 22 at 22:48
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437611%2fadd-a-contact-form-to-out-of-stock-product-variations-in-woocommerce%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The following code will add a contact form to a selected out of stock product variation:
add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
if( ! $data['is_in_stock'] )
$data['availability_html'] .= do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
return $data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
So you will need to tweak your existing code as follow:
add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
function add_contact_form() {
global $product;
if( ! $product->is_in_stock( ) && ! $product->is_type('variable') )
echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
}
Code goes in function.php file of your active child theme (or active theme). Tested and works
Amazing - cheers @LoicTheAztec that worked perfectly
– Andy Ward
Nov 22 at 22:48
add a comment |
The following code will add a contact form to a selected out of stock product variation:
add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
if( ! $data['is_in_stock'] )
$data['availability_html'] .= do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
return $data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
So you will need to tweak your existing code as follow:
add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
function add_contact_form() {
global $product;
if( ! $product->is_in_stock( ) && ! $product->is_type('variable') )
echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
}
Code goes in function.php file of your active child theme (or active theme). Tested and works
Amazing - cheers @LoicTheAztec that worked perfectly
– Andy Ward
Nov 22 at 22:48
add a comment |
The following code will add a contact form to a selected out of stock product variation:
add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
if( ! $data['is_in_stock'] )
$data['availability_html'] .= do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
return $data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
So you will need to tweak your existing code as follow:
add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
function add_contact_form() {
global $product;
if( ! $product->is_in_stock( ) && ! $product->is_type('variable') )
echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
}
Code goes in function.php file of your active child theme (or active theme). Tested and works
The following code will add a contact form to a selected out of stock product variation:
add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
if( ! $data['is_in_stock'] )
$data['availability_html'] .= do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
return $data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
So you will need to tweak your existing code as follow:
add_action('woocommerce_single_product_summary', 'add_contact_form', 30,2);
function add_contact_form() {
global $product;
if( ! $product->is_in_stock( ) && ! $product->is_type('variable') )
echo do_shortcode('[contact-form-7 id="6513" title="Out of Stock Form"]');
}
Code goes in function.php file of your active child theme (or active theme). Tested and works
edited Nov 22 at 21:14
answered Nov 22 at 21:09
LoicTheAztec
84k126095
84k126095
Amazing - cheers @LoicTheAztec that worked perfectly
– Andy Ward
Nov 22 at 22:48
add a comment |
Amazing - cheers @LoicTheAztec that worked perfectly
– Andy Ward
Nov 22 at 22:48
Amazing - cheers @LoicTheAztec that worked perfectly
– Andy Ward
Nov 22 at 22:48
Amazing - cheers @LoicTheAztec that worked perfectly
– Andy Ward
Nov 22 at 22:48
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437611%2fadd-a-contact-form-to-out-of-stock-product-variations-in-woocommerce%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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