Is it possible to get Yoast primary category from product ID alone - woocommerce

Is it possible get Yoast Primary Category from Product ID ($product->get_id()) alone?
I've tried
$primary_term_id = yoast_get_primary_term_id( 'product_cat', $product->get_id() );
but to no avail.

Related

Add to category but keep current primary category using wp_set_object_terms

I'm using wp_set_object_terms to add product to a category. But the newly added category gets set as primary category which I don't want – this also means that the breadcrumb is changed to contain the new category instead of the old primary category.
My code is like:
wp_set_object_terms( $product_id, $new_category_id, 'product_cat', true);
I did try to remove and re-add the old primary category which I wanted to keep for the breadcrumb:
wp_set_object_terms( $product_id, $new_category_id, 'product_cat', true);
wp_remove_object_terms( $product_id, $old_primary_cat, 'product_cat' );
wp_set_object_terms( $product_id, $old_primary_cat, 'product_cat', true);
But that did not work
How do I add a category but without changing the primary category?
Thanks!

In-store only products WooCommerce (WordPress)

I'm using WordPress with WooCommerce to sell physical products to customers online.
I would like to have either specific products or rather a specific category of products as "available in-store only" thus not displaying the "add to cart" button for those products, forcing the customers to get in touch with me or to physically come into my store to buy those products.
Some of these products are using variations (colors, options, etc) with different prices so I need to keep those products as "variable products".
Would love to have ideas / solutions to this as I can't find any. Thanks!
You can check for category slug or id, and filter woocommerce_is_purchasable like so:
function BN_restrict_store_only( $purchasable, $product ){
//The category by slug
$pickup_prod_cat_slug = 'available-in-store-only'; // slug
//The category by id
$pickup_prod_cat_ids ='681'; // the id of the product cat
// For variations (
if ( $product->is_type('variation') ) {
$parent = wc_get_product( $product->get_parent_id() );
$product_id = $parent->get_id();
if ( has_term ( array( $pickup_prod_cat_slug, $pickup_prod_cat_ids), 'product_cat', $product_id )) {
$purchasable = false;
}
}
// For simple and other product types
else {
$product_id = $product->get_id();
if ( has_term ( array( $pickup_prod_cat_slug, $pickup_prod_cat_ids), 'product_cat', $product_id )) {
$purchasable = false;
}
}
return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'BN_restrict_store_only', 10, 2 );
add_filter( 'woocommerce_variation_is_purchasable', 'BN_restrict_store_only', 10, 2 );
Works both with simple products and variations.
You have to make a category with the slug "available-in-store-only" or get the id/slug of whatever product cat you want to use.
File goes in the functions.php of your child theme, or CodeSnippets.

Woocommerce product category filter dosn't update ACF

There is custom filed created to display second place for Seo text just under products on product category archive pages.
When entering category from menu bar - the different description in ACF correctly changes according to chosen category.
The problem is - WHEN - we are in any of these product categories and want to use product category filter from the side bar. Then nothing is changing. Category changes but description from ACF stays from previous category. That should work like this, we ar in A category, but want to filter products with B category filter, after applying that filter there should be updated page with b category products and updated ACF Seo description. How can we resolve that?
My code:
add_action( 'woocommerce_after_main_content', 'my_extra_description' );
function my_extra_description() {
global $wp_query;
apply_filters( 'acf/update_value', $value, $post_id, $field, $original );
# get the query object
$category = $wp_query->get_queried_object();
#get the cat ID
$category_ID = $category->term_id;
#get the field
$contenu_categorie = get_field( 'contenu_categorie', 'category_'.$category_ID );
#if we have data, show it
if( $contenu_categorie ){
echo $contenu_categorie;
}
}
Plase help :)

Wordpress: How do I remove the category "uncategorized" from all posts in bulk?

