From f231f68403ced07843b0743b964bdbcc620f3cb6 Mon Sep 17 00:00:00 2001 From: laury Date: Wed, 20 Jul 2016 12:13:42 -0400 Subject: [PATCH] Using the Yoast SEO Primary Category selection in Woocommerce 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 | 64 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/woocommerce.php b/woocommerce.php index ec350f7..eecb4c0 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -67,34 +67,54 @@
- + $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' ); - name; ?> + 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; + } + ?> + + + + 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; + } + } + ?> -- 2.17.1