skip to Main Content

In relation to question asked on wordpress.stackexchange

I am trying to hide the ‘add to cart’ woocommerce button when a user selects a specific item in a dropdown list. I am using the WordPress platform and have tried multiple JQuery snippets in both functions.php and through 3rd party plugins but am still struggling to get it to work.

In Functions.php theme file

function load_scripts() {
?>
    <script type="text/javascript">
    $(document).ready(function(){
        $(document.getElementsByName("select-1574795073993")).change(function(){
            $( "select option:selected").each(function(){
                if($(this).attr("value")=="International"){
                    $(".single_add_to_cart_button").hide();
                }
            });
        }).change();
    });
    </script>
<?php
}
add_action('wp_enqueue_scripts', 'load_scripts');

Please can someone assist with simple steps to 1) provide correct code, and 2) insert in the woocommerce.com hosted plarform.

Much appreciated!

2

Answers


  1. Chosen as BEST ANSWER

    In addition to the above, I would like to add an AND clause so that also if the item is sold by Ronks Michael that it will hide button as well

    E.g. of the source code:

    <a class="by-vendor-name-link" style="display: block;" href="https://picflick.co.za/vendor/ronks-michael/">Sold By Ronks Michael</a>
    

    So when it = "Sold By Ronks Michael" the if statement will be true.

    Thank you


  2. Try this below code.

    function load_scripts(){ ?>
        <script type="text/javascript">
            (function($){
                $(document).ready(function(){
                    $('select[name="select-1574795073993"]').on('change', function() {
                        if( this.value == "International" && $('.product_meta .by-vendor-name-link').text() == 'Sold By Ronks Michael' ){
                            $(".single_add_to_cart_button").hide();
                        }else{
                            $(".single_add_to_cart_button").show(); 
                        }
                    });
                });
            })(jQuery);
        </script>
    <?php }
    add_action( 'wp_footer', 'load_scripts', 10, 1 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search