Woocommerce - remove product from category

user3406217

I'm trying to do the following, but I'm stuck with coding. Hope someone can help me out here.

In Woocommerce (on the product-edit page) I can choose in which category the product will be. I made a code where the product will be put in an extra category 'Aanbiedingen' (id=87) upon save, when there are certain conditions.

if ( !empty ($_POST['sale_enddate']) && ($_POST['sale_begindate']) ) {
    $cat_ids = array( 87 );
    wp_set_object_terms( $product_id, $cat_ids, 'product_cat', true );
    }   

Now I would like to have a code where (upon save) the product will be removed from the 'Aanbiedingen' category, but it will still be in other categories.

Example: The product is in category 'Aanbiedingen', 'Comfort', 'Therapie' and 'Warmtezakken' Can someone help me with a code where (upon save) the product will only be in 'Comfort', 'Therapie' and 'Warmtezakken' ?

I guess it has something todo with getting categories with wp_get_object_terms , removing value 'Aanbiedingen' (or id 87) and saving this altered array back with wp_set_object_terms?

I tried several things, but I can't get it done. Can someone please help me?

doublesharp

You just need to use the wp_remove_object_terms($post_id, $terms, $taxonomy) function to remove the specific category term ID from your object (in this case a Product custom post type).

As a side note, you also want to use isset() to check for the $_POST variable, instead of !empty() - you will get a warning using the latter method if the value isn't in the POST'd values

// category ID array used for both adding & removing
$cat_ids = array( 87 );

if ( isset( $_POST['sale_enddate'] ) && $_POST['sale_begindate'] ) {
    // conditions met for adding product to category
    wp_set_object_terms( $product_id, $cat_ids, 'product_cat', true );
} else {
    // otherwise remove it
    wp_remove_object_terms( $product_id, $cat_ids, 'product_cat' );
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

WooCommerce - Remove product title from breadcrumbs but keep all category hyperlinks

How to get product category ids from order items in WooCommerce

Exclude specific product ids from a category price suffix change in WooCommerce

Get a list of siblings term ids from current product category in WooCommerce

Remove specific product variation from cart in WooCommerce

Remove add cart button in Woocommerce for a specific product category

Get "primary" category image from WooCommerce product

Exclude a product category from custom displayed button on Woocommerce archives

Display products from specific product category in Woocommerce

Redirect when buying products from a certain product category in Woocommerce

Remove first word from the WooCommerce product title

Exclude a product category from the loop in Woocommerce

Remove <p> tags from product attributes in WooCommerce

Remove product category placeholder image in Woocommerce

Hide “remove item” from cart for WooCommerce product category

Check a product category for a product in Woocommerce

Exclude a product category from Woocommerce related products

Exclude a product category from Related products in WooCommerce

Remove header image from WooCommerce product page

If product category has children remove permalink from parent term in WooCommerce

Get the category name from a WooCommerce product using the product id or the category id

Woocommerce: Product Category Widget

Magento remove product from category programmatically

Woocommerce Product child category from current parent category

WooCommerce remove product from loop by id

Remove product category titles from shop page woocommerce

How to remove Price from Product Category page?

Remove "No products found" from a specific product category in Woocommerce

Remove other products if certain product category is present on WooCommerce checkout