wordpress-seo icon indicating copy to clipboard operation
wordpress-seo copied to clipboard

`%%primary_category%%` template variable doesn't work for WooCommerce products

Open mmikhan opened this issue 7 years ago • 25 comments

Please give us a description of what happened.

When using the %%primary_category%% template variable for the WooCommerce products, it doesn't output anything on the SEO title field.

Please describe what you expected to happen.

The %%primary_category%% template variable should output the primary category of WooCommerce products.

How can we reproduce this behavior?

  1. Use the %%primary_category%% template variable on any WooCommerce products SEO title field

Technical info

  • WordPress version: 4.8
  • Yoast SEO version: 5.0.1

mmikhan avatar Jul 07 '17 12:07 mmikhan

Woocommerce products use product categories and product tags. They are custom taxonomies (product_cat and product_tag) and they aren't default categories/tags.

Function retrieve_primary_category gets primary_category for category.

I already tried to make something similar, but it's better to introduce new variable %%primary_product_cat%% because it's related to different taxonomy, something like this:

        private function retrieve_primary_product_cat() {
                $primary_product_cat = null;

                if ( ! empty( $this->args->ID ) ) {
                        $wpseo_primary_category = new WPSEO_Primary_Term( 'product_cat', $this->args->ID );

stodorovic avatar Jul 08 '17 08:07 stodorovic

Related: #5312

stodorovic avatar Jul 09 '17 07:07 stodorovic

Please inform the customer of conversation # 211084 when this conversation has been closed.

Pcosta88 avatar Jul 26 '17 19:07 Pcosta88

I hope this will be followed through. A much needed feature to make this SEO plugin meaningful for an ecommerce site.

bukerfly avatar Aug 24 '17 11:08 bukerfly

Please inform the customer of conversation # 218758 when this conversation has been closed.

suascat avatar Aug 30 '17 09:08 suascat

Please inform the customer of conversation # 221968 when this conversation has been closed.

amboutwe avatar Sep 05 '17 01:09 amboutwe

Still not worked on that?

Can I help to make that happen?

michael-voit avatar Dec 15 '17 13:12 michael-voit

Any progress on adding this feature? Please, would love to have it!

marcusode avatar Jan 08 '18 07:01 marcusode

Many people have products in more than one product category, it's impossible to use %%ct_product_cat%% as the Titles will be so long! When do you think can we get the primary Woocommerce category as part of the variables? I feel like this plugin addresses everything except this - it's a real shame! Any suggestions on how to get around this?

Shar7 avatar Jan 18 '18 22:01 Shar7

Same problem here! Is it not possible to select just the primary category?

Chef-Panda avatar Mar 06 '18 07:03 Chef-Panda

Please inform the customer of conversation # 392714 when this conversation has been closed.

rmarcano avatar Jun 28 '18 17:06 rmarcano

Please inform the customer of conversation # 409531 when this conversation has been closed.

mariusghitulescu avatar Jul 26 '18 08:07 mariusghitulescu

Please inform the customer of conversation # 409668 when this conversation has been closed.

mayada-ibrahim avatar Jul 26 '18 14:07 mayada-ibrahim

Please inform the customer of conversation # 417134 when this conversation has been closed.

Pcosta88 avatar Aug 20 '18 14:08 Pcosta88

Please inform the customer of conversation # 433256 when this conversation has been closed.

rmarcano avatar Oct 12 '18 12:10 rmarcano

Related: https://github.com/Yoast/wordpress-seo/issues/5921

monbauza avatar Oct 29 '18 11:10 monbauza

Please inform the customer of conversation # 479991 when this conversation has been closed.

suascat avatar Mar 04 '19 06:03 suascat

Please inform the customer of conversation # 494277 when this conversation has been closed.

mmikhan avatar Apr 13 '19 19:04 mmikhan

Please inform the customer of conversation # 494554 when this conversation has been closed.

mayada-ibrahim avatar Apr 19 '19 16:04 mayada-ibrahim

Please inform the customer of conversation # 530391 when this conversation has been closed.

mmikhan avatar Aug 06 '19 08:08 mmikhan

+1 for https://wordpress.org/support/topic/issues-with-ct_product_cat-and-primary_category/

mmikhan avatar Dec 19 '20 04:12 mmikhan

Please inform the customer of conversation # 739346 when this conversation has been closed.

suascat avatar May 28 '21 08:05 suascat

Is it safe to say that almost 4 years after this BUG was first opened that this issue is likely never going to be fixed?

It's a shame since this is a pretty basic expectation (especially for those of us that have paid for Yoast

mjdotmom avatar May 28 '21 20:05 mjdotmom

Alright guys (& ladies), I created my own snippet variable and after hours banging my head on my desktop it works ! My code is probably ugly as I'm a noob... Here it is :

//define the custom replacement callback
function get_yoast_primary_category() {
  // Show Yoast primary category OR YEAR
  $category = get_the_terms( $post_id, 'product_cat' );
  // If post has a category assigned.
  if ($category) {
    $category_display = '';
    $taxonomy = 'product_cat';
    $primary_cat_id = yoast_get_primary_term_id( 'product_cat', $product_id );
    if ( $primary_cat_id ) {
      $primary_cat = get_term($primary_cat_id, $taxonomy);
      if(isset($primary_cat->name))
        $category_display = $primary_cat->name;
    }
    if ( !empty($category_display) ) {
	    return $category_display;
	  } else {
      return date("Y");
    }
  } else { return date("Y"); }
}
// define the action for register yoast_variable replacments
function register_custom_yoast_snippet_variable() {
    wpseo_register_var_replacement( '%%main_category_woocommerce%%', 'get_yoast_primary_category', 'advanced', 'woocommerce primary category' );
}
// Add action
add_action('wpseo_register_extra_replacements', 'register_custom_yoast_snippet_variable');

use %%main_category_woocommerce%% it will not work in the Yoast preview of your product page, however it does if you look at your source code

Feel free to make it pretty ! Even though that works (for me at least...) Yoast still needs to correct this problem...

I'd like to thank my long-time sponsors : Google & Stack Overflow amazing community

memyselfandob avatar Jun 02 '21 14:06 memyselfandob

Please inform the customer of conversation # 925067 when this conversation has been closed.

rohans456 avatar Aug 04 '22 13:08 rohans456