Using the Yoast SEO Primary Category selection in Woocommerce
authorlaury <laury@gaslightmedia.com>
Wed, 20 Jul 2016 16:13:42 +0000 (12:13 -0400)
committerlaury <laury@gaslightmedia.com>
Wed, 20 Jul 2016 16:13:42 +0000 (12:13 -0400)
The tours page now shows the name of the Primary Category as set
by yoast SEO rather than the first category Woo picks by default.

woocommerce.php

index ec350f7..eecb4c0 100644 (file)
                 </div>
             <?php } if (is_product_tag() || is_product_category()) { ?>
                 <div class="small-12 large-12 columns main woo-tags">
-                <?php
-                        /**
-                         * woocommerce_before_main_content hook
-                         *
-                         * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
-                         * @hooked woocommerce_breadcrumb - 20
-                         */
-                        function wc_category_title_archive_products(){
-
-                        $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
-
-                        if ( $product_cats && ! is_wp_error ( $product_cats ) ){
+                    <?php
+                    /**
+                     * woocommerce_before_main_content hook
+                     *
+                     * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
+                     * @hooked woocommerce_breadcrumb - 20
+                     */
+                    function wc_category_title_archive_products(){
 
-                            $single_cat = array_shift( $product_cats ); ?>
+                    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
 
-                            <small class="product_category_title_item"><?php echo $single_cat->name; ?></small>
+                    if ( $product_cats && ! is_wp_error ( $product_cats ) ){
 
+                        $single_cat = array_shift( $product_cats );
+                        
+                        // Set name to the default first picked category (however WC decides that)
+                        $primaryCatName = $single_cat->name;
+                        
+                        // Try to get the Primary Category based on how Yoast SEO has set it in the postmeta table
+                        global $wpdb;
+                        $sqlGetPrimary = "SELECT meta_value
+                                FROM $wpdb->postmeta
+                                WHERE
+                                    (meta_key = '_yoast_wpseo_primary_product_cat' AND post_id = '". get_the_id() ."');
+                        ";
+                        $primaryCatID = $wpdb->get_var($wpdb->prepare($sqlGetPrimary));
+                        $term = get_term_by( 'id', $primaryCatID, 'product_cat' );
+                        
+                        // Use the primary category instead if it's valid
+                        if( $term->name ) {
+                            $primaryCatName = $term->name;
+                        }
+                    ?>
+                    
+                    <small class="product_category_title_item"><?php echo $primaryCatName; ?></small>          
+                    
                     <?php }
                     }
                     add_action( 'woocommerce_after_shop_loop_item_title', 'wc_category_title_archive_products', 5 );
-                                            do_action( 'woocommerce_before_main_content' );
-                                            add_filter( 'loop_shop_columns', 'tm_product_columns', 5);
-                                                function tm_product_columns($columns) {
-                                                    if ( is_product_category() || is_product_tag() ) {
-                                                        $columns = 3;
-                                                        return $columns;
-                                                    }
-                    } ?>
+                    do_action( 'woocommerce_before_main_content' );
+                    add_filter( 'loop_shop_columns', 'tm_product_columns', 5);
+                    function tm_product_columns($columns) {
+                        if ( is_product_category() || is_product_tag() ) {
+                            $columns = 3;
+                            return $columns;
+                        }
+                    } 
+                    ?>
 
                <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>