phpjquerywordpresswoocommerce

how change sticky select option link to add to cart link in woocommerce on sticky select option button for variable product


I m new at Woocommerce. I want to change the sticky select option link in add to cart link in woocommerce on the sticky select option button for variable products. The product should be directly added to the cart. I want to do this when clicked add to cart at the bottom sticky it should add the default selected size into the cart. I don't have knowledge of PHP. I tried some below code but it only changed the select option to add to the cart text and the product is not added to the cart. enter image description here

add_filter( 'woocommerce_product_add_to_cart_text', 'custom_variable_add_to_cart_text' );
function custom_variable_add_to_cart_text( $add_to_cart_text ) {
   global $product;
   if( $product->is_type( 'variable' ) ) {
      $add_to_cart_text = __( 'Add to cart', 'woocommerce' );
   }
   return $add_to_cart_text;
}

enter image description here

I want to do when clicked add to cart at the bottom sticky it should add the default selected size into cart.

the links is below.. https://neobery.com/product/lavender-essential-oil-100-percent-pure-organic-natural-for-hair-skin-body/


Solution

  • Add below code inside function.php

    function my_custom_script()
    {
    ?>
      <script>
        jQuery(document).on(
          "click",
          ".single_link_to_cart_button",
          function(e) {
            e.preventDefault();
            jQuery('form.cart :submit').click();
          }
        );
      </script>
    <?php
    }
    add_action('wp_footer', 'my_custom_script');