如何在单个产品页面中隐藏特定产品属性WooCommerce的数量字段

问题描述:

I don't want to show the quantity field for this attribute.

How do I hide the "quantity" field for specific variable product on single product page?

add_filter( 'woocommerce_before_single_variation', 'remove_qty_field', 10, 2 );
function remove_qty_field() {
    global  $product;
    //get_product id
    $product = get_product( $product->ID );
    //get attributes
    $attributes = $product->get_attributes();
    //product type
    $product_type = $product->is_type( 'variable' );

    //if Product is variable 
    if( $product_type && array_key_exists( 'select-quantity', $attributes ) ){
        echo '<style type="text/css">.quantity, .buttons_added { width:0; height:0; display: none; visibility: hidden; }</style>';
    } 
     else{
       //leave as it is 
    } 
}

I just now solve my problem hope it will help others

add_filter( 'woocommerce_before_single_variation', 'remove_qty_field', 10, 2 );
function remove_qty_field() {
    global  $post;
    //get_product id
    $product = get_product( $post->ID );
    //get attributes
    $attributes = $product->get_attributes();
    //product type
    $product_type = $product->is_type( 'variable' );

    //if Product is variable 
    if( $product_type && array_key_exists( 'select-quantity', $attributes ) ){
        echo '<style type="text/css">.quantity, .buttons_added { width:0; height:0; display: none; visibility: hidden; }</style>';
    } 
     else{
      echo '<style type="text/css">.quantity, .buttons_added { margin-bottom:15px;}</style>';
    }
}