简体   繁体   English

如何以编程方式向 WooCommerce 产品添加类别?

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

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.WooCommerce ,如果某个商品售罄,我想为其添加一个“售罄”类别,以便我可以在单独的部分中显示这些商品。

I created the "sold out" category in the WooCommerce / WordPress Admin panel.我在WooCommerce / WordPress管理面板中创建了“售罄”类别。

This is what I have in my functions.php :这是我在我的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?现在,我如何自动将“售罄”类别添加到任何售罄的WooCommcerce产品中?

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).代码位于活动子主题(或活动主题)的 functions.php 文件中。 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) :现在,将术语 Id 用于“售罄”产品类别应该更轻松,因此您可以通过这种方式替换(在代码中)

$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) :仅通过“售罄”产品类别术语名称的术语 id (用实际术语 Id 替换19

$term_id = 19; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM