skip to Main Content

This is a customize add to cart button for example I added a custom or custom added field code in a product admin page that will place a location. and in the client or user side, the user cannot use the add to cart button if it cannot correspond the same exact location saved in the product details.

add_action('template_redirect', function (){
    global $post;
    if(isset($_POST['loc-address']) && ! empty($_POST['loc-address'])){
        //setcookie
        setcookie("loc_address",  $_POST['loc-address'], time() + 3600);

        //check if this was a product post type
        if(!is_null($_POST['loc-address']) && !empty($_COOKIE['loc_address'])){

            $value_product = wc_get_product($post->ID);

                remove_action('woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30);
            }
        else {
            echo 'Enable add to cart button';
        }
    }

2

Answers


  1. Hope this help you.

    add_filter( 'woocommerce_is_purchasable', 'vna_is_purchasable', function , 10, 2 );
    vna_is_purchasable( $purchasable, $product ){
        return true || false; // depending on your condition
    }
    
    Login or Signup to reply.
  2. Add the following code snippet in the Theme Functions (function.php) file of your currently activated theme as required.

    Change Button Text

    add_filter('woocommerce_product_single_add_to_cart_text','custom_add_to_cart_button_woocommerce');
    
    function custom_add_to_cart_button_woocommerce() {
      return __('WooCommerce custom add to cart button code', 'woocommerce');
    }
    

    Remove Add to cart buttons

    remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 20);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search