Get the product variations skus from a variable product in WooCommerce
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to get an array of skus from variable products in woocommerce. Each variation has its own sku, and I have also set an overall sku for the product that encompasses all variations (in the inventory tab)
So for simple products this code works:
global $product;
$product_sku = $product->get_sku();
But when I am working with a variable product, it returns the sku listed in the Inventory tab, rather than any skus for the variations. So what would be a good way to get those variation skus in an array?
Here is some psuedo code for how I am thinking about the process
$inventory_sku = 'sku1';
$variation_skus = get_skus_by_inv_sku($inventory_sku);
echo $variation_skus[0];
Thanks in advance!
php wordpress woocommerce variations sku
add a comment |
I am trying to get an array of skus from variable products in woocommerce. Each variation has its own sku, and I have also set an overall sku for the product that encompasses all variations (in the inventory tab)
So for simple products this code works:
global $product;
$product_sku = $product->get_sku();
But when I am working with a variable product, it returns the sku listed in the Inventory tab, rather than any skus for the variations. So what would be a good way to get those variation skus in an array?
Here is some psuedo code for how I am thinking about the process
$inventory_sku = 'sku1';
$variation_skus = get_skus_by_inv_sku($inventory_sku);
echo $variation_skus[0];
Thanks in advance!
php wordpress woocommerce variations sku
add a comment |
I am trying to get an array of skus from variable products in woocommerce. Each variation has its own sku, and I have also set an overall sku for the product that encompasses all variations (in the inventory tab)
So for simple products this code works:
global $product;
$product_sku = $product->get_sku();
But when I am working with a variable product, it returns the sku listed in the Inventory tab, rather than any skus for the variations. So what would be a good way to get those variation skus in an array?
Here is some psuedo code for how I am thinking about the process
$inventory_sku = 'sku1';
$variation_skus = get_skus_by_inv_sku($inventory_sku);
echo $variation_skus[0];
Thanks in advance!
php wordpress woocommerce variations sku
I am trying to get an array of skus from variable products in woocommerce. Each variation has its own sku, and I have also set an overall sku for the product that encompasses all variations (in the inventory tab)
So for simple products this code works:
global $product;
$product_sku = $product->get_sku();
But when I am working with a variable product, it returns the sku listed in the Inventory tab, rather than any skus for the variations. So what would be a good way to get those variation skus in an array?
Here is some psuedo code for how I am thinking about the process
$inventory_sku = 'sku1';
$variation_skus = get_skus_by_inv_sku($inventory_sku);
echo $variation_skus[0];
Thanks in advance!
php wordpress woocommerce variations sku
php wordpress woocommerce variations sku
edited Jan 3 at 23:42
LoicTheAztec
96.5k1371113
96.5k1371113
asked Jan 3 at 21:57
Brian DuncanBrian Duncan
628
628
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The shortest and lightest way is to use WPDB Class to make an SQL query embedded in a function:
function get_variations_skus( $product_id ){
global $wpdb;
return $wpdb->get_col("
SELECT pm.meta_value
FROM {$wpdb->prefix}postmeta as pm
INNER JOIN {$wpdb->prefix}posts as p ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = $product_id
AND pm.meta_key = '_sku'
AND pm.meta_value != ''
");
}
Code goes in function.php file of your active child theme (or active theme) or in a plugin. Tested and works.
USAGE - In a variable product:
global $product;
// Variable product main sku
$sku = $product->get_sku();
// The variations skus for this variable product (in an array)
$variations_skus = get_variations_skus( $product->get_id() );
// Testing output variations skus: Array to string conversion (coma separated skus)
echo sizeof($variations_skus) > 0 ? implode( ', ', $variations_skus ) : 'No skus';
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%2f54030358%2fget-the-product-variations-skus-from-a-variable-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
The shortest and lightest way is to use WPDB Class to make an SQL query embedded in a function:
function get_variations_skus( $product_id ){
global $wpdb;
return $wpdb->get_col("
SELECT pm.meta_value
FROM {$wpdb->prefix}postmeta as pm
INNER JOIN {$wpdb->prefix}posts as p ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = $product_id
AND pm.meta_key = '_sku'
AND pm.meta_value != ''
");
}
Code goes in function.php file of your active child theme (or active theme) or in a plugin. Tested and works.
USAGE - In a variable product:
global $product;
// Variable product main sku
$sku = $product->get_sku();
// The variations skus for this variable product (in an array)
$variations_skus = get_variations_skus( $product->get_id() );
// Testing output variations skus: Array to string conversion (coma separated skus)
echo sizeof($variations_skus) > 0 ? implode( ', ', $variations_skus ) : 'No skus';
add a comment |
The shortest and lightest way is to use WPDB Class to make an SQL query embedded in a function:
function get_variations_skus( $product_id ){
global $wpdb;
return $wpdb->get_col("
SELECT pm.meta_value
FROM {$wpdb->prefix}postmeta as pm
INNER JOIN {$wpdb->prefix}posts as p ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = $product_id
AND pm.meta_key = '_sku'
AND pm.meta_value != ''
");
}
Code goes in function.php file of your active child theme (or active theme) or in a plugin. Tested and works.
USAGE - In a variable product:
global $product;
// Variable product main sku
$sku = $product->get_sku();
// The variations skus for this variable product (in an array)
$variations_skus = get_variations_skus( $product->get_id() );
// Testing output variations skus: Array to string conversion (coma separated skus)
echo sizeof($variations_skus) > 0 ? implode( ', ', $variations_skus ) : 'No skus';
add a comment |
The shortest and lightest way is to use WPDB Class to make an SQL query embedded in a function:
function get_variations_skus( $product_id ){
global $wpdb;
return $wpdb->get_col("
SELECT pm.meta_value
FROM {$wpdb->prefix}postmeta as pm
INNER JOIN {$wpdb->prefix}posts as p ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = $product_id
AND pm.meta_key = '_sku'
AND pm.meta_value != ''
");
}
Code goes in function.php file of your active child theme (or active theme) or in a plugin. Tested and works.
USAGE - In a variable product:
global $product;
// Variable product main sku
$sku = $product->get_sku();
// The variations skus for this variable product (in an array)
$variations_skus = get_variations_skus( $product->get_id() );
// Testing output variations skus: Array to string conversion (coma separated skus)
echo sizeof($variations_skus) > 0 ? implode( ', ', $variations_skus ) : 'No skus';
The shortest and lightest way is to use WPDB Class to make an SQL query embedded in a function:
function get_variations_skus( $product_id ){
global $wpdb;
return $wpdb->get_col("
SELECT pm.meta_value
FROM {$wpdb->prefix}postmeta as pm
INNER JOIN {$wpdb->prefix}posts as p ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = $product_id
AND pm.meta_key = '_sku'
AND pm.meta_value != ''
");
}
Code goes in function.php file of your active child theme (or active theme) or in a plugin. Tested and works.
USAGE - In a variable product:
global $product;
// Variable product main sku
$sku = $product->get_sku();
// The variations skus for this variable product (in an array)
$variations_skus = get_variations_skus( $product->get_id() );
// Testing output variations skus: Array to string conversion (coma separated skus)
echo sizeof($variations_skus) > 0 ? implode( ', ', $variations_skus ) : 'No skus';
answered Jan 3 at 23:40
LoicTheAztecLoicTheAztec
96.5k1371113
96.5k1371113
add a comment |
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%2f54030358%2fget-the-product-variations-skus-from-a-variable-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