Disabling the purchase for one product in woocommerce
I am trying to turn off the possibility of buying a selected product, but all the materials I have found so far do not work for me. I would like to exclude a specific product, preferably by removing the button, because I want this product to work like any other, but without the possibility to be buying.
This code would be perfect for me:
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable');
function filter_is_purchasable( $is_purchasable, $product ) {
return ( $product->id == 534 ? false : $is_purchasable );
}
Unfortunately it does not work. Any ideas?
php wordpress woocommerce product
add a comment |
I am trying to turn off the possibility of buying a selected product, but all the materials I have found so far do not work for me. I would like to exclude a specific product, preferably by removing the button, because I want this product to work like any other, but without the possibility to be buying.
This code would be perfect for me:
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable');
function filter_is_purchasable( $is_purchasable, $product ) {
return ( $product->id == 534 ? false : $is_purchasable );
}
Unfortunately it does not work. Any ideas?
php wordpress woocommerce product
add a comment |
I am trying to turn off the possibility of buying a selected product, but all the materials I have found so far do not work for me. I would like to exclude a specific product, preferably by removing the button, because I want this product to work like any other, but without the possibility to be buying.
This code would be perfect for me:
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable');
function filter_is_purchasable( $is_purchasable, $product ) {
return ( $product->id == 534 ? false : $is_purchasable );
}
Unfortunately it does not work. Any ideas?
php wordpress woocommerce product
I am trying to turn off the possibility of buying a selected product, but all the materials I have found so far do not work for me. I would like to exclude a specific product, preferably by removing the button, because I want this product to work like any other, but without the possibility to be buying.
This code would be perfect for me:
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable');
function filter_is_purchasable( $is_purchasable, $product ) {
return ( $product->id == 534 ? false : $is_purchasable );
}
Unfortunately it does not work. Any ideas?
php wordpress woocommerce product
php wordpress woocommerce product
edited Nov 24 '18 at 3:22
LoicTheAztec
90.6k1365104
90.6k1365104
asked Nov 21 '18 at 0:58
dezajnerdezajner
82
82
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This code is just a bit outdated since Woocommerce 3, as $product->id
need to be replaced with $product->get_id()
… Try the following instead (where 534 is the related product ID where add to cart button will not appear):
add_filter('woocommerce_is_purchasable', 'purchasable_product_customizations', 10, 2 );
function purchasable_product_customizations( $is_purchasable, $product ) {
if( in_array( $product->get_id() == 534 )
$is_purchasable = false;
return $is_purchasable;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If your product ID is a variable product
For a variable product ID where you want to disable allvariations, you will replace this line:
if( in_array( $product->get_id() == 534 )
by this one:
if( in_array( $product->get_parent_id() == 534 )
If it still doesn't work is that something else is interacting and making trouble, like some customizations made by you, your theme or a plugin… Also it can be because you are not adding this code in the right place.
All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.
– dezajner
Nov 21 '18 at 7:59
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%2f53403845%2fdisabling-the-purchase-for-one-product-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
This code is just a bit outdated since Woocommerce 3, as $product->id
need to be replaced with $product->get_id()
… Try the following instead (where 534 is the related product ID where add to cart button will not appear):
add_filter('woocommerce_is_purchasable', 'purchasable_product_customizations', 10, 2 );
function purchasable_product_customizations( $is_purchasable, $product ) {
if( in_array( $product->get_id() == 534 )
$is_purchasable = false;
return $is_purchasable;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If your product ID is a variable product
For a variable product ID where you want to disable allvariations, you will replace this line:
if( in_array( $product->get_id() == 534 )
by this one:
if( in_array( $product->get_parent_id() == 534 )
If it still doesn't work is that something else is interacting and making trouble, like some customizations made by you, your theme or a plugin… Also it can be because you are not adding this code in the right place.
All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.
– dezajner
Nov 21 '18 at 7:59
add a comment |
This code is just a bit outdated since Woocommerce 3, as $product->id
need to be replaced with $product->get_id()
… Try the following instead (where 534 is the related product ID where add to cart button will not appear):
add_filter('woocommerce_is_purchasable', 'purchasable_product_customizations', 10, 2 );
function purchasable_product_customizations( $is_purchasable, $product ) {
if( in_array( $product->get_id() == 534 )
$is_purchasable = false;
return $is_purchasable;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If your product ID is a variable product
For a variable product ID where you want to disable allvariations, you will replace this line:
if( in_array( $product->get_id() == 534 )
by this one:
if( in_array( $product->get_parent_id() == 534 )
If it still doesn't work is that something else is interacting and making trouble, like some customizations made by you, your theme or a plugin… Also it can be because you are not adding this code in the right place.
All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.
– dezajner
Nov 21 '18 at 7:59
add a comment |
This code is just a bit outdated since Woocommerce 3, as $product->id
need to be replaced with $product->get_id()
… Try the following instead (where 534 is the related product ID where add to cart button will not appear):
add_filter('woocommerce_is_purchasable', 'purchasable_product_customizations', 10, 2 );
function purchasable_product_customizations( $is_purchasable, $product ) {
if( in_array( $product->get_id() == 534 )
$is_purchasable = false;
return $is_purchasable;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If your product ID is a variable product
For a variable product ID where you want to disable allvariations, you will replace this line:
if( in_array( $product->get_id() == 534 )
by this one:
if( in_array( $product->get_parent_id() == 534 )
If it still doesn't work is that something else is interacting and making trouble, like some customizations made by you, your theme or a plugin… Also it can be because you are not adding this code in the right place.
This code is just a bit outdated since Woocommerce 3, as $product->id
need to be replaced with $product->get_id()
… Try the following instead (where 534 is the related product ID where add to cart button will not appear):
add_filter('woocommerce_is_purchasable', 'purchasable_product_customizations', 10, 2 );
function purchasable_product_customizations( $is_purchasable, $product ) {
if( in_array( $product->get_id() == 534 )
$is_purchasable = false;
return $is_purchasable;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If your product ID is a variable product
For a variable product ID where you want to disable allvariations, you will replace this line:
if( in_array( $product->get_id() == 534 )
by this one:
if( in_array( $product->get_parent_id() == 534 )
If it still doesn't work is that something else is interacting and making trouble, like some customizations made by you, your theme or a plugin… Also it can be because you are not adding this code in the right place.
edited Nov 24 '18 at 3:26
answered Nov 21 '18 at 1:33
LoicTheAztecLoicTheAztec
90.6k1365104
90.6k1365104
All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.
– dezajner
Nov 21 '18 at 7:59
add a comment |
All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.
– dezajner
Nov 21 '18 at 7:59
All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.
– dezajner
Nov 21 '18 at 7:59
All modifications add a child theme to the functions.php, unfortunately in this case it receives the HTTP ERROR 500, and the page stops working.
– dezajner
Nov 21 '18 at 7:59
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%2f53403845%2fdisabling-the-purchase-for-one-product-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