简体   繁体   English

获取Woocommerce中可变产品的“库存”产品变量

[英]Get the count of “in stock” product variations for a variable product in Woocommerce

I'm trying to make a snippet for functions.php file that shows only one price of selected variation, thus omitting the price range displayed along with variation price on single product page. 我正在尝试为functions.php文件创建一个代码段,该文件只显示所选变体的一个价格,从而省略了单个产品页面上显示的价格范围以及变化价格。 I'm using the following code: 我正在使用以下代码:

    add_action( 'woocommerce_before_single_product', 'my_remove_variation_price' );
$product_variations=$product_variable->get_available_variations;
function my_remove_variation_price() {
  global $product;
  if ( $product->is_type( 'variable' ) {
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price' );
  }
}

The problem is when you have, for instance, two variations of single product and one goes out of stock, this script hides the price of one remaining variation on single product page. 问题是,例如,当您有两种不同的单品和一种缺货时,此脚本会隐藏单个产品页面上剩余的一个变体的价格。 I was thinking maybe to have a COUNT of available variations per product and use IF to show them using standard single product template. 我想也许每个产品可以有COUNT个可用的变体,并使用IF来使用标准的单个产品模板显示它们。 Or maybe you have better idea how to solve that? 或者你可能更清楚如何解决这个问题?

To get the "in stock" variations count of a variable product on single product pages you can: 要在单个产品页面上获得可变产品的“库存”变化计数,您可以:

1) Use only php and Woocommerce WC_Product methods: 1)仅使用php和Woocommerce WC_Product方法:

global $product;
$count_in_stock = 0;

if ( $product->is_type( 'variable' ) ) {
    $variation_ids = $product->get_children(); // Get product variation IDs

    foreach( $variation_ids as $variation_id ){
        $variation = wc_get_product($variation_id);
        if( $variation->is_in_stock() )
            $count_in_stock++;
    }
}

// Your condition
if( $count_in_stock <= 1 ){
   // Do something
}

2) Use a SQL query with few php (quick and lighter): 2)使用几个php(快速和轻量级)的SQL查询:

global $wpdb, $product;
$product_id = $product->get_id();
$count_in_stock = 0;

if ( $product->is_type( 'variable' ) ) {
    $count = $wpdb->get_col("
        SELECT count(p.ID) as in_stock_count FROM {$wpdb->prefix}postmeta as pm
        INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID
        WHERE p.post_type LIKE 'product_variation' AND p.post_parent = $product_id
        AND pm.meta_key LIKE '_stock_status' AND pm.meta_value LIKE 'instock'
    ");

    $count_in_stock = reset($count);
}

// Your condition
if( $count_in_stock <= 1 ){
   // Do something
}      

Both codes are tested and work. 两个代码都经过测试和工作。

This is my Final working code, achieved with the help of LoicTheAztec answer: 这是我的最终工作代码,在LoicTheAztec答案的帮助下实现:

add_action( 'woocommerce_before_single_product', 'my_remove_variation_price' );
function my_remove_variation_price() {
    global $product;
    $count_in_stock = 0;

    if ( $product->is_type( 'variable' )) {

        $variation_ids = $product->get_children(); // Get product variation IDs

        foreach( $variation_ids as $variation_id ){
            $variation = wc_get_product($variation_id);
            if( $variation->is_in_stock() )
                $count_in_stock++;
        }   
    }

    if( $count_in_stock <= 1 ) 
    {
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price' );
    }   

}

声明:本站的技术帖子网页,遵循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 可变产品 - Hide WooCommerce variable product from catalog if all variations are out of stock 获取 WooCommerce 可变产品变体的销售日期 - Get on sale dates for the variations of a WooCommerce variable product 从Woocommerce 3中的可变产品获取版本ID - Get variations IDs from a variable product in Woocommerce 3 从 WooCommerce 中的可变产品中获取产品变体 skus - Get the product variations skus from a variable product in WooCommerce 从Woocommerce中的可变产品变体获取产品属性标签 - Get product attribute labels from variations of a variable product in Woocommerce Woocommerce产品变化:更改“库存”的位置 - Woocommerce product variations: Change position of „in stock“ 在产品管理页面上显示 woocommerce 产品变体和库存数量 - show woocommerce product variations and stock qty on product admin page WooCommerce 产品变化:自动启用管理库存和设置库存数量 - WooCommerce product variations: Auto enable Manage Stock and set stock quantity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM