Woocommerce replace add to cart button with custom button/link

I need to replace the Add to Cart button (only on certain categories) with a custom button for “Text a Dealer.” The “Text a Dealer” button will trigger a gravity form in a lightbox which allows the user to submit a text message over the Twilio SMS service.

Here is a screenshot

Read More

I think I know how to link the button to a form in a lightbox but I do not know how to replace the button.

Related posts

2 comments

  1. add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button' );
    function replace_default_button(){
        return '<button>Text a Dealer</button>';
    }
    

    You can replace button code with your desired code.
    This will replace the default button code with your custom code.

    You also want this customization to be applicable only on certain categories. This can be achieved by addition of some more code. See below.

    add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button' );
    function replace_default_button(){
        //list category slugs where button needs to be changed
        $selected_cats = array('cat-one-slug', 'cat-two-slug', 'cat-three-slug');
        //get current category object
        $current_cat = get_queried_object();
        //get category slug from category object
        $current_cat_slug = $current_cat->slug;
        //check if current category slug is in the selected category list
        if( in_array($current_cat_slug, $selected_cats) ){
            //replace default button code with custom code
            return '<button>Text a Dealer</button>';
        }
    }
    

    Hope this helps.

  2. $args = array();
        if ( $product ) {
            $defaults = array(
                'quantity'   => 1,
                'class'      => implode(
                    ' ',
                    array_filter(
                        array(
                            'button add-to-cart-loop',
                            'product_type_' . $product->get_type(),
                            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
                            $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
                        )
                    )
                )
            );
    
            $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
    
            $price = $product->get_price();
            echo apply_filters( 'woocommerce_loop_add_to_cart_link',
                sprintf( '<a rel="nofollow" data-product_price_'.esc_attr( $product->get_id() ).'="'. $price .'" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s"><span>%s</span></a>',
                    esc_url( $product->add_to_cart_url() ),
                    esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
                    esc_attr( $product->get_id() ),
                    esc_attr( $product->get_sku() ),
                    esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
                    esc_html( $product->add_to_cart_text() )
                ),
            $product );
        }
    

Comments are closed.