Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在变量产品中的每个属性值旁边显示库存状态_Php_Wordpress_Woocommerce_Stock_Variations - Fatal编程技术网

Php 在变量产品中的每个属性值旁边显示库存状态

Php 在变量产品中的每个属性值旁边显示库存状态,php,wordpress,woocommerce,stock,variations,Php,Wordpress,Woocommerce,Stock,Variations,我想在我的商店中实现类似的功能: 我有这个代码,但这个代码在每个变量旁边显示数量 function get_stock_variations_from_product(){ global $product; $variations = $product->get_available_variations(); foreach($variations as $variation){ $variation_id = $variation['variation_id'];

我想在我的商店中实现类似的功能:

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

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();
}
}
还有这个代码:

  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;
global$产品;
$product_VARIANTIONS=$product->获取可用的_VARIANTIONS();
foreach($product_变量为$VARIANCE){
$var_data=$variation['attributes'];
$var_data['in_stock']=$variation['is_in_stock'];
}
//列出库存可用或不可用的所有属性数组。。
回声';
打印(变量数据);
回声';
死亡
如何自定义变量产品以在每个属性值旁边显示库存状态


感谢您为WooCommerce 3+提供了更快、优化的代码版本,仅适用于:


更新了WooCommerce兼容性或以前的版本2.6.x

您可以使用连接在
woocommerce\u variation\u option\u name
过滤器挂钩中的自定义函数来完成此操作。这仅适用于对其变体具有独特属性的产品

代码如下:

add_filter('woocommerce_variation_option_name','customize_variations_terms_name',10,1);
函数自定义\u变体\u术语\u名称($term\u name){
if(is_admin())
返回$term\u name;
全球$产品;
$second_loop_stoped=false;
//获取可用的产品变体
$product_VARIANTIONS=$product->获取可用的_VARIANTIONS();
//迭代每个可用的产品变体
foreach($product_变量为$VARIANCE){
$variation_id=$variation['variation_id'];
$variation_obj=新的WC_产品_variation($variation_id);
##WOOCOMMERCE复古兼容性##

if(version_compare(WC_version,'3.0','Hi,Loic)这对Woo 3.X有效。你能修改代码使其也适用于Woo 2.X吗?代码经过更新、测试并在2.6.X版本上运行,非常完美!!我希望你的代码对其他人有用。@DrMTR我也希望,因为这花费了我一些时间…)
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;

}