0

I have added a new custom field in checkout page named " Date of event ", It's working fine. But i want one thing to be done which is " When user order single/multiple products then hide "Add to cart" button and show unavailable message instead of button for that selected date of event. " Like if user selected date " 7/2/2019 " in " Date of event field " during checkout, then after he ordered that product, hide " Add to cart " button and show unavailable message instead of button for " 7/2/2019 " date of event. I don't know how to do this.
Which hooks and actions will do this.
I have googled it a lot, but didn't get any answer.

Please help me.

Custom field Code:

add_action('woocommerce_after_checkout_billing_form', 'date_of_event_field');

function date_of_event_field($checkout){

    echo '<div id="date_of_event_field" class="margin-top-20">';

        woocommerce_form_field( 'date_of_event', array(
            'type'          => 'date',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Date Of Event'),
            'required'      => true,
        ), $checkout->get_value( 'date_of_event' ));

    echo '</div>';

}

Code to hide Add to cart button and show message instead of button:

function make_product_unavailable( $_product, $order ) {
    if( $order->id == $_product ){
        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');
    }
}

It's a try from my side, because i don't know how to do to this and i don't know which filter/action hook will be used for this.

Please help me.

Zain Shabir
  • 35
  • 1
  • 12

1 Answers1

0

Source : https://wisdmlabs.com/blog/the-right-way-to-hide-add-to-cart-button-in-woocommerce/

Check if a customer has purchased a specific products in WooCommerce

Note : The below code is not tested but it will work if some part is modified as per your requirement..

add_filter( 'woocommerce_is_purchasable', 'hidecart',10,1);
function hidecart(){
 $found = has_bought_items();
 if(!empty($found["order_date"])){
    $current_date = date("d/m/Y");
    if($found["order_date"] == $current_date && $found["product_id"] == get_the_ID()){
        return false;
    }
 }
}

function has_bought_items() 
{
 $bought = false;
 $order_date =  '';
 //get product id if single or for shop page u will have to retrieve in some other way
$product_id = get_the_ID();

// Get all customer orders
$customer_orders = get_posts( array(
    'numberposts' => -1,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(),
    'post_type'   => 'shop_order', // WC orders post type
    'post_status' => 'wc-completed' // Only orders with status "completed"
) );
foreach ( $customer_orders as $customer_order ) {
    // Updated compatibility with WooCommerce 3+
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
    $order = wc_get_order( $customer_order );

    // Iterating through each current customer products bought in the order
    foreach ($order->get_items() as $item) {
        // WC 3+ compatibility
        if ( version_compare( WC_VERSION, '3.0', '<' ) ) 
            $order_product_id = $item['product_id'];
        else
            $order_product_id = $item->get_product_id();

        // Your condition related to your 2 specific products Ids
        if ( $product_id == $product_id) ) {
            $bought = true;
            $order_date = $order->order_date;
            // you can fetch your event date stored as order meta
            $arr = array("product_id"=>$product_id,"order_date"=>$order_date,"bought"=>$bought);
            return $arr;
        }
      }
    }
 $arr = array("product_id"=>"","order_date"=>$order_date,"bought"=>$bought);
 return $order_date;
 }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Now, add to cart button is hide in all products, not in that one which user has ordered. – Zain Shabir Jul 02 '19 at 13:26
  • Okay. Can you return an array from has_bought_items() in such way array("product_id"=>$product_id,"order_date"=>$order_date,"bought"=>$bought); Now , you can retrieve data from array in hidecart() and compare it with the product id and return false accordingly. – Sonali Agrawal Jul 02 '19 at 13:39
  • I didn't understand – Zain Shabir Jul 02 '19 at 13:42
  • Edited. You will have to modify it but you can check the way array is return. – Sonali Agrawal Jul 02 '19 at 14:59
  • but the button is hide in all products. I just want it to hide in ordered product according to only on that date which user has put in " date of event " field during checkout. – Zain Shabir Jul 02 '19 at 15:04
  • The updated code does. Basically, it is fetching the orders of current users and if the product id is found in already placed orders with the event date same as current date it returns false to hide the button. – Sonali Agrawal Jul 03 '19 at 05:10
  • The code you updated above, it's hiding add to cart button in all product which is not ordered – Zain Shabir Jul 03 '19 at 06:32