在 WooCommerce 可变产品中的每个属性值旁边显示库存状态 [英] Show stock status next to each attribute value in WooCommerce variable products

查看:42
本文介绍了在 WooCommerce 可变产品中的每个属性值旁边显示库存状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的商店中实现类似这样的东西:

我有这个代码,但它在每个变体旁边显示数量.

function get_stock_variations_from_product(){全球$产品;$variations = $product->get_available_variations();foreach($variations 作为 $variation){$variation_id = $variation['variation_id'];$variation_obj = new WC_Product_variation($variation_id);$stock = $variation_obj->get_stock_quantity();}}

还有这个代码:

 全局 $product;$product_variations = $product->get_available_variations();foreach ($product_variations 作为 $variation) {$var_data = $variation['attributes'];$var_data['in_stock'] = $variation['is_in_stock'];}//列出库存可用或不可用数组的所有属性..echo '

';打印_r($var_data);echo '</pre>';死;

如何自定义变量产品以在每个属性值旁边显示库存状态?

谢谢

解决方案

更快和优化的 WooCommerce 3+ 代码版本仅在:

<块引用>

您可以使用其他 WC_Product_Variation 或 WC_Product 方法来获取和显示你想要的数据……

I want to implement in my store something similar like this:

I have this code, but this show quantity next to each variation.

function get_stock_variations_from_product(){
global $product;
$variations = $product->get_available_variations();
foreach($variations as $variation){
     $variation_id = $variation['variation_id'];
     $variation_obj = new WC_Product_variation($variation_id);
     $stock = $variation_obj->get_stock_quantity();
}
}

And also this code:

  global $product;
$product_variations = $product->get_available_variations();

foreach ($product_variations as $variation)  {
    $var_data = $variation['attributes'];
    $var_data['in_stock'] = $variation['is_in_stock'];
}

//List all attributes with stock available or not array..
echo '<pre>';
print_r($var_data);
echo '</pre>';
die;

How can I customize the variable products to show stock Status next to each attribute values?

Thanks

解决方案

Faster and optimized code version for WooCommerce 3+ only in:
Display variation stock status on single dropdown variable products in Wocommerce 3


Updated WooCommerce compatibility or previous version 2.6.x

You can do it using a custom function hooked in woocommerce_variation_option_name filter hook. This is only viable for products that have a unique attribute for their variations…

Here is the code:

add_filter( 'woocommerce_variation_option_name', 'customizing_variations_terms_name', 10, 1 );
function customizing_variations_terms_name( $term_name ){

    if(is_admin())
        return $term_name;

    global $product;
    $second_loop_stoped = false;

    // Get available product variations
    $product_variations = $product->get_available_variations();

    // Iterating through each available product variation
    foreach($product_variations as $variation){

        $variation_id = $variation['variation_id'];
        $variation_obj = new WC_Product_Variation( $variation_id );

        ## WOOCOMMERCE RETRO COMPATIBILITY ##
        if ( version_compare( WC_VERSION, '3.0', '<' ) ) # BEFORE Version 3 (older)
        {
            $stock_status = $variation_obj->stock_status;
            $stock_qty = intval($variation_obj->stock);

            // The attributes WC slug key and slug value for this variation
            $attributes_arr = $variation_obj->get_variation_attributes();
        }
        else # For newest verions: 3.0+ (and Up)
        {
            $stock_status = $variation_obj->get_stock_status();
            $stock_qty = $variation_obj->get_stock_quantity();

            // The attributes taxonomy key and slug value for this variation
            $attributes_arr = $variation_obj->get_attributes();
        }

        if(count($attributes_arr) != 1) // Works only for 1 attribute set in the product
            return $term_name;

        // Get the terms for this attribute
        foreach( $attributes_arr as $attr_key => $term_slug){
            // Get the attribute taxonomy
            $term_key = str_replace('attribute_', '', $attr_key );

            // get the corresponding term object
            $term_obj = get_term_by( 'slug', $term_slug, $term_key );
            if( $term_obj->name == $term_name ){ // If the term name matches we stop the loops
                $second_loop_stoped = true;
                break;
            }
        }
        if($second_loop_stoped)
            break;
    }
    if( $stock_qty>0 )
        return $term_name .= ' - ' . $stock_status . ' ('.$stock_qty.')';
    else
        return $term_name .= ' - ' . $stock_status;

}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code works and is tested in WooCommerce 2.6.x and 3+.


You will get this (for example):

You can use other WC_Product_Variation or WC_Product methods to get and display the data you want…

这篇关于在 WooCommerce 可变产品中的每个属性值旁边显示库存状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