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


How to hide quantity field in single product page for specific product attribute WooCommerce

我不想显示此属性的数量字段。

如何在单个产品页面上隐藏特定可变产品的"数量"字段?

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 
    } 
}

我刚刚解决了我的问题,希望它能帮助其他人

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>';
    }
}