PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How to add a category programmatically to a WooCommerce product?

 May 09, 2022     php, product, taxonomy-terms, woocommerce, wordpress     No comments   

Issue

In WooCommerce, if an item is sold out, I would like to add a category "sold out" to it, so that I can show these items in a separate section.

I created the "sold out" category in the WooCommerce/WordPress Admin panel.

This is what I have in my functions.php:

add_action( 'woocommerce_before_shop_loop_item_title', 'mytheme_display_sold_out_loop_woocommerce' );
 
function mytheme_display_sold_out_loop_woocommerce() {
    global $product;
 
    if ( !$product->is_in_stock() ) {
       // add category "sold out"   
    }
}

Now, how can I automatically add the category "sold out" to any sold out WooCommcerce product?


Solution

Assuming that the product category term name "sold out" exists, try the following:

add_action( 'woocommerce_before_shop_loop_item_title', 'mytheme_display_sold_out_loop_woocommerce' );
 
function mytheme_display_sold_out_loop_woocommerce() {
    global $product;

    $term_name = 'Sold out';

    // Get the product category term Id for "Sold out" term name
    $term_id   = get_term_by( 'name', $term_name, 'product_cat' )->term_id;
 
    // 1. Add product category "Sold out"
    if ( ! $product->is_in_stock() && ! has_term( $term_id, 'product_cat', $product->get_id() ) ) {
       // Get product categories (if there is any)
       $term_ids = (array) $product->get_category_ids();

       // Add the product category term Id for "Sold out" term name to $term_ids array
       $term_ids[] = $term_id;

       $product->set_category_ids( $term_ids ); // Update product categories
       $product->save(); // Save to database
    } 
    // 2. Remove product category "Sold out"
    elseif ( $product->is_in_stock() && has_term( $term_id, 'product_cat', $product->get_id() ) ) {
       // Get product categories (if there is any)
       $term_ids = (array) $product->get_category_ids();

       // Remove the product category term Id for "Sold out" term name in $term_ids array
       if ( ( $key = array_search( $term_id, $term_ids ) ) !== false ) {
           unset($term_ids[$key]);
       }

       $product->set_category_ids( $term_ids ); // Update product categories
       $product->save(); // Save to database
    }
}

Code goes in functions.php file of your active child theme (or active theme). It should work.


Now it should be lighter to use the term Id for "sold out" product category, so this way you could replace (in the code):

$term_name = 'Sold out';

// Get the product category term Id for "Sold out" term name
$term_id   = get_term_by( 'name', $term_name, 'product_cat' )->term_id;

by only the term id for 'Sold out' product category term name like (replace 19 by the real term Id):

$term_id = 19; 


Answered By - LoicTheAztec
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing