简体   繁体   English

如果所有变体都缺货,则从目录中隐藏 WooCommerce 可变产品

[英]Hide WooCommerce variable product from catalog if all variations are out of stock

Within my WooCommerce shop, there are products with variations in color.在我的 WooCommerce 商店中,有颜色变化的产品。 The update on the stock is on variations level because the external feed sees every color as a different product.库存的更新处于变化级别,因为外部提要将每种颜色视为不同的产品。

Now I've got a issue where all the variations of a variable product are out of stock , but the product itself still show up in the catalog of the Webshop.现在我遇到了一个问题,变量产品的所有变体都缺货,但产品本身仍然显示在网上商店的目录中。 Even tho I turn on the setting "Hide out of stock items from the catalog".即使我打开了“从目录中隐藏缺货商品”设置。 But this setting only works for products without variations.但此设置仅适用于没有变化的产品。

When you click on a product with variations that are all out of stock it shows: "This product is currently out of stock and unavailable.".当您单击一个变体全部缺货的产品时,它会显示:“该产品目前缺货且不可用。”。

Is there a setting in WooCommerce that can also hide those products in my catalog? WooCommerce 中是否有设置也可以将这些产品隐藏在我的目录中? Or do I've to make an extra script that will check every variable product and put it out of stock at its highest level?或者我是否必须制作一个额外的脚本来检查每个可变产品并使其最高级别缺货?

The code that I tried on a variable product with no effect:我在变量产品上尝试的代码无效:

$out_of_stock_staus = 'outofstock';

// 1. Updating the stock quantity
update_post_meta($product_id, '_stock', 0);

// 2. Updating the stock status
update_post_meta( $product_id, '_stock_status', wc_clean( $out_of_stock_staus ) );

// 3. Updating post term product visibility
wp_set_post_terms( $product_id, $out_of_stock_staus, 'product_visibility', true ); 

How can I hide WooCommerce variable product from catalog if all variations are out of stock?如果所有变体都缺货,如何从目录中隐藏 WooCommerce 可变产品?

Note: Since WooCommerce 3 and 4, it's better to use WC_Product available methods as there are new custom tables and cached data to update.注意:由于 WooCommerce 3 和 4,最好使用WC_Product 可用方法,因为有新的自定义表和缓存数据要更新。

There is a way to show or hide variable products from product catalog (a bit complex) .有一种方法可以显示或隐藏产品目录中的可变产品(有点复杂)

1). 1)。 A lightweight conditional function to check if a variable product has all it's product variations "out of stock":一个轻量级的条件 function检查变量产品是否具有“缺货”的所有产品变体:

// Conditional function to check if a variable product has at least a variation in stock
function is_wc_variable_product_in_stock( $product_id ){
    global $wpdb;

    $count = $wpdb->get_var( $wpdb->prepare( "
        SELECT COUNT(ID)
        FROM {$wpdb->posts} p
        INNER JOIN {$wpdb->postmeta} pm
            ON p.ID           =  pm.post_id
        WHERE p.post_type     =  'product_variation'
            AND p.post_status =  'publish'
            AND p.post_parent =  %d
            AND pm.meta_key   =  '_stock_status'
            AND pm.meta_value != 'outofstock'
    ", $product_id ) );

    return $count > 0 ? true : false;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 This function is needed for the following, so keep it.下面需要这个function,所以请保留。


2). 2)。 A custom function that will check for all your variable products (and all their variations stock) adding custom meta data (This function will be used just once and then removed).自定义 function将检查所有可变产品(及其所有变体库存)添加自定义元数据(此 function 将只使用一次,然后删除)。

// Function that will check the variations stock for each variable products adding custom meta data
function check_and_update_all_variable_products(){
    // Only for admins
    if( ! current_user_can('edit_products')) return;

    // get all variable product Ids
    $variable_products_ids = wc_get_products( array(
        'limit'  => -1,
        'status' => 'publish',
        'type'   => 'variable',
        'return' => 'ids',
    ));

    // Loop through variable products
    foreach( $variable_products_ids as $variable_id ) {
        // Check if all the product variations are "out of stock" or not
        $value      = is_wc_variable_product_in_stock( $variable_id ) ? '0' : '1';

        $meta_value = (string) get_post_meta( $variable_id, '_all_variations_outofstock', true );

        if ( $value !== $meta_value ) {
            // Create/Udpate custom meta data
            update_post_meta( $variable_id, '_all_variations_outofstock', $value );
        }
    }
}
// Run the function: browse any page of your web site
check_and_update_all_variable_products();

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Once saved, brownse any page of your we site, to run this function.保存后,浏览我们网站的任何页面,以运行此 function。 Then remove it afterwards.然后将其删除。


3). 3)。 Now a hooked function that will hide your variable products which variations are all "out of stock", from WooCommerce catalog and from single product pages:现在一个挂钩的 function将隐藏您的可变产品,这些产品的变体都是“缺货”,来自 WooCommerce 目录和单个产品页面:

// Hide product variations which variations are all "out of stock"
add_action( 'woocommerce_product_query', 'hide_variable_products_with_all_outofstock_variations', 10, 2 );
function hide_variable_products_with_all_outofstock_variations( $q, $query ) {
    // Get any existing meta query
    $meta_query = $q->get( 'meta_query');

    // Define an additional meta query
    $meta_query['relation'] = 'OR';
    $meta_query[] = array(
        'key'     => '_all_variations_outofstock',
        'value'   => '1',
        'compare' => '!=',
    );
    $meta_query[] = array(
        'key'     => '_all_variations_outofstock',
        'compare' => 'NOT EXISTS',
    );

    // Set the new merged meta query
    $q->set( 'meta_query', $meta_query );
}

// Hide "Out of stock" variable product single pages
add_action( 'template_redirect', 'hide_out_of_stock_variable_product_single_pages' );
function hide_out_of_stock_variable_product_single_pages(){
    if( get_post_meta( get_the_id(), '_all_variations_outofstock', true ) ) {
        // Redirect to Shop page
        wp_redirect( wc_get_page_permalink( 'shop' ) );
        exit();
    }
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 tested and works.测试和工作。


4). 4)。 Add / Update product variation stock custom meta: On admin single product pages, check variable product stock status and update custom meta data (and also when a paid order update the product stock) :添加/更新产品变体库存自定义元:在管理单个产品页面上,检查可变产品库存状态并更新自定义元数据(以及当支付订单更新产品库存时)

// Custom function that update the parent variable product stock custom meta data
function update_parent_variable_product_stock_custom_meta( $product_var ) {
    if( ! is_a($product_var, 'WC_Product') ) {
        $product = wc_get_product( $product_var );
    } else { $product = $product_var; }

    if ( $product->is_type('variation') ) {
        // Get the parent product id from product variation
        $product_id = (int) $product->get_parent_id();
    } elseif ( $product->is_type('variable') ) {
        $product_id = (int) $product->get_id();
    } else {
        return; // Exit
    }

    // Check if all the product variations are "out of stock" or not
    $value = is_wc_variable_product_in_stock( $product_id ) ? '0' : '1';

    // add/Udpate custom meta data
    update_post_meta( $variable_id, '_all_variations_outofstock', $value );
}

// Update variable product stock custom meta data on admin single product pages
add_action( 'woocommerce_process_product_meta_variable', 'update_variable_product_stock_custom_meta_data' );
function update_product_variable_stock_custom_meta_data ( $product_id ) {
    update_parent_variable_product_stock_custom_meta( $product_id );
}

// On processed update product stock event (Like on orders)
add_action( 'woocommerce_updated_product_stock', 'wc_updated_product_stock_callback' );
function wc_updated_product_stock_callback ( $product_id ) {
    update_parent_variable_product_stock_custom_meta( $product_id );
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 tested and works.测试和工作。


5). 5)。 Change a product stock status programmatically and add/update product variation stock custom meta (your revisited code, but for product variations or even simple products) :以编程方式更改产品库存状态并添加/更新产品变体库存自定义元(您重新访问的代码,但对于产品变体甚至简单产品)

$stock_status = 'outofstock';

// Get an instance of WC_Product Object from the product ID (product variation)
$product = wc_get_product( $product_id );

// 1. Updating the stock quantity
$product->set_stock_quantity(0);

// 2. Updating the stock status (and "product_visibility" related term )
$product->set_stock_status($stock_status);

// 3. Save product data and refresh cached data
$product->save();

// Update parent variable product stock custom meta data
update_parent_variable_product_stock_custom_meta( $product );

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

相关问题 从可变产品中获取所有变体的总库存 In Woocommerce - Get the total stock of all variations from a variable product In Woocommerce Woocommerce 中可变产品的所有变体的总库存 - Total stock of all variations from a variable product In Woocommerce 显示在 WooCommerce 可变产品上售罄,当所有变体都缺货时 - Display sold out on WooCommerce variable product when all variations are out of stock 隐藏 WooCommerce 目录中具有特定库存状态的所有产品 - Hide all products with a specific stock status from WooCommerce catalog 隐藏在Woocommerce中变量产品缺货时添加到购物车块 - Hide Add to cart block when a variable product is out of stock in Woocommerce 如果 WooCommerce 中的一个变体缺货,则将所有变体设置为缺货 - Set all variations out of stock if one variation is out of stock in WooCommerce Woocommerce产品目录中的有货和缺货部分如何定制 - How to customize in stock and out of stock section in Woocommerce PRODUCT CATALOG 获取Woocommerce中可变产品的“库存”产品变量 - Get the count of “in stock” product variations for a variable product in Woocommerce 显示用于 woocommerce 缺货产品变体的自定义 div 块 - Display a custom div block for woocommerce out of stock product variations 更新 Woocommerce 中可变产品的所有变体价格 - Update all variations prices of a variable product in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM