• Resolved had_hc

    (@had_hc)


    Hi, I’m trying to create a custom taxonomy for woocommerce product using the code below;

    function add_gift_taxonomies() {
         // Add new "Locations" taxonomy to Posts
         register_taxonomy_for_object_type( 'product_gifts', 'product' );
         register_taxonomy('product_gifts', 'product', array(
           // Hierarchical taxonomy (like categories)
           'hierarchical' => true,
     	  'public'                     => true,
         'show_ui'                    => true,
         'show_admin_column'          => true,
         'show_in_nav_menus'          => true,
         'show_tagcloud'              => true,
           // This array of options controls the labels displayed in the WordPress Admin UI
           'labels' => array(
             'name' => _x( 'Gifts', 'taxonomy general name' ),
             'singular_name' => _x( 'Gift', 'taxonomy singular name' ),
             'search_items' =>  __( 'Search Gifts' ),
             'all_items' => __( 'All Gifts' ),
             'parent_item' => __( 'Parent Gift' ),
             'parent_item_colon' => __( 'Parent Gift:' ),
             'edit_item' => __( 'Edit Gift' ),
             'update_item' => __( 'Update Gift' ),
             'add_new_item' => __( 'Add New Gift' ),
             'new_item_name' => __( 'New Gift Name' ),
             'menu_name' => __( 'Gifts' ),
           ),
           // Control the slugs used for this taxonomy
           'rewrite' => array(
             'slug' => 'gifts', // This controls the base slug that will display before each term
             'with_front' => false, // Don't display the category base before "/locations/"
             'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
           ),
         ));
       }
       add_action( 'init', 'add_gift_taxonomies', 0 );

    This code is based on an article from Smashing Magazine. There’s no problem in creating the taxonomy. All is well.

    However, when trying to view the taxonomy, i got a “Page not found” error. Tried to add a new file taxonomy-product_gifts.php file in the child theme root folder with below code;

    <?php
    /**
     * The Template for displaying products in a product category. Simply includes the archive template
     *
     * This template can be overridden by copying it to yourtheme/woocommerce/taxonomy-product_cat.php.
     *
     * HOWEVER, on occasion WooCommerce will need to update template files and you (the theme developer).
     * will need to copy the new files to your theme to maintain compatibility. We try to do this.
     * as little as possible, but it does happen. When this occurs the version of the template file will.
     * be bumped and the readme will list any important changes.
     *
     * @see 	    http://docs.woothemes.com/document/template-structure/
     * @package 	WooCommerce/Templates
     * @version     1.6.4
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly
    }
    
    wc_get_template( 'archive-product.php' );

    But still got page not found error.

    Tried using solution found from Stackoverflow by modifying for mine as below;

    add_filter('template_include', 'product_gifts_set_template');
       function product_gifts_set_template( $template ){
         if(is_tax('product_gifts')) :
           $taxonomy = 'product_gifts';
           $term = get_query_var($taxonomy);
           $prod_term = get_terms($taxonomy, 'slug='.$term.'');
           $term_slug = $prod_term[0]->slug;
           $t_id = $prod_term[0]->term_id;
           $term_meta = get_option( "taxonomy_$t_id" );
           $term_meta['product_gifts_access_pin'];
    
           wc_get_template( 'archive-product.php' );
    
        else :
    
           wc_get_template( 'archive-product.php' );
    
        endif;
    
       }

    But still, not found.

    Can anybody help me and tell me if there is anything wrong that I’m doing here? What am I missing here?

    Thanks in advance!

    https://wordpress.org/plugins/woocommerce/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    Settings > Permalinks > Save.

    Also, it must have a post within it.

    Thread Starter had_hc

    (@had_hc)

    ahh, i knew it I’m missing some simple step only. it’s working now, thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Display Woocommerce Custom Taxonomy’ is closed to new replies.