简体   繁体   中英

Woocommerce dropdown screen, add to cart hook

i am using storefront woocommerce theme and i have a few variable products, the dropdown screen says chose an option but i want it to be add to cart ( so it picks the default version of the variable product) and i dont know how to do this.

I have code snippets and i am trying to solve this by using a hook on add to cart function.

function moznost() {

if ( $product->is_type( 'variable' ) ) {
$url=get_id();
        return $url;



}

add_action( 'woocommerce_product_add_to_cart_url', 'moznost' );

I want the dropdown screen to not say choose an option but rather say "add to cart" and add the default version of the variable product to cart

Actually you can do it using the admin panel without any code. Follow:

  1. Edit Product
  2. Variations
  3. Choose "Default Form Values"

Then the option of this variable product will be selected as you set in the default value.

If you still want to remove the Select Options text, use this code. Test result: 在此处输入图片说明

add_filter('woocommerce_dropdown_variation_attribute_options_args', 'custom_woocommerce_product_add_to_cart_text', 10, 2);

function custom_woocommerce_product_add_to_cart_text($args){
 $args['show_option_none'] = 0;
  return $args;
}

Ref: https://stackoverflow.com/questions/44652421/woocommerce-change-select-option-text-in-variable-product-page

I hope this will help. Have a good day.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM