,How To Create A Categorized Blog For Your Woocommerce Store? - SHOPHUB
CodeTips & Tutorials

How To Create A Categorized Blog For Your Woocommerce Store?

In the event that you’re running a WooCommerce store that likewise has a blog, at that point, you may choose to keep your blog totally separate from your store, with its very own classifications and substance. In any case, for the more prominent mix with your store, it may be a smart thought to make blog entries that identify with your item classes.

To do this, you’ll have to add the item class scientific categorization to your normal posts, make a few posts with those item classifications, and after that show the blog entries on the item class document pages in your store.

Fortunately, WooCommerce furnishes you with activity and channel snares you can use to do this.

So we should begin!

What You’ll Need to Create a Blog for Each Category or Department in Your WooCommerce Store

To track, you’ll need:

  • an improvement establishment of WordPress
  • a code supervisor
  • WooCommerce introduced and actuated
  • items added to your store (I’ve imported the spurious item information that accompanies WooCommerce—for subtleties of how to do this, see this guide)
  • A WooCommerce-perfect topic enacted (I’m utilizing Storefront)

Here’s my beginning store:WooCommerce store - front page

Making Our Plugin

I will make a module to do this, which is preferred practice over adding it to your topic. So begin by making a record in your wp-content/modules registry. I’ve called mine tutsplus-woocommerce-classification blog.php.

Open the new document and include the opening lines:

01
02
03
04
05
06
07
08
09
10
11
<?php
/**
 * Plugin Name: Tuts+ Add blog posts to WooCommerce product categrories
 * Plugin URI: https://tutsplus.com
 * Description: Add a list of relevant blog osts to product category archives in WooCommerce
 * Version: 1.0
 * Author: Rachel McCollin
 * Author URI: http://rachelmccollin.co.uk
 * Textdomain: tutsplus
 *
/

Edit the above code to reflect the fact that this is your plugin, and save your plugin file. Now activate the plugin on your site.

Checking WooCommerce is Activated

I don’t need any of the code in this module to run if WooCommerce isn’t enacted on this site. The most ideal approach to check WooCommerce is initiated is to check if the woocommerce channel exists. Provided that this is true, I’ll include the majority of the add_action() calls here, and this will fire the majority of the capacities in my module.

Add this to your module:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
function tutsplus_check_woocommerce_active() {
     
    // check if woocommerce is activated
    if ( class_exists( 'woocommerce' ) ) {
 
        add_action( 'init', 'tutsplus_register_productcats_posts' );
        add_filter( 'woocommerce_taxonomy_args_product_cat', 'tutsplus_change_productcat_label' );
        add_action( 'woocommerce_after_main_content', 'tutsplus_display_productcats_posts' );
 
    }
    
    else {
        return;
    }
}
add_action( 'plugins_loaded', 'tutsplus_check_woocommerce_active' );   

The next step is to register the product_cat taxonomy for the post post type. You do this using the register taxonomy for object type() function.

Add this to your plugin:

1
2
3
4
5
function tutsplus_register_productcats_posts() {
     
     register_taxonomy_for_object_type( 'product_cat', 'post' );
     
}

This adds the product_cat taxonomy, which WooCommerce uses for product categories, to blog posts. Note that I haven’t hooked it to an action, as that’s already done in my check for WooCommerce.

Now if you take a look at your admin screens you’ll see that you have two taxonomies for posts called Categories:Two category taxonomies for posts

That is somewhat confounding, so we should change the name utilized in the administrator menu for item classifications. WooCommerce gives us a channel we can use to do that, called woocommerce_taxonomy_args_product_cat.

So add the capacity beneath to your module:

1
02
03
04
05
06
07
08
09
10
11
function tutsplus_change_productcat_label( $args ) {
    
    $args['labels'] = array(
        'label'         => _x( 'Product Categories', 'tutsplus' ),
        'menu_name'     => _x( 'Product Categories', 'Admin menu name', 'tutsplus' ),
        'add_new_item'  => _x( 'Add new product category', 'tutsplus' ),
    );
    return $args;
}

his will change the name of the product_cat taxonomy to Product Categories in the menu.

Refresh your admin screen and check it:Product categories correctly labelled in the admin menu

That ought to diminish some perplexity (in spite of the fact that you’ll see it doesn’t expel it totally as it’s impractical to change the name in Quick Edit, which we’ll be utilizing in the blink of an eye).

Note: You may choose to expel the standard class scientific categorization. All things considered, utilize the register_taxonony() capacity to enroll it for an unfilled cluster of post types, which successfully evacuates it.

Presently when you include a post, you can allocate item classifications to it. The manner in which you do this will rely upon whether your site is utilizing the Gutenberg editorial manager or not, as WooCommerce item classes don’t work with Gutenberg yet.

On the off chance that you have Gutenberg empowered, you’ll need to include the item classes utilizing the Quick Edit connect underneath the post name in the primary Posts screen:Adding a product category in quick edit

Ensure you don’t befuddle the two scientific categorizations called Category here—there isn’t any approach to rename item classes in Quick Edit.

In case you’re utilizing the exemplary supervisor, you can add item classifications to your posts utilizing the post altering screen:Adding product categories to a postclassic editor

All things considered, after some time, WooCommerce will end up good with Gutenberg and this circumstance will change.

Presently make a couple of posts and give them diverse item classes.

Yielding Posts in the Product Category Archive

The subsequent advance is to yield those posts in the item class document. You do this by making a circle utilizing WP_Query and afterward utilizing the woocommerce_after_shop_loop activity snare which WooCommerce pursues yielding items on the document pages.

In the event that you need to look at this snare, investigate templates/archive-product.php  in WooCommerce. In the event that you like, you should need to utilize an alternate snare to yield your blog entries in a better place.

Create a new function in your plugin file, making sure it has the name you used when hooking it to the woocommerce_after_shop_loop hook earlier on:

1
2
3
function tutsplus_display_productcats_posts() {
    
}

Now let’s populate that function.

Checking We’re on a Product Category Archive

First, add a contingent tag to check in case we’re on an item class document, as you don’t need blog entries showed on the primary shop page or the inquiry page. Inside your capacity, include this:

1
2
3
if ( is_product_category() ) {
}

Now your code will only run on the product category archive pages.

Next, inside the braces of your conditional check, add a line to get the current queried object, i.e. the current product category:

1
$productcat = get_queried_object();

Now we can use that variable when setting up our query arguments. Next, add this:

1
2
3
4
5
6
7
8
9
$args = array(
    'post_type' => 'post',
    'product_cat' => $productcat->slug,
    'posts_per_page' => 5
);
$query = new WP_query ( $args );
if ( $query->have_posts() ) { ?>
        
<?php }

This defines our query arguments, runs the query, and checks if it has any posts. Next, we need to add a loop inside that if statement:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
<section class="productcat-posts">
    <?php echo '<h2>' . esc_html__( 'Blog Posts', 'tutsplus' ) . '</h2>'; ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <?php //contents of loop ?>
        <article id="post-<?php the_ID(); ?>"<?php post_class(); ?>>
        
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <?php the_excerpt(); ?>
            <a href="<?php the_permalink(); ?>"> Read More...</a>
        
        </article>
        <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    
</section>

This opens a new section tag and outputs the blog posts inside an article tag. Make sure you use wp_reset_postdata() at the end to reset the query.

This is what your full function will look like:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function tutsplus_display_posts_in_category_archives() {
    
    if ( is_product_category() ) {
        
        $productcat = get_queried_object();
        
        $args = array(
            'post_type' => 'post',
            'product_cat' => $productcat->slug,
            'posts_per_page' => 5
        );
        $query = new WP_query ( $args );
        if ( $query->have_posts() ) { ?>
        
            <section class="productcat-posts">
                <h2>Blog Posts</h2>
            
                    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            
                    <?php //contents of loop ?>
                    <article id="post-<?php the_ID(); ?>"<?php post_class(); ?>>
                    
                        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                        <?php the_excerpt(); ?>
                        <a href="<?php the_permalink(); ?>"> Read More...</a>
                    
                    </article>
            
                    <?php endwhile; ?>
            
                <?php rewind_posts(); ?>
                
            </section>
          
        <?php }
        
    }
}
add_action( 'woocommerce_after_shop_loop', 'tutsplus_display_posts_in_category_archives' );

Now save your plugin file and check the archive page for a product category you’ve added posts to. In my Clothing category archive, if I scroll down past the products, my posts are displayed:woocommerce store

Presently you have your most recent blog entries for the item classification showed on its chronicle page.

Rundown

Having a blog that is coordinated with the branches of your store can enable you to sell more items and make your blog progressively significant to clients. In this instructional exercise, you’ve figured out how to add blog entries to class file pages in your woocommerce store, furnishing your clients with additional substance.

 

We will be happy to hear your thoughts

      Leave a reply