Support » Plugin: Custom Product Tabs for WooCommerce » Tab content shortcode

  • Resolved drspock

    (@drspock)


    I use this wonderful plugin with woocommerce. For some reason, I would like to be able to retrieve tabs content for each product with a shortcode. Is that possible?

    Thanks in advance for your help

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

    (@yikesitskevin)

    Hi @drspock,

    This is not possible but we might be able to work out a different solution. What are you trying to do? Do you want to put the tab content somewhere else on the product page?

    Let me know.

    Cheers,
    Kevin.

    Thread Starter drspock

    (@drspock)

    Wow @yikesitskevin you answer always so fast our questions, that’s amazing! How are you doing that???

    To be short, I am using “WC product Builder” which is an extension for DIVI that allows designing woocommerce product template pages without coding. Since I am not (yet) the king of php (I am learning), that’s the most convenient way I found to build a custom product page. However, my client wants accordions on some pages instead of tabs. So, I would like to be able to retrieve tab content and put it in the accordion module of DIVI. Hope my explanation is clear…

    Thanks for your help

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Hi @drspock,

    I just respond to tickets when I see them in my inbox 😛

    Unfortunately you won’t be able to use our tabs inside of an accordion like that.

    The only way (that I know of) to change WooCommerce’s product tabs layout is to use their template hierarchy and override the tabs.php file. (More info on that here: https://docs.woocommerce.com/document/template-structure/).

    Alternatively, you could also try creating your own shortcode (and I could help you with that). That will help move you one step closer to being the king of php 😉

    Cheers,
    Kevin.

    • This reply was modified 3 years, 8 months ago by yikesitskevin.
    Thread Starter drspock

    (@drspock)

    Not everybody responds as soon as they get the tickets, it takes sometimes hours or even days before getting an answer! Your service is really excellent!

    The thing is that the “WC product builder” has likely its own template, I am not sure it uses Woocommerce’s template… DIVI is tricky when it comes to customizing things and templates… I like the option of my own shortcode if you think it’s something feasible, as you said that would be a step forward in my php knowledge! I guess I have to use the wordpress shortcode API?

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Yes we’ll be using the WordPress Shortcode API. I will give you a good example to get started with though. Give me a couple minutes.

    Thread Starter drspock

    (@drspock)

    Take your time! That’s not good for your heart to be stressed!:-)

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Okay here is a quick shortcode example.

    function yikes_custom_product_tabs_shortcode( $args ) {
    	global $post;
    	
    	// Define our default values
    	$defaults = array(
    		'product_id' => $post->ID,
    	);
    
    	// Let the user-defined values override our defaults
    	$values = is_array( $args ) ? array_merge( $defaults, $args ) : $defaults;
    
    	// Make sure we have a product ID and that the product ID is for a product 
    	if ( empty( $values['product_id'] ) || ! empty( $values['product_id'] ) && get_post_type( $values['product_id'] ) !== 'product' ) {
    		return;
    	}
    
    	// Fetch our tabs
    	$tabs = get_post_meta( $values['product_id'], 'yikes_woo_products_tabs', true );
    
    	// Debug statement to show all tab data. Feel free to remove.
    	// echo '<pre>'; var_dump( $tabs ); echo '</pre>';
    
    	$html = '';
    
    	// Loop through the tabs and display each one
    	foreach( $tabs as $tab ) {
    		$html .= '<p>' . $tab['title'] . '</p>';
    		$html .= '<p>' . $tab['content'] . '</p>';
    	}
    
    	// Make sure to return your content, do not echo it.
    	return $html;
    }
    
    add_shortcode( 'custom_product_tabs', 'yikes_custom_product_tabs_shortcode' );

    I added this to my functions.php file so I could easily access it.

    You can use it on a single product page like [custom_product_tabs] and it will automatically pick up the current product. Alternatively, you could pass in a product ID like [custom_product_tabs product_id="10"] to grab all of the tabs for the product with an ID of 10.

    It doesn’t do anything special right now – it fetches all of the tabs, loops through them, and returns the title and content for each one.

    Try it out and let me know how comfortable you are working with it.

    Cheers,
    Kevin.

    Thread Starter drspock

    (@drspock)

    Thanks, seems great! Now, this will output all tabs in one “html shot”, what if I would like to output a single tab content (title and content)? Is there a way to have for example [custom_product_tabs tab_id=”1″], [custom_product_tabs tab_id=”2″]?

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    I figured you might want that for placing individual tabs in an accordion but I wasn’t sure how it would work. Would you know how many tabs a product has before using the shortcode? Are you able and willing to add the shortcode individually to each accordion “piece”?

    If so, here’s an updated version. In this version, there’s a new argument tab_number. To show the first tab: [custom_product_tabs tab_number="1"]. If you don’t specify a tab number then it will show all of the tabs. If you specify a tab number that doesn’t exist, then nothing will be returned.

    function yikes_custom_product_tabs_shortcode( $args ) {
    	global $post;
    	
    	// Define our default values
    	$defaults = array(
    		'product_id' => $post->ID,
    		'tab_number' => 'all',
    	);
    
    	// Let the user-defined values override our defaults
    	$values = is_array( $args ) ? array_merge( $defaults, $args ) : $defaults;
    
    	// Make sure we have a product ID and that the product ID is for a product 
    	if ( empty( $values['product_id'] ) || ! empty( $values['product_id'] ) && get_post_type( $values['product_id'] ) !== 'product' ) {
    		return;
    	}
    
    	// Fetch our tabs
    	$tabs = get_post_meta( $values['product_id'], 'yikes_woo_products_tabs', true );
    
    	// Get just the specified tab. (minus tab number by one so it starts at 0)
    	$tabs = absint( $values['tab_number'] ) < 1 ? $tabs : ( isset( $tabs[ absint( $values['tab_number'] ) - 1 ] ) ? array( $tabs[ absint( $values['tab_number'] ) - 1 ] ) : array() );
    
    	if ( empty( $tabs ) ) {
    		return;
    	}
    
    	// Debug statement to show all tab data. Feel free to remove.
    	// echo '<pre>'; var_dump( $tabs ); echo '</pre>';
    
    	$html = '';
    
    	// Loop through the tabs and display each one
    	foreach( $tabs as $tab ) {
    		$html .= '<p>' . $tab['title'] . '</p>';
    		$html .= '<p>' . $tab['content'] . '</p>';
    	}
    
    	// Make sure to return your content, do not echo it.
    	return $html;
    }
    
    add_shortcode( 'custom_product_tabs', 'yikes_custom_product_tabs_shortcode' );

    Make sure to remove the old snippet before adding this new one.

    Cheers,
    Kevin.

    Thread Starter drspock

    (@drspock)

    This is exactly what I need! You know what? I think that you should add this feature to your paid version! I am sure that it would lead some sale, and the job is already done!

    I don’t need the full version of the plugin yet, but I would like to thank you, is there a way to send you something? Coffee is on me!

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Haha maybe we will add it to our paid plugin.

    I’m glad I could help!

    You don’t have to send us anything but a nice review is always appreciated (https://wordpress.org/support/plugin/yikes-inc-easy-custom-woocommerce-product-tabs/reviews/#new-post) 🙂

    Thread Starter drspock

    (@drspock)

    The review is done, thanks again it’s really appreciated!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Tab content shortcode’ is closed to new replies.