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).
- create product attribute in products->attributes. tick yes enable archives? , make type "select". add attribute terms under configure terms.
- install advanced custom fields onto wordpress.
- create new field group.
- in field group create rule show field group if
taxonomy term
is equal to
product **your attribute**
. - in field group create field field label='hidden', field type='true / false' , set other settings like.
- publish/update field group.
- go terms want hide created in step 1. should have tickbox select whether attribute should hidden or not. tick apply.
- create variable product variations made product attributes.
- in custom code, remove
***some condition***
, replaceget_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.
Comments
Post a Comment