Display the stock availability for all product types in Woocommerce archive pages
up vote
0
down vote
favorite
I am using this code in showing the stocks of products:
add_action( 'woocommerce_after_shop_loop_item', 'display_variable_product_stock_quantity', 10 );
function display_variable_product_stock_quantity(){
wc_get_variable_product_stock_quantity( 'echo_html' );
}
function show_stock() {
global $product;
if ( $product->stock ) { // if manage stock is enabled
if ( ! $product->managing_stock() && ! $product->is_in_stock() )
echo '';
}
if ( number_format($product->stock,0,'','') > 0 ) { // if stock is low
echo '<div class="remainingpc" style="text-align:center;"><font color="red"> ' . number_format($product->stock,0,'','') . ' Pcs Left</font></div>';
}
else {
echo '<div class="remaining" style="text-align:center;"><font color="red">Out of Stock</font></div>';
}
}
add_action('woocommerce_after_shop_loop_item','show_stock', 10);
And if the product is a variable I use this answer code to display the stock availability:
Get the total stock of all variations from a variable product In Woocommerce
How can I merge this codes in a single conditional function?
For example. if the products is a simple product, the other code for variable product will not display.
php wordpress woocommerce product stock
add a comment |
up vote
0
down vote
favorite
I am using this code in showing the stocks of products:
add_action( 'woocommerce_after_shop_loop_item', 'display_variable_product_stock_quantity', 10 );
function display_variable_product_stock_quantity(){
wc_get_variable_product_stock_quantity( 'echo_html' );
}
function show_stock() {
global $product;
if ( $product->stock ) { // if manage stock is enabled
if ( ! $product->managing_stock() && ! $product->is_in_stock() )
echo '';
}
if ( number_format($product->stock,0,'','') > 0 ) { // if stock is low
echo '<div class="remainingpc" style="text-align:center;"><font color="red"> ' . number_format($product->stock,0,'','') . ' Pcs Left</font></div>';
}
else {
echo '<div class="remaining" style="text-align:center;"><font color="red">Out of Stock</font></div>';
}
}
add_action('woocommerce_after_shop_loop_item','show_stock', 10);
And if the product is a variable I use this answer code to display the stock availability:
Get the total stock of all variations from a variable product In Woocommerce
How can I merge this codes in a single conditional function?
For example. if the products is a simple product, the other code for variable product will not display.
php wordpress woocommerce product stock
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am using this code in showing the stocks of products:
add_action( 'woocommerce_after_shop_loop_item', 'display_variable_product_stock_quantity', 10 );
function display_variable_product_stock_quantity(){
wc_get_variable_product_stock_quantity( 'echo_html' );
}
function show_stock() {
global $product;
if ( $product->stock ) { // if manage stock is enabled
if ( ! $product->managing_stock() && ! $product->is_in_stock() )
echo '';
}
if ( number_format($product->stock,0,'','') > 0 ) { // if stock is low
echo '<div class="remainingpc" style="text-align:center;"><font color="red"> ' . number_format($product->stock,0,'','') . ' Pcs Left</font></div>';
}
else {
echo '<div class="remaining" style="text-align:center;"><font color="red">Out of Stock</font></div>';
}
}
add_action('woocommerce_after_shop_loop_item','show_stock', 10);
And if the product is a variable I use this answer code to display the stock availability:
Get the total stock of all variations from a variable product In Woocommerce
How can I merge this codes in a single conditional function?
For example. if the products is a simple product, the other code for variable product will not display.
php wordpress woocommerce product stock
I am using this code in showing the stocks of products:
add_action( 'woocommerce_after_shop_loop_item', 'display_variable_product_stock_quantity', 10 );
function display_variable_product_stock_quantity(){
wc_get_variable_product_stock_quantity( 'echo_html' );
}
function show_stock() {
global $product;
if ( $product->stock ) { // if manage stock is enabled
if ( ! $product->managing_stock() && ! $product->is_in_stock() )
echo '';
}
if ( number_format($product->stock,0,'','') > 0 ) { // if stock is low
echo '<div class="remainingpc" style="text-align:center;"><font color="red"> ' . number_format($product->stock,0,'','') . ' Pcs Left</font></div>';
}
else {
echo '<div class="remaining" style="text-align:center;"><font color="red">Out of Stock</font></div>';
}
}
add_action('woocommerce_after_shop_loop_item','show_stock', 10);
And if the product is a variable I use this answer code to display the stock availability:
Get the total stock of all variations from a variable product In Woocommerce
How can I merge this codes in a single conditional function?
For example. if the products is a simple product, the other code for variable product will not display.
php wordpress woocommerce product stock
php wordpress woocommerce product stock
edited Nov 10 at 11:26
LoicTheAztec
83.2k125993
83.2k125993
asked Nov 10 at 4:17
Erwin Manalang
587
587
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
The following will handle the display of the stock availability for all product types in woocommerce archive product pages as shop.
To handle the stock availability display for other product types than variable, you can use the dedicated function wc_get_stock_html() instead, which will simplify the code.
add_action( 'woocommerce_after_shop_loop_item', 'wc_loop_get_product_stock_availability_text', 10 );
function wc_loop_get_product_stock_availability_text() {
global $wpdb, $product;
// For variable products
if( $product->is_type('variable') ) {
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish' AND p.post_parent = '".get_the_id()."'
AND pm.meta_key = '_stock' AND pm.meta_value IS NOT NULL
");
if ( $stock_quantity > 0 ) {
echo '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
echo '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
return;
}
}
// Other products types
else {
echo wc_get_stock_html( $product );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Tested and it works. You're a life saver sir :)
– Erwin Manalang
Nov 11 at 2:44
By the way sir, I am using ajax search plugin for woocommerce. i've used the {_stock} field in order to add the stocks in the results but {_stocks} displays not the total of variants. how can I know the field of the sum of stocks? thanks sir
– Erwin Manalang
Nov 11 at 5:29
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',
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%2f53235951%2fdisplay-the-stock-availability-for-all-product-types-in-woocommerce-archive-page%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
up vote
2
down vote
accepted
The following will handle the display of the stock availability for all product types in woocommerce archive product pages as shop.
To handle the stock availability display for other product types than variable, you can use the dedicated function wc_get_stock_html() instead, which will simplify the code.
add_action( 'woocommerce_after_shop_loop_item', 'wc_loop_get_product_stock_availability_text', 10 );
function wc_loop_get_product_stock_availability_text() {
global $wpdb, $product;
// For variable products
if( $product->is_type('variable') ) {
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish' AND p.post_parent = '".get_the_id()."'
AND pm.meta_key = '_stock' AND pm.meta_value IS NOT NULL
");
if ( $stock_quantity > 0 ) {
echo '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
echo '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
return;
}
}
// Other products types
else {
echo wc_get_stock_html( $product );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Tested and it works. You're a life saver sir :)
– Erwin Manalang
Nov 11 at 2:44
By the way sir, I am using ajax search plugin for woocommerce. i've used the {_stock} field in order to add the stocks in the results but {_stocks} displays not the total of variants. how can I know the field of the sum of stocks? thanks sir
– Erwin Manalang
Nov 11 at 5:29
add a comment |
up vote
2
down vote
accepted
The following will handle the display of the stock availability for all product types in woocommerce archive product pages as shop.
To handle the stock availability display for other product types than variable, you can use the dedicated function wc_get_stock_html() instead, which will simplify the code.
add_action( 'woocommerce_after_shop_loop_item', 'wc_loop_get_product_stock_availability_text', 10 );
function wc_loop_get_product_stock_availability_text() {
global $wpdb, $product;
// For variable products
if( $product->is_type('variable') ) {
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish' AND p.post_parent = '".get_the_id()."'
AND pm.meta_key = '_stock' AND pm.meta_value IS NOT NULL
");
if ( $stock_quantity > 0 ) {
echo '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
echo '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
return;
}
}
// Other products types
else {
echo wc_get_stock_html( $product );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Tested and it works. You're a life saver sir :)
– Erwin Manalang
Nov 11 at 2:44
By the way sir, I am using ajax search plugin for woocommerce. i've used the {_stock} field in order to add the stocks in the results but {_stocks} displays not the total of variants. how can I know the field of the sum of stocks? thanks sir
– Erwin Manalang
Nov 11 at 5:29
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The following will handle the display of the stock availability for all product types in woocommerce archive product pages as shop.
To handle the stock availability display for other product types than variable, you can use the dedicated function wc_get_stock_html() instead, which will simplify the code.
add_action( 'woocommerce_after_shop_loop_item', 'wc_loop_get_product_stock_availability_text', 10 );
function wc_loop_get_product_stock_availability_text() {
global $wpdb, $product;
// For variable products
if( $product->is_type('variable') ) {
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish' AND p.post_parent = '".get_the_id()."'
AND pm.meta_key = '_stock' AND pm.meta_value IS NOT NULL
");
if ( $stock_quantity > 0 ) {
echo '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
echo '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
return;
}
}
// Other products types
else {
echo wc_get_stock_html( $product );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
The following will handle the display of the stock availability for all product types in woocommerce archive product pages as shop.
To handle the stock availability display for other product types than variable, you can use the dedicated function wc_get_stock_html() instead, which will simplify the code.
add_action( 'woocommerce_after_shop_loop_item', 'wc_loop_get_product_stock_availability_text', 10 );
function wc_loop_get_product_stock_availability_text() {
global $wpdb, $product;
// For variable products
if( $product->is_type('variable') ) {
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish' AND p.post_parent = '".get_the_id()."'
AND pm.meta_key = '_stock' AND pm.meta_value IS NOT NULL
");
if ( $stock_quantity > 0 ) {
echo '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
echo '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
return;
}
}
// Other products types
else {
echo wc_get_stock_html( $product );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
edited Nov 10 at 11:32
answered Nov 10 at 11:25
LoicTheAztec
83.2k125993
83.2k125993
Tested and it works. You're a life saver sir :)
– Erwin Manalang
Nov 11 at 2:44
By the way sir, I am using ajax search plugin for woocommerce. i've used the {_stock} field in order to add the stocks in the results but {_stocks} displays not the total of variants. how can I know the field of the sum of stocks? thanks sir
– Erwin Manalang
Nov 11 at 5:29
add a comment |
Tested and it works. You're a life saver sir :)
– Erwin Manalang
Nov 11 at 2:44
By the way sir, I am using ajax search plugin for woocommerce. i've used the {_stock} field in order to add the stocks in the results but {_stocks} displays not the total of variants. how can I know the field of the sum of stocks? thanks sir
– Erwin Manalang
Nov 11 at 5:29
Tested and it works. You're a life saver sir :)
– Erwin Manalang
Nov 11 at 2:44
Tested and it works. You're a life saver sir :)
– Erwin Manalang
Nov 11 at 2:44
By the way sir, I am using ajax search plugin for woocommerce. i've used the {_stock} field in order to add the stocks in the results but {_stocks} displays not the total of variants. how can I know the field of the sum of stocks? thanks sir
– Erwin Manalang
Nov 11 at 5:29
By the way sir, I am using ajax search plugin for woocommerce. i've used the {_stock} field in order to add the stocks in the results but {_stocks} displays not the total of variants. how can I know the field of the sum of stocks? thanks sir
– Erwin Manalang
Nov 11 at 5:29
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%2f53235951%2fdisplay-the-stock-availability-for-all-product-types-in-woocommerce-archive-page%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