Scenario:
I have 1000 posts that have the "Uncategorized" category, and I want to remove "Uncategorized" from all of those posts and set a different category for those posts.
In other words– take all Uncategorized posts and move them to another category, in one fell swoop.
Can I do this in bulk without going through each post individually?
What you are looking for is the WordPress bulk editor.
Go to Posts > Categories > Uncategorized
Click the "Screen Options" tab in the top right corner, then change "Number of items per page:" to 1000. (If you are on a really slow server you might consider doing less at a time)
Now select all of the items on the page and click the "Bulk Actions" drop-down above the select all and select the "Edit” option.
Hit Apply
In the bulk editor click the “new category” you want to change all of the posts to and hit update.
Once you have added all of the posts to the “new category” you need to remove the “Uncategorized” category. To do this:
Go to Settings > Writing
Now change the “Default Post Category” to something besides “Uncategorized”
Go back to Posts > Categories and delete the “Uncategorized” category
Now you can create the “Uncategorized” category again if you like and change it back to the default.
Once you delete the “Uncategorized” category it will remove it from all of your posts.
If you have some posts that you want to remain in “Uncategorized” then create a new category called “temp” and assign all of the posts you want to remain to that category. Once you delete “Uncategorized” create it again and assign the posts in “temp” back to that category.
As you've discovered, the bulk editor only allows the ADDITION of categories to multiple posts - it's not possible to REMOVE categories from multiple posts. The best option i found was to temporarily install this plug-in https://wordpress.org/plugins/bulk-remove-posts-from-category/ (in the WP repository) which adds the ability to REMOVE categories from multiple posts using the same bulk edit method. it simply adds an additional 'remove' checkbox under the category list.
The uncategorized category has an ID of 1. What our worksflow will be is,
Get all posts which is assigned to the uncategorized category.
We will only get post ID's which will make our query up to 1000 times faster on a site with thousands of posts. This will also help that our query does not time out or hit a maximum memory fatal error.
Use wp_set_object_terms() to remove and set our new tems
NOTE:
The code below requires PHP 5.4+ and any changes will be non reversable, so back up your database first
$args = [
'nopaging' => true, // Gets all posts
'cat' => 1, // Only gets posts assigned to category 1 which is the uncategorized category
'fields' => 'ids', // Only get post ID's, make query up 1000 times faster on huge databases
];
$q = get_posts( $args );
if ( $q ) {
foreach ( $q as $v ) {
// Get all the post categories
$categories = get_the_category( $v );
$category_ids = [];
foreach ( $categories as $category ) {
// Replace all uncategorized category instances with our new category id and build new array
if ( $category->term_id == 1 ) {
$category_ids[] = (int) 21; // REPLACE WITH THE CORRECT NEW CATEGORY ID
} else {
$category_ids[] = (int) $category->term_id;
}
}
// Set our new categories to the post
if ( $category_ids ) // Unnecessary check for categories, but just in case should something fail
wp_set_object_terms( $v, $category_ids, 'category' );
}
}
Note, nowhere have we changed the $post global or setup postdata, so we don't need to call wp_reset_postdata() :-)
Wordpress stores the parent/child relationships between categories and posts in the wp_term_relationships table, which is documented here. As #Pieter Goosen noted, the "Uncategoried" category has a ID of 1. So you can backup your SQL database, then connect to it with a SQL command line client (sudo mysql wordpress works for me), and run this SQL command:
delete from wp_term_relationships where term_taxonomy_id = 1;
If you want to move all uncategorized posts to another category, you can use the Bulk Move plugin.
If you want to remove a category from some posts, Bulk remove posts from category might be a better option.
As Melebius said, it is much, much quicker (and easier) to use https://wordpress.org/plugins/bulk-remove-posts-from-category/ ! It works with products as well as posts. Brilliant!
You can add this to your theme's functions.php file, then refresh any page on your site once, then remove the function.
Use at your own risk and backup the database first! There's no UNDO button on this.
<?php
$args = array(
'posts_per_page' => -1
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) :
setup_postdata( $post );
$categories = get_the_category();
$catcount = count($categories);
$postid = $post->ID;
$catlist = array();
//Building a list of categories for each post and EXCLUDING "uncategorized"
foreach( $categories as $category ) {
if($category->name == 'Uncategorized') {
continue;
}
$catlist[] = $category->term_id;
}
// If there's just one category, and that category is "Uncategorized", move the category to one of your choosing
if($catcount == 1 && $categories[0]->name == "Uncategorized") {
// This is the category ID that you want to move uncategorized posts to
$catlist = array(189);
}
wp_set_object_terms( $postid, $catlist, 'category' );
endforeach;
wp_reset_postdata();
?>

Retrive WooCommerce Category name by WooCommerce Category ID

I have been searching and found a function that will display all WooCommerce product categories, but I am looking for a function that will retrieve single product category name by giving product category ID. Raw product category name, no url, no thumbnail, nothing.
I need to use it in my template file.
For instance:
Category Name for 'Bags' has id '15'
I need something like this:
showCategoryName(15);
RESULT: Bags
Thanks
You can use WP native functions like get_terms:
function woocommerceCategoryName($id){
$term = get_term( $id, 'product_cat' );
return $term->name;
}

Resources