在Woocommerce中,在产品变体旁边显示“缺货”

问题描述 投票:2回答:1

我想对我的网站进行一些修改,但是由于某些原因,可以得到它。我尝试使用此thread of Stack中发现的功能:

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;

}

但是由于某些原因插入函数时,请在每个变体旁边显示“缺货”。我不使用标准的Woocommerce下拉变体样式,而是使用WC Variations Radio Buttons在每个变体旁边显示单选按钮,而不是下拉菜单。问题是我只想在每个变体旁边显示“缺货”。而不是“有库存”,因此客户在按“添加到购物车”按钮之前将知道所需的变型没有库存。所以似乎问题是此代码:

if( $stock_qty>0 )
return $term_name .= ' - ' . $stock_status . ' ('.$stock_qty.')';
else
return $term_name .= ' - ' . $stock_status;

}

函数看起来像返回

return $term_name .= ' - ' . $stock_status;

一直。有什么帮助吗?从我的问题预览可以看到here。谢谢。

EDIT:这是来自variable.php单选按钮插件的代码,其中将变化形式打印为单选按钮:

<td class="value">
                        <?php
                        if ( ! empty( $options ) ) {
                            if ( taxonomy_exists( $name ) ) {
                                // Get terms if this is a taxonomy - ordered. We need the names too.
                                $terms = wc_get_product_terms( $product->get_id(), $name, array( 'fields' => 'all' ) );

                                foreach ( $terms as $term ) {
                                    if ( ! in_array( $term->slug, $options ) ) {
                                        continue;
                                    }
                                    print_attribute_radio( $checked_value, $term->slug, $term->name, $sanitized_name );
                                }
                            } else {
                                foreach ( $options as $option ) {
                                    print_attribute_radio( $checked_value, $option, $option, $sanitized_name );
                                }
                            }
                        }

                        echo end( $attribute_keys ) === $name ? 
apply_filters( 'woocommerce_reset_variations_link', '<a 
class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' 
) : '';
                        ?>
                    </td>

也许需要更新某些内容以使其正常显示?

php wordpress function woocommerce variations
1个回答
0
投票

改为执行此操作

if( $stock_qty>0 )
    //return $term_name .= ' - ' . $stock_status . ' ('.$stock_qty.')';
    return $term_name;
else
    //return $term_name .= ' - ' . $stock_status;
return $term_name .= ' - Out Of Stock';
© www.soinside.com 2019 - 2024. All rights reserved.