Krullmizter
Krullmizter

Reputation: 567

Remove and replace woocommerce add to cart button

I'm working on a solution where I have a ACF on each WooCommerce product. When the custom field is other than NULL it will add a custom anchor tag below the previous Add to cart button. This works fine, but I can't get it to work so that when the above steps are completed, the add to cart button would be removed with the hook woocommerce_is_purchasable. This might be a bad idea overall, because I've seen that if I use the woocommerce_is_purshable, and set it to false outside the woocommerce_after_add_to_cart_button hook it removes both the Add to cart button and my custom made Order A Catalog button.

add_filter('woocommerce_after_add_to_cart_button', function () {
    $order_a_catalog_url = get_field('order_a_catalog_url', get_the_ID());
    $order_a_catalog_name = __('Order A Catalog', 'wp');

    if ($order_a_catalog_url != null) {
        echo '<a class="button" href="'.$order_a_catalog_url.'">'.$order_a_catalog_name.'</a>';

        add_filter( 'woocommerce_is_purchasable', '__return_false' );
    }
    
}, 100);

Edit updated code after answer.

add_action('woocommerce_simple_add_to_cart', function () { 
    global $product;

    $order_a_catalog_url  = get_field('order_a_catalog_url', $product->get_id());
    $order_a_catalog_name = __('Order A Catalog', 'wp');

    if ($order_a_catalog_url != null) {
        echo '<a class="button" href="'.$order_a_catalog_url.'">'.$order_a_catalog_name.'</a>';
        remove_action('woocommerce_'.$product->get_type().'_add_to_cart', 'woocommerce_'.$product->get_type().'_add_to_cart', 30);
    }

});

Upvotes: 1

Views: 2413

Answers (1)

Bhautik
Bhautik

Reputation: 11282

You can use the woocommerce_simple_add_to_cart action hook to remove add to cart button. check below code.

add_filter('woocommerce_after_add_to_cart_button', function () {
    $order_a_catalog_url = get_field('order_a_catalog_url', get_the_ID());
    $order_a_catalog_name = __('Order A Catalog', 'wp');

    if ($order_a_catalog_url != null) {
        echo '<a class="button" href="'.$order_a_catalog_url.'">'.$order_a_catalog_name.'</a>';
    }
    
}, 100);


add_action('woocommerce_simple_add_to_cart',   'wc_68661105_remove_add_to_cart_buttons_single' );
function wc_68661105_remove_add_to_cart_buttons_single(){ 
    global $product;
    $order_a_catalog_url = get_field( 'order_a_catalog_url', $product->get_id() );
    $order_a_catalog_name = __('Order A Catalog', 'wp');
    if ($order_a_catalog_url != null) {
        echo '<a class="button" href="'.$order_a_catalog_url.'">'.$order_a_catalog_name.'</a>';
        remove_action( 'woocommerce_'.$product->get_type().'_add_to_cart', 'woocommerce_'.$product->get_type().'_add_to_cart', 30 );
    }
}

Below actions are for other product types if you want to also remove add to cart button to variable, external, and grouped product type.

add_action('woocommerce_variable_add_to_cart', 'wc_68661105_remove_add_to_cart_buttons_single' );   
add_action('woocommerce_external_add_to_cart', 'wc_68661105_remove_add_to_cart_buttons_single' );   
add_action('woocommerce_grouped_add_to_cart',  'wc_68661105_remove_add_to_cart_buttons_single' );   

Code will go in active theme functions.php file. Tested and Works.

Upvotes: 1

Related Questions