php – WooCommerce自定义Variation Dropdown

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – WooCommerce自定义Variation Dropdown脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_2@
是否有可能定制woocommerce变化下拉菜单,如果我们有2种颜色和大小的变化,一种颜色“大”的缺货,是否可以添加“缺货”与每个变化下拉菜单.

解决方法

在你的theme / functions.PHP中试试这个

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;

}

发表在这里感谢loictheaztec

Show stock status next to each attribute value in WooCommerce variable products

@H_502_2@

脚本宝典总结

以上是脚本宝典为你收集整理的php – WooCommerce自定义Variation Dropdown全部内容,希望文章能够帮你解决php – WooCommerce自定义Variation Dropdown所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。