• Resolved Tony Brocklebank

    (@tony-brocklebank)


    Hi,

    I am building a new site based on an old site with around 6,000 products and 5000 categories.

    This makes the page look very untidy, so I’d like to add a global tab to solve the problem.

    I know I need the Pro version to allow me to add a global tab, but what I’d like to be able to do is move the categories from the top of the product page and list them, ideally under with the correct hierarchy as a seperate tab.

    I’ve manually set up one product here so you can see what I mean:

    Can I do this with your plugin, and if not would you be willing to customise for us?

    Thanks

    Tony

    The page I need help with: [log in to see the link]

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Hi Tony,

    Sounds like you’ll need to use a custom PHP function (or shortcode) to populate the contents of a tab. How are the categories related to the product? Do you want to show the product’s current category and all sub categories? Or do you just want to show all of the product categories?

    Let me know the technical relationship between the product and the categories shown on the tab and I can try to write a function for you.

    Thank you,
    Kevin.

    Thread Starter Tony Brocklebank

    (@tony-brocklebank)

    Thanks! Just realised I’ve emailed you instead of replying here, hope that’s ok?

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    I didn’t receive an email – if the contents are safe for the public, can you paste it here? Let me know.

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Hi Tony,

    Let’s start with trying to make a shortcode that will display the top level categories attached to the product. Then we can work on removing the categories from the page, making the output look nice, etc.

    Here is a shortcode function. Add this to your child theme’s functions.php file and add the [yikes_display_product_categories] shortcode to a custom tab.

    add_shortcode( 'yikes_display_product_categories', 'yikes_display_product_categories' );
    
    function yikes_display_product_categories() {
    	global $product;
    
    	if ( ! is_object( $product ) || empty( $product ) ) {
    		return;
    	}
    
    	$product_id    = $product->get_id();
    	$product_terms = get_the_terms( $product_id, 'product_cat' );
    
    	if ( empty( $product_terms ) ) {
    		return;
    	}
    
    	$shortcode_html = '';
    
    	$shortcode_html .= '<ul class="yikes-product-cats-shortcode">';
    	foreach ( $product_terms as $term ) {
    
    		// Filter out Toshiba and child cats.
    		if ( ! empty( $term->parent ) || $term->name === 'Toshiba' ) {
    			continue;
    		}
    		$shortcode_html .= "<li>{$term->name}</li>";
    	}
    	$shortcode_html .= '</ul>';
    
    	return $shortcode_html;
    }

    Cheers,
    Kevin.

    Thread Starter Tony Brocklebank

    (@tony-brocklebank)

    I’m probably doing something wrong, I’m getting the following error:

    Your PHP code changes were rolled back due to an error on line 35 of file wp-content/themes/maxstore-child/functions.php. Please fix and try saving again.

    syntax error, unexpected ‘function’ (T_FUNCTION)

    Have I added it in the right place? The file is :

    
    <?php
    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;
    
    // BEGIN ENQUEUE PARENT ACTION
    // AUTO GENERATED - Do not modify or remove comment markers above or below:
    
    if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
        function chld_thm_cfg_locale_css( $uri ){
            if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
                $uri = get_template_directory_uri() . '/rtl.css';
            return $uri;
        }
    endif;
    add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
    
    if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
        function chld_thm_cfg_parent_css() {
            wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( 'bootstrap' ) );
        }
    endif;
    add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
             
    if ( !function_exists( 'child_theme_configurator_css' ) ):
        function child_theme_configurator_css() {
            wp_enqueue_style( 'chld_thm_cfg_separate', trailingslashit( get_stylesheet_directory_uri() ) . 'ctc-style.css', array( 'chld_thm_cfg_parent','maxstore-stylesheet','font-awesome' ) );
        }
    endif;
    add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 20 );
    
    // END ENQUEUE PARENT ACTION
    
    add_shortcode( 'yikes_display_product_categories', 'yikes_display_product_categories' );
    
    function yikes_display_product_categories() {
    	global $product;
    
    	if ( ! is_object( $product ) || empty( $product ) ) {
    		return;
    	}
    
    	$product_id    = $product->get_id();
    	$product_terms = get_the_terms( $product_id, 'product_cat' );
    
    	if ( empty( $product_terms ) ) {
    		return;
    	}
    
    	$shortcode_html = '';
    
    	$shortcode_html .= '<ul class=&quot;yikes-product-cats-shortcode&quot;>';
    	foreach ( $product_terms as $term ) {
    
    		// Filter out Toshiba and child cats.
    		if ( ! empty( $term->parent ) || $term->name === 'Toshiba' ) {
    			continue;
    		}
    		$shortcode_html .= &quot;<li>{$term->name}</li>&quot;;
    	}
    	$shortcode_html .= '</ul>';
    
    	return $shortcode_html;
    }
    
    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Hmm… Looks fine to me… I do see a mix of quote styles. I’m wondering if the markup is getting messed up. Do you have SFTP access?

    Thread Starter Tony Brocklebank

    (@tony-brocklebank)

    Sorry, ignore all that – I’d copied from the email – when I copied it from here it worked!

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Can you remove the bottom part of your last reply or wrap the code in backticks (or by hitting the code button)? The stray HTML is breaking the forums HTML ??

    Thread Starter Tony Brocklebank

    (@tony-brocklebank)

    It’s sort of worked but the wrong way round?

    It’s removed the parent tag for Toshiba, but left all the other lower level tages, but removed all the product categories?

    Unless I’ve done something wrong?

    Thread Starter Tony Brocklebank

    (@tony-brocklebank)

    It’s worked but the wrong way round.

    The top level parent tags are still there, but the sub categories and lowest level categories have gone.

    I was trying to get just the lowest level categories to display?

    It might be the way I’ve explained what I needed?

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Can you link me the product the shortcode is being used on? I don’t see it. What’s the tab name?

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Oh, I thought you only wanted the top level. We can reverse that…

    add_shortcode( 'yikes_display_product_categories', 'yikes_display_product_categories' );
    
    function yikes_display_product_categories() {
    	global $product;
    
    	if ( ! is_object( $product ) || empty( $product ) ) {
    		return;
    	}
    
    	$product_id    = $product->get_id();
    	$product_terms = get_the_terms( $product_id, 'product_cat' );
    
    	if ( empty( $product_terms ) ) {
    		return;
    	}
    
    	$shortcode_html = '';
    
    	$shortcode_html .= '<ul class="yikes-product-cats-shortcode">';
    	foreach ( $product_terms as $term ) {
    
    		// Filter out Toshiba and child cats.
    		if ( empty( $term->parent ) || $term->name === 'Toshiba' ) {
    			continue;
    		}
    		$shortcode_html .= "<li>{$term->name}</li>";
    	}
    	$shortcode_html .= '</ul>';
    
    	return $shortcode_html;
    }

    Also, can you please edit your earlier reply where you pasted the entire file? If you look at the post on the forums it’s messing up the whole page’s HTML ??

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Moving categories to a tab globally’ is closed to new replies.