php 当拍卖时间结束时添加产品类别并从Woocommerce产品中删除所有类别

k4ymrczo  于 2023-01-12  发布在  PHP
关注(0)|答案(1)|浏览(99)

我想添加一个产品猫到我的Woocommerce产品时,时间是0,这样我就可以在不同的部分显示这些项目。
我做了类别“拍卖结束”,我想添加到产品。
代码可以工作,但我仍然在一个不应该出现的地方显示产品。
我想我需要删除所有当前类别之前,然后只有“拍卖结束”类别。

function auction_end_date_countdown() {

        global $product;
        
        $product      = apply_filters( 'yith_wcact_get_auction_product', $product );

        ob_start();

        if ( 'auction' === $product->get_type() ) { 

                                    $dateclose = $product->get_end_date();
                                    
                                    if ( $dateclose ) {
                                    
                                        $today = date("Y-m-d H:i:s");                                   
                                        $format_date = get_option( 'yith_wcact_general_date_format', 'Y-m-d H:i:s' );
                                        
                                        $format = $format_date . ' ' . $format_time;
                                        
                                        $date = ( date( 'Y-m-d H:i:s', $dateclose ) );
                                                                            
                                        $diff = abs(strtotime($today) - strtotime($date));


                                        if ($today < $date) {
                                            echo $date;
                                        }else{
                                            echo $today;
                                            
                                            
                                                global $product;

                                                $term_name = 'Auction ended';

                                                // Get the product category term Id for "Sold out" term name
                                                $term_id   = get_term_by( 'name', $term_name, 'product_cat' )->term_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
                                                 
                                            
                                            }
                                        
                                                    
                                        
                                        
                                    }
                                     

        }

        $html = ob_get_clean();
        return $html;

        
}
add_shortcode( 'veiling_eind_datum_aftellen', 'auction_end_date_countdown' );
jutyujz0

jutyujz01#

要添加产品类别“拍卖结束”,并不显示在某些旋转木马/名册中,我需要删除所有类别,然后添加我想添加的类别。(我的情况)

global $product;
                                            
                                                $product->set_category_ids( array() ); // Remove all categories from product
                                                $term_name = 'Afgelopen veiling';

                                                // Get the product category term Id for "" term name
                                                $term_id   = get_term_by( 'name', $term_name, 'product_cat' )->term_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

相关问题