Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display the terms or product attributes for a specific product category(woocommerce)

I have been researching all over the net and forums regarding my question but I can't seem to produce the correct results. Basically I'm trying to display the terms or product attributes for only a specific product category.

Here is the code I have been working on.

<fieldset class="liquor-types-control filter-controls" >

    <?php 

    $terms = get_terms( 'wine', /*<--Product Category */ 'pa_liquor-types' /* <--Product Attribute */);
    foreach ( $terms as $term ) :
    ?>

    <label> 
        <input type="checkbox" value="<?php echo "." . $term->slug ?>"  /> 
        <?php echo $term->name ?>
    </label>

    <?php endforeach; ?>


</fieldset>


(
    [errors] => Array
        (
            [invalid_taxonomy] => Array
                (
                    [0] => Invalid taxonomy.
                )

        )

    [error_data] => Array
        (
        )
)
like image 708
clestcruz Avatar asked Nov 14 '16 03:11

clestcruz


People also ask

How do I display a specific category product in WooCommerce?

Go to WooCommerce → Settings, select the Products tab, and then choose the Display option. For each of the Shop Page Display and Default Category Display options, select Show products. Save your changes.

How do I display a particular category product in WooCommerce shortcode?

These two shortcodes will display your product categories on any page. [product_category] – Will display products in a specified product category. [product_categories] – Will display all your product categories.


2 Answers

The var_dump is showing that you are using taxonomies on WordPress. While I don't have experience directly with Wordpress, the Wordpress site docs say:

Prior to 4.5.0, the first parameter of get_terms() was a taxonomy or list of taxonomies:

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:

From the function reference:

$term = get_term( $term_id, $taxonomy );

Gives you term slug: e.g.: term-slug-example

$slug = $term->slug;

Gives you term name: e.g. Term Name Example

$name = $term->name; 

First, make sure you are using the correct version - you are using the syntax from prior 4.5.0

Second, the error is saying that the taxonomy pa_liquor-types is invalid. You need to check where this is being defined.

Check your create_initial_taxonomies() function syntax and post it if necessary.

like image 174
mseifert Avatar answered Sep 22 '22 12:09

mseifert


try something like this:

<?php
$terms = get_terms( 'wine', 'pa_liquor-types');

foreach($terms as $term) { ?>
    <input type="checkbox" value="<?php echo "." . $term['slug'];?>"/>
    <?php echo $term['name'];?>
<?php } ?>
like image 43
Grey Avatar answered Sep 24 '22 12:09

Grey