我在我的 WooCommerce 网站中创建了“新到货”产品类别部分。我希望分配给新类别的产品将隐藏或自动从“新到货”类别中删除,并且如果分配了一个或多个类别,则在其他类别中保持可见。
详细说明:
示例: 商品“红色T恤”被指定为“新品到货”和“促销”类别,那么15天后,该商品将被隐藏或从“新品到货部分”中删除,但在“新品到货部分”中仍然可见“销售”类别。
这是迄今为止我的代码:
// Add a custom action to schedule a job when a product is published
add_action('woocommerce_new_product', 'schedule_product_visibility_change', 10, 2);
function schedule_product_visibility_change($product_id, $product)
{
// Schedule the job to run after 15 days
wp_schedule_single_event(time() + 15 * DAY_IN_SECONDS, 'auto_hide_product_from_new_arrival', array($product_id));
}
// Hook to run when the scheduled job is triggered
add_action('auto_hide_product_from_new_arrival', 'auto_hide_product_from_new_arrival_callback');
function auto_hide_product_from_new_arrival_callback($product_id)
{
// Get product categories
$categories = wp_get_post_terms($product_id, 'product_cat', array('fields’ => ‘ids'));
// Check if the product is in the "New Arrival" category
if (in_array('new-arrival-category-id', $categories)) {
// Update product visibility
wp_set_object_terms($product_id, 'exclude-from-catalog', 'visibility', true);
}
}
// Restore product visibility if it’s removed from the “New Arrival” category before 15 days
add_action('woocommerce_remove_product_cat', 'restore_product_visibility_on_category_removal', 10, 2);
function restore_product_visibility_on_category_removal($product_id, $category)
{
// Get product categories
$categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));
// Check if the product was in the "New Arrival" category
if ($category->term_id === 'new-arrival-category-id' && !in_array('new-arrival-category-id', $categories)) {
// Restore product visibility
wp_remove_object_terms($product_id, 'exclude-from-catalog', 'visibility');
}
}
但是我的代码不完整并且不起作用。任何帮助将不胜感激。
你让事情变得毫无意义,而且你的代码中有多个错误......你只需要在15天后安排“新品到货”类别取消分配,这样产品就不会再在“新品到货”类别中可见,但是将在分配给它的其他类别中保持可见。
尝试以下操作:
// Add a custom action to schedule a job when a product is published
add_action( 'woocommerce_new_product', 'schedule_unassign_product_category', 10, 2 );
function schedule_unassign_product_category( $product_id, $product ) {
// Schedule the job to run after 15 days
wp_schedule_single_event(time() + 15 * DAY_IN_SECONDS, 'unassign_new_arrival_product_category', array($product_id));
}
// Hook to run when the scheduled job is triggered
add_action( 'unassign_new_arrival_product_category', 'unassign_new_arrival_product_category_callback', 10, 1 );
function unassign_new_arrival_product_category_callback( $product_id )
{
$taxonomy = 'product_cat';
$terms = wp_get_post_terms($product_id, $taxonomy); // Get product categories
// Loop through assigned product categories
foreach( $terms as $key => $term ) {
// Check if the product is in the "New Arrival" category
if ( 'new-arrival' === $term->slug ) {
unset($terms[$key]); // Remove 'new-arrival' category from the array
if ( ! empty($terms) ) {
wp_set_object_terms($product_id, $terms, $taxonomy); // Update product assigned categories
}
break;
}
}
}
代码位于子主题的functions.php 文件中(或插件中)。应该可以。
还是不行
我也尝试过这段代码,请检查并建议该怎么做。
我还尝试按产品发布日期和产品修改日期获取产品,并将其分配给“最近”产品类别,但未收到所需的输出。
我只想将产品保留在发布日期后 15 天内 f
// Add a custom column to display recent products in the category admin page
function add_recent_products_column($columns) {
$columns['recent_products'] = 'Recent Products';
return $columns;
}
add_filter('manage_edit-product_cat_columns', 'add_recent_products_column');
// Populate the custom column with recent products and assign to a specific category
function display_recent_products_column($content, $column, $term_id) {
if ('recent_products' === $column) {
$category_slug = get_term($term_id, 'product_cat')->slug;
// Query recent products in the specified category
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category_slug,
),
),
'post_status' => 'publish', // Consider only published products
);
$recentProducts = new WP_Query($args);
// Output recent product count in the category admin page
echo '<span>' . $recentProducts->found_posts . ' recent products</span>';
// Assign recent products to a specific category
$new_category_id = get_term_by('slug', 'recent', 'product_cat');
if ($new_category_id) {
foreach ($recentProducts->posts as $recentProduct) {
wp_set_post_terms($recentProduct->ID, $new_category_id->term_id, 'product_cat', true);
}
}
wp_reset_postdata();
}
}
add_action('manage_product_cat_custom_column', 'display_recent_products_column', 10, 3);
// Schedule daily event to update recent category
function schedule_daily_event() {
if (!wp_next_scheduled('daily_event_hook')) {
wp_schedule_event(time(), 'daily', 'daily_event_hook');
}
}
add_action('wp', 'schedule_daily_event');
// Callback function for the daily event
function update_recent_category_daily() {
// Query all products
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
);
$allProducts = new WP_Query($args);
// Get the 'recent' category ID
$recent_category_id = get_term_by('slug', 'recent', 'product_cat')->term_id;
// Iterate through each product
foreach ($allProducts->posts as $product) {
// Get the publish date
$post_date = get_the_date('Y-m-d', $product->ID);
// Get the modified date
$post_modified = get_the_modified_date('Y-m-d', $product->ID);
// Get the date 15 days ago
$date_15_days_ago = date('Y-m-d', strtotime('-15 days'));
// Check if the product should be included or excluded from the 'recent' category
if ($post_date >= $date_15_days_ago || $post_modified >= $date_15_days_ago) {
// Include in the 'recent' category
wp_set_post_terms($product->ID, $recent_category_id, 'product_cat', true);
} else {
// Exclude from the 'recent' category
wp_remove_object_terms($product->ID, $recent_category_id, 'product_cat');
}
}
wp_reset_postdata();
}
add_action('daily_event_hook', 'update_recent_category_daily');
// Callback function for updating on publish_post hook
function update_recent_category_on_publish($ID, $post) {
// Check if the post is a product
if ($post->post_type === 'product') {
// Trigger the daily event to update the 'recent' category
do_action('daily_event_hook');
}
}
add_action('publish_post', 'update_recent_category_on_publish', 10, 2);