php - WooCommerce - Hide specific variations -


how can hide variation dropdown on product page, still let purchased through woocommerce url coupons?

if make variation not active, hidden drop down, message "this product can not purchased" in cart. want hide list, not disable entirely.

any appreciated.

thank you!

the following solution worked on theme, you're running bootstrap may have issues.

we'll modify option tag of options want hidden hidden attribute. take following code , add theme's functions.php or custom plugin:

custom code

function custom_woocommerce_dropdown_variation_attribute_options_html( $html, $args ) {     $product = $args[ 'product' ];     $attribute = $args[ 'attribute' ];     $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );     $options = $args[ 'options' ];     if ( empty( $options ) && !empty( $product ) && !empty( $attribute ) ) {         $attributes = $product->get_variation_attributes();         $options = $attributes[ $attribute ];     }      foreach ( $terms $term ) {         if ( in_array( $term->slug, $options ) && ***some condition***) {             $html = str_replace( '<option value="' . esc_attr( $term->slug ) . '" ', '<option hidden value="' . esc_attr( $term->slug ) . '" ', $html );         }     }     return $html; } add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'custom_woocommerce_dropdown_variation_attribute_options_html', 10, 2 ); 

note browsers don't recognise hidden attribute. if want full cross browser compatibility, you'll want @ answers @ how hide <option> in <select> menu css?. adding css property style="display:none" may work browsers.

advanced custom fields

now, in code above, i've written ***some condition***. condition needs check whether option should hidden or not. add information need create custom field attribute. can manually, advanced custom fields plugin (acf).

  1. create product attribute in products->attributes. tick yes enable archives? , make type "select". add attribute terms under configure terms.edit product attributeproduct attribute terms
  2. install advanced custom fields onto wordpress.
  3. create new field group.
  4. in field group create rule show field group if taxonomy term is equal to product **your attribute**.
  5. in field group create field field label='hidden', field type='true / false' , set other settings like.
  6. publish/update field group.
  7. go terms want hide created in step 1. should have tickbox select whether attribute should hidden or not. tick apply.term hidden checkbox
  8. create variable product variations made product attributes.add attribute productadd variation
  9. in custom code, remove ***some condition*** , replace get_field( 'hidden', $term ) ). acf function value of 'hidden' field attribute's tern.

after that, terms ticked hidden should not appear in dropdown on product page. in example can see green missing dropdown. dropdown hidden attribute


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -