WordPress.org

Forums

WooCommerce - excelling eCommerce
Adding a Brand taxonomy and displaying it in the single template (7 posts)

  1. nortont
    Member
    Posted 2 years ago #

    I have successfully created a new taxonomy for brands using the code below but can not work out how to display it in the product page.

    This is how I ad the custom taxonomy

    //hook into the init action and call create_book_taxonomies when it fires
    add_action( 'init', 'create_product_taxonomies', 0 );
    //add_action('admin_init', 'flush_rewrite_rules'); 
    
    //create two taxonomies, genres and writers for the post type "book"
    function create_product_taxonomies()
    {
      // Add new taxonomy, make it hierarchical (like categories)
      $labels = array(
        'name' => _x( 'Brands', 'taxonomy general name' ),
        'singular_name' => _x( 'Brand', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Brands' ),
        'all_items' => __( 'All Brands' ),
        'parent_item' => __( 'Parent Brand' ),
        'parent_item_colon' => __( 'Parent Brands:' ),
        'edit_item' => __( 'Edit Brands' ),
        'update_item' => __( 'Update Brands' ),
        'add_new_item' => __( 'Add New Brand' ),
        'new_item_name' => __( 'New Brand Name' ),
        'menu_name' => __( 'Brands' ),
      );     
    
      register_taxonomy('product_brand',array('product'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'query_var' => true,
        //'rewrite' => true,
        'rewrite' => array( 'slug' => 'brands', 'with_front' => true ),
      ));
    function filter_post_type_link($link, $post)
    {
        if ($post->post_type != 'product')
            return $link;
      if ($cats = get_the_terms($post->ID, 'product_brand'))
        if ($cats = get_the_terms($post->ID, 'product_brand'))
            $link = str_replace('%product_brand%', array_pop($cats)->slug, $link);
        return $link;
    }
    add_filter('post_type_link', 'filter_post_type_link', 10, 2);

    All good so far and can add my brands as I add a product.

    To try to add it to the template I have found the following in meta.php

    <?php echo $product->get_categories( ', ', ' <span class="posted_in">'.__('Category:', 'woocommerce').' ', '.</span>'); ?>
    	<?php echo $product->get_tags( ', ', ' <span class="tagged_as">'.__('Tags:', 'woocommerce').' ', '.</span>'); ?>

    and found the code for the get_categories array in woocommerce_template.php but am not sure what to copy and change to use it.

    // NOTE: using child_of instead of parent - this is not ideal but due to a WP bug ( http://core.trac.wordpress.org/ticket/15626 ) pad_counts won't work
    		$args = array(
    			'child_of'		=> $product_category_parent,
    			'menu_order'	=> 'ASC',
    			'hide_empty'	=> 1,
    			'hierarchical'	=> 1,
    			'taxonomy'		=> 'product_cat',
    			'pad_counts'	=> 1
    		);
    		$product_categories = get_categories( $args  );
    
    		$product_category_found = false;
    
    		if ( $product_categories ) {
    
    			foreach ( $product_categories as $category ) {
    
    				if ( $category->parent != $product_category_parent )
    					continue;
    
    				if ( ! $product_category_found ) {
    					// We found a category
    					$product_category_found = true;
    					echo $before;
    				}
    
    				woocommerce_get_template( 'content-product_cat.php', array(
    					'category' => $category
    				) );
    
    			}
    
    		}

    Appreciate your help
    charlotteswebpapercraft.com.au

    http://wordpress.org/extend/plugins/woocommerce/

  2. bheadrick
    Member
    Posted 2 years ago #

    Why not add a Brand Attribute?

  3. nortont
    Member
    Posted 2 years ago #

    Please explain a little Bheadrick more as I do not know what this is/does

  4. bheadrick
    Member
    Posted 2 years ago #

  5. siyamak45
    Member
    Posted 1 year ago #

    @nortont Did you solve this problem?

  6. nortont
    Member
    Posted 1 year ago #

    I ended up using the brands plugin - Not totally happy with it but it does the job.
    Here is my use - http://charlotteswebpapercraft.com.au/brands/

    here is the plugin http://www.woothemes.com/products/brands/

  7. bheadrick
    Member
    Posted 1 year ago #

    @nortont, what don't you like about it? What do you want to do that isn't being implemented?

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic