使用 PHP 从 Woocommerce 中的产品中删除标签

问题描述 投票:0回答:2

我想从 WooCommerce 中的产品中删除标签

function prefix_show_is_products() {
    $products = wc_get_products(array(
        'tag' => array('weekly')
    ));

// CODE TO REMOVE 'weekly' from the products

}

谢谢您的帮助!

wordpress woocommerce tags product
2个回答
0
投票

您应该使用https://developer.wordpress.org/reference/functions/wp_remove_object_terms/

wp_remove_object_terms($product->get_id(), 'weekly', 'product_tag');

0
投票
add_action('woocommerce_before_single_product', 'remove_all_product_tags_on_single_product');

function remove_all_product_tags_on_single_product() {
    // Checking if the current page is a product page
    if (is_product()) {
        // We receive the current product
        global $product;

        // Checking whether the product is an object of the WC_Product class
        if ($product instanceof WC_Product) {
            // We get the product ID
            $product_id = $product->get_id();

            // Remove all product tags
            wp_remove_object_terms($product_id, get_terms('product_tag', array('fields' => 'ids')), 'product_tag');
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.