Activate a custom button once product is added to cart on Woocommerce single product page
In Woocommerce single product pages, I've added a custom button using the woocommerce_after_add_to_cart
hook. The button will, when clicked/ pressed, re-directs the customer to the checkout.
But to avoid the customer clicking the button before having added the product to the cart, I added this: if ( WC()->cart->get_cart_contents_count() != 0 ) {
to the function.
My question is this: how can I have this button always available but inactive until a product is added to cart? How do I make it "grayed out" (not clickable) until there is a product in the cart?
Here is my complete code:
add_action('woocommerce_after_add_to_cart_button, 'instant_checkout');
function instant_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
if ( WC()->cart->get_cart_contents_count() != 0 ) {
echo '<a href="'.$checkout_url.'" class="single_add_to_cart_button button alt">Instant Checkout</a>'; } }
Thanks for any help on this.
php wordpress woocommerce product cart
add a comment |
In Woocommerce single product pages, I've added a custom button using the woocommerce_after_add_to_cart
hook. The button will, when clicked/ pressed, re-directs the customer to the checkout.
But to avoid the customer clicking the button before having added the product to the cart, I added this: if ( WC()->cart->get_cart_contents_count() != 0 ) {
to the function.
My question is this: how can I have this button always available but inactive until a product is added to cart? How do I make it "grayed out" (not clickable) until there is a product in the cart?
Here is my complete code:
add_action('woocommerce_after_add_to_cart_button, 'instant_checkout');
function instant_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
if ( WC()->cart->get_cart_contents_count() != 0 ) {
echo '<a href="'.$checkout_url.'" class="single_add_to_cart_button button alt">Instant Checkout</a>'; } }
Thanks for any help on this.
php wordpress woocommerce product cart
add a comment |
In Woocommerce single product pages, I've added a custom button using the woocommerce_after_add_to_cart
hook. The button will, when clicked/ pressed, re-directs the customer to the checkout.
But to avoid the customer clicking the button before having added the product to the cart, I added this: if ( WC()->cart->get_cart_contents_count() != 0 ) {
to the function.
My question is this: how can I have this button always available but inactive until a product is added to cart? How do I make it "grayed out" (not clickable) until there is a product in the cart?
Here is my complete code:
add_action('woocommerce_after_add_to_cart_button, 'instant_checkout');
function instant_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
if ( WC()->cart->get_cart_contents_count() != 0 ) {
echo '<a href="'.$checkout_url.'" class="single_add_to_cart_button button alt">Instant Checkout</a>'; } }
Thanks for any help on this.
php wordpress woocommerce product cart
In Woocommerce single product pages, I've added a custom button using the woocommerce_after_add_to_cart
hook. The button will, when clicked/ pressed, re-directs the customer to the checkout.
But to avoid the customer clicking the button before having added the product to the cart, I added this: if ( WC()->cart->get_cart_contents_count() != 0 ) {
to the function.
My question is this: how can I have this button always available but inactive until a product is added to cart? How do I make it "grayed out" (not clickable) until there is a product in the cart?
Here is my complete code:
add_action('woocommerce_after_add_to_cart_button, 'instant_checkout');
function instant_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
if ( WC()->cart->get_cart_contents_count() != 0 ) {
echo '<a href="'.$checkout_url.'" class="single_add_to_cart_button button alt">Instant Checkout</a>'; } }
Thanks for any help on this.
php wordpress woocommerce product cart
php wordpress woocommerce product cart
edited Nov 13 '18 at 16:15
LoicTheAztec
85.5k136095
85.5k136095
asked Nov 13 '18 at 14:26
user10551357
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The following will display a custom disabled button after add to cart button… When the product will be added to cart the product will be enabled and linked to checkout page:
add_action('woocommerce_after_add_to_cart_button', 'get_instant_checkout_buttom_html');
function get_instant_checkout_buttom_html() {
global $product;
$href = ''; // Initializing
$class = ' disabled'; // Initializing
// Continue only if cart is not empty
if ( ! WC()->cart->is_empty() ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $item ) {
// When product is in cart
if( $item['product_id'] == $product->get_id() ){
$href = ' href="'.wc_get_checkout_url().'"'; // Add the link to the button
$class = ''; // Remove "disabled" class
break; // Stop the loop
}
}
}
// Button output
echo '<a'.$href.' class="single_add_to_cart_button button alt'.$class.'">'.__("Instant Checkout").'</a>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.
– user10551357
Nov 13 '18 at 15:04
@WooDevil Updated… try it
– LoicTheAztec
Nov 13 '18 at 15:18
thank you for your help once again, works like a charm :)
– user10551357
Nov 13 '18 at 15:19
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%2f53283197%2factivate-a-custom-button-once-product-is-added-to-cart-on-woocommerce-single-pro%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 will display a custom disabled button after add to cart button… When the product will be added to cart the product will be enabled and linked to checkout page:
add_action('woocommerce_after_add_to_cart_button', 'get_instant_checkout_buttom_html');
function get_instant_checkout_buttom_html() {
global $product;
$href = ''; // Initializing
$class = ' disabled'; // Initializing
// Continue only if cart is not empty
if ( ! WC()->cart->is_empty() ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $item ) {
// When product is in cart
if( $item['product_id'] == $product->get_id() ){
$href = ' href="'.wc_get_checkout_url().'"'; // Add the link to the button
$class = ''; // Remove "disabled" class
break; // Stop the loop
}
}
}
// Button output
echo '<a'.$href.' class="single_add_to_cart_button button alt'.$class.'">'.__("Instant Checkout").'</a>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.
– user10551357
Nov 13 '18 at 15:04
@WooDevil Updated… try it
– LoicTheAztec
Nov 13 '18 at 15:18
thank you for your help once again, works like a charm :)
– user10551357
Nov 13 '18 at 15:19
add a comment |
The following will display a custom disabled button after add to cart button… When the product will be added to cart the product will be enabled and linked to checkout page:
add_action('woocommerce_after_add_to_cart_button', 'get_instant_checkout_buttom_html');
function get_instant_checkout_buttom_html() {
global $product;
$href = ''; // Initializing
$class = ' disabled'; // Initializing
// Continue only if cart is not empty
if ( ! WC()->cart->is_empty() ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $item ) {
// When product is in cart
if( $item['product_id'] == $product->get_id() ){
$href = ' href="'.wc_get_checkout_url().'"'; // Add the link to the button
$class = ''; // Remove "disabled" class
break; // Stop the loop
}
}
}
// Button output
echo '<a'.$href.' class="single_add_to_cart_button button alt'.$class.'">'.__("Instant Checkout").'</a>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.
– user10551357
Nov 13 '18 at 15:04
@WooDevil Updated… try it
– LoicTheAztec
Nov 13 '18 at 15:18
thank you for your help once again, works like a charm :)
– user10551357
Nov 13 '18 at 15:19
add a comment |
The following will display a custom disabled button after add to cart button… When the product will be added to cart the product will be enabled and linked to checkout page:
add_action('woocommerce_after_add_to_cart_button', 'get_instant_checkout_buttom_html');
function get_instant_checkout_buttom_html() {
global $product;
$href = ''; // Initializing
$class = ' disabled'; // Initializing
// Continue only if cart is not empty
if ( ! WC()->cart->is_empty() ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $item ) {
// When product is in cart
if( $item['product_id'] == $product->get_id() ){
$href = ' href="'.wc_get_checkout_url().'"'; // Add the link to the button
$class = ''; // Remove "disabled" class
break; // Stop the loop
}
}
}
// Button output
echo '<a'.$href.' class="single_add_to_cart_button button alt'.$class.'">'.__("Instant Checkout").'</a>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
The following will display a custom disabled button after add to cart button… When the product will be added to cart the product will be enabled and linked to checkout page:
add_action('woocommerce_after_add_to_cart_button', 'get_instant_checkout_buttom_html');
function get_instant_checkout_buttom_html() {
global $product;
$href = ''; // Initializing
$class = ' disabled'; // Initializing
// Continue only if cart is not empty
if ( ! WC()->cart->is_empty() ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $item ) {
// When product is in cart
if( $item['product_id'] == $product->get_id() ){
$href = ' href="'.wc_get_checkout_url().'"'; // Add the link to the button
$class = ''; // Remove "disabled" class
break; // Stop the loop
}
}
}
// Button output
echo '<a'.$href.' class="single_add_to_cart_button button alt'.$class.'">'.__("Instant Checkout").'</a>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
edited Nov 13 '18 at 15:19
answered Nov 13 '18 at 14:41
LoicTheAztecLoicTheAztec
85.5k136095
85.5k136095
thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.
– user10551357
Nov 13 '18 at 15:04
@WooDevil Updated… try it
– LoicTheAztec
Nov 13 '18 at 15:18
thank you for your help once again, works like a charm :)
– user10551357
Nov 13 '18 at 15:19
add a comment |
thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.
– user10551357
Nov 13 '18 at 15:04
@WooDevil Updated… try it
– LoicTheAztec
Nov 13 '18 at 15:18
thank you for your help once again, works like a charm :)
– user10551357
Nov 13 '18 at 15:19
thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.
– user10551357
Nov 13 '18 at 15:04
thanks for the answer, but it does exactly what my code does? It does not display the button until there is a product in the cart. I need the button visible, just not clickable, until a product is added to cart.
– user10551357
Nov 13 '18 at 15:04
@WooDevil Updated… try it
– LoicTheAztec
Nov 13 '18 at 15:18
@WooDevil Updated… try it
– LoicTheAztec
Nov 13 '18 at 15:18
thank you for your help once again, works like a charm :)
– user10551357
Nov 13 '18 at 15:19
thank you for your help once again, works like a charm :)
– user10551357
Nov 13 '18 at 15:19
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.
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%2f53283197%2factivate-a-custom-button-once-product-is-added-to-cart-on-woocommerce-single-pro%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