Get term names array from all product for a specific product attribute in Woocommerce
I'm trying to get an array with all custom terms for a specific product attribute "Color" From all products.
This is my function code:
// custom_terms_taxonomy function
function custom_terms_taxonomy()
{
$custom_terms = array();
// array with products
$products_array = array( 'post_type' => 'product', 'posts_per_page' => -1 );
foreach ( $products_array as $product_array ) {
// attribute string
$terms_array = $product_array->get_attribute( 'pa_color' );
// array with attributes
$terms_array = ! empty($terms_array) ? (array) explode(', ', $terms_array) : array();
foreach ( $terms_array as $term_array ) {
// check if the value exist aleready in array
if (!in_array($term_array, $custom_terms)) {
// add attributes to Custom_array
array_push($custom_terms,$term_array);
}
}
}
return $custom_terms;
}
//output
$att_array=custom_terms_taxonomy();
print_r($att_array);
This script didn't work. Any help is appreciated.
php wordpress woocommerce custom-taxonomy taxonomy-terms
add a comment |
I'm trying to get an array with all custom terms for a specific product attribute "Color" From all products.
This is my function code:
// custom_terms_taxonomy function
function custom_terms_taxonomy()
{
$custom_terms = array();
// array with products
$products_array = array( 'post_type' => 'product', 'posts_per_page' => -1 );
foreach ( $products_array as $product_array ) {
// attribute string
$terms_array = $product_array->get_attribute( 'pa_color' );
// array with attributes
$terms_array = ! empty($terms_array) ? (array) explode(', ', $terms_array) : array();
foreach ( $terms_array as $term_array ) {
// check if the value exist aleready in array
if (!in_array($term_array, $custom_terms)) {
// add attributes to Custom_array
array_push($custom_terms,$term_array);
}
}
}
return $custom_terms;
}
//output
$att_array=custom_terms_taxonomy();
print_r($att_array);
This script didn't work. Any help is appreciated.
php wordpress woocommerce custom-taxonomy taxonomy-terms
add a comment |
I'm trying to get an array with all custom terms for a specific product attribute "Color" From all products.
This is my function code:
// custom_terms_taxonomy function
function custom_terms_taxonomy()
{
$custom_terms = array();
// array with products
$products_array = array( 'post_type' => 'product', 'posts_per_page' => -1 );
foreach ( $products_array as $product_array ) {
// attribute string
$terms_array = $product_array->get_attribute( 'pa_color' );
// array with attributes
$terms_array = ! empty($terms_array) ? (array) explode(', ', $terms_array) : array();
foreach ( $terms_array as $term_array ) {
// check if the value exist aleready in array
if (!in_array($term_array, $custom_terms)) {
// add attributes to Custom_array
array_push($custom_terms,$term_array);
}
}
}
return $custom_terms;
}
//output
$att_array=custom_terms_taxonomy();
print_r($att_array);
This script didn't work. Any help is appreciated.
php wordpress woocommerce custom-taxonomy taxonomy-terms
I'm trying to get an array with all custom terms for a specific product attribute "Color" From all products.
This is my function code:
// custom_terms_taxonomy function
function custom_terms_taxonomy()
{
$custom_terms = array();
// array with products
$products_array = array( 'post_type' => 'product', 'posts_per_page' => -1 );
foreach ( $products_array as $product_array ) {
// attribute string
$terms_array = $product_array->get_attribute( 'pa_color' );
// array with attributes
$terms_array = ! empty($terms_array) ? (array) explode(', ', $terms_array) : array();
foreach ( $terms_array as $term_array ) {
// check if the value exist aleready in array
if (!in_array($term_array, $custom_terms)) {
// add attributes to Custom_array
array_push($custom_terms,$term_array);
}
}
}
return $custom_terms;
}
//output
$att_array=custom_terms_taxonomy();
print_r($att_array);
This script didn't work. Any help is appreciated.
php wordpress woocommerce custom-taxonomy taxonomy-terms
php wordpress woocommerce custom-taxonomy taxonomy-terms
edited Nov 25 '18 at 18:15
LoicTheAztec
93.4k1367106
93.4k1367106
asked Nov 25 '18 at 17:40
mywebhostelmywebhostel
223
223
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
There are some mistakes and missing things in your code… try the following:
// custom_terms_taxonomy function
function custom_terms_taxonomy()
{
$custom_terms = array();
// array with products
$products = wc_get_products( array( 'status' => 'publish', 'limit' => -1 ) );
foreach ( $products as $product ) {
// attribute string
$terms_names = $product->get_attribute( 'pa_color' );
// array with attributes
$terms_names_array = ! empty($terms_names) ? (array) explode(', ', $terms_names) : array();
foreach ( $terms_names_array as $terms_name ) {
// check if the value exist aleready in array
if ( !in_array( $terms_name, $custom_terms ) ) {
// add attribute term name in the array
$custom_terms = $terms_name;
}
}
}
return $custom_terms;
}
// Raw output
print_r(custom_terms_taxonomy());
Tested and works.
Works perfectly!
– mywebhostel
Nov 25 '18 at 19:25
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%2f53470147%2fget-term-names-array-from-all-product-for-a-specific-product-attribute-in-woocom%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
There are some mistakes and missing things in your code… try the following:
// custom_terms_taxonomy function
function custom_terms_taxonomy()
{
$custom_terms = array();
// array with products
$products = wc_get_products( array( 'status' => 'publish', 'limit' => -1 ) );
foreach ( $products as $product ) {
// attribute string
$terms_names = $product->get_attribute( 'pa_color' );
// array with attributes
$terms_names_array = ! empty($terms_names) ? (array) explode(', ', $terms_names) : array();
foreach ( $terms_names_array as $terms_name ) {
// check if the value exist aleready in array
if ( !in_array( $terms_name, $custom_terms ) ) {
// add attribute term name in the array
$custom_terms = $terms_name;
}
}
}
return $custom_terms;
}
// Raw output
print_r(custom_terms_taxonomy());
Tested and works.
Works perfectly!
– mywebhostel
Nov 25 '18 at 19:25
add a comment |
There are some mistakes and missing things in your code… try the following:
// custom_terms_taxonomy function
function custom_terms_taxonomy()
{
$custom_terms = array();
// array with products
$products = wc_get_products( array( 'status' => 'publish', 'limit' => -1 ) );
foreach ( $products as $product ) {
// attribute string
$terms_names = $product->get_attribute( 'pa_color' );
// array with attributes
$terms_names_array = ! empty($terms_names) ? (array) explode(', ', $terms_names) : array();
foreach ( $terms_names_array as $terms_name ) {
// check if the value exist aleready in array
if ( !in_array( $terms_name, $custom_terms ) ) {
// add attribute term name in the array
$custom_terms = $terms_name;
}
}
}
return $custom_terms;
}
// Raw output
print_r(custom_terms_taxonomy());
Tested and works.
Works perfectly!
– mywebhostel
Nov 25 '18 at 19:25
add a comment |
There are some mistakes and missing things in your code… try the following:
// custom_terms_taxonomy function
function custom_terms_taxonomy()
{
$custom_terms = array();
// array with products
$products = wc_get_products( array( 'status' => 'publish', 'limit' => -1 ) );
foreach ( $products as $product ) {
// attribute string
$terms_names = $product->get_attribute( 'pa_color' );
// array with attributes
$terms_names_array = ! empty($terms_names) ? (array) explode(', ', $terms_names) : array();
foreach ( $terms_names_array as $terms_name ) {
// check if the value exist aleready in array
if ( !in_array( $terms_name, $custom_terms ) ) {
// add attribute term name in the array
$custom_terms = $terms_name;
}
}
}
return $custom_terms;
}
// Raw output
print_r(custom_terms_taxonomy());
Tested and works.
There are some mistakes and missing things in your code… try the following:
// custom_terms_taxonomy function
function custom_terms_taxonomy()
{
$custom_terms = array();
// array with products
$products = wc_get_products( array( 'status' => 'publish', 'limit' => -1 ) );
foreach ( $products as $product ) {
// attribute string
$terms_names = $product->get_attribute( 'pa_color' );
// array with attributes
$terms_names_array = ! empty($terms_names) ? (array) explode(', ', $terms_names) : array();
foreach ( $terms_names_array as $terms_name ) {
// check if the value exist aleready in array
if ( !in_array( $terms_name, $custom_terms ) ) {
// add attribute term name in the array
$custom_terms = $terms_name;
}
}
}
return $custom_terms;
}
// Raw output
print_r(custom_terms_taxonomy());
Tested and works.
answered Nov 25 '18 at 18:08
LoicTheAztecLoicTheAztec
93.4k1367106
93.4k1367106
Works perfectly!
– mywebhostel
Nov 25 '18 at 19:25
add a comment |
Works perfectly!
– mywebhostel
Nov 25 '18 at 19:25
Works perfectly!
– mywebhostel
Nov 25 '18 at 19:25
Works perfectly!
– mywebhostel
Nov 25 '18 at 19:25
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%2f53470147%2fget-term-names-array-from-all-product-for-a-specific-product-attribute-in-woocom%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