wordpress-seo
wordpress-seo copied to clipboard
`%%primary_category%%` template variable doesn't work for WooCommerce products
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?
- 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
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 );
Related: #5312
Please inform the customer of conversation # 211084 when this conversation has been closed.
I hope this will be followed through. A much needed feature to make this SEO plugin meaningful for an ecommerce site.
Please inform the customer of conversation # 218758 when this conversation has been closed.
Please inform the customer of conversation # 221968 when this conversation has been closed.
Still not worked on that?
Can I help to make that happen?
Any progress on adding this feature? Please, would love to have it!
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?
Same problem here! Is it not possible to select just the primary category?
Please inform the customer of conversation # 392714 when this conversation has been closed.
Please inform the customer of conversation # 409531 when this conversation has been closed.
Please inform the customer of conversation # 409668 when this conversation has been closed.
Please inform the customer of conversation # 417134 when this conversation has been closed.
Please inform the customer of conversation # 433256 when this conversation has been closed.
Related: https://github.com/Yoast/wordpress-seo/issues/5921
Please inform the customer of conversation # 479991 when this conversation has been closed.
Please inform the customer of conversation # 494277 when this conversation has been closed.
Please inform the customer of conversation # 494554 when this conversation has been closed.
Please inform the customer of conversation # 530391 when this conversation has been closed.
+1 for https://wordpress.org/support/topic/issues-with-ct_product_cat-and-primary_category/
Please inform the customer of conversation # 739346 when this conversation has been closed.
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
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
Please inform the customer of conversation # 925067 when this conversation has been closed.