Sami

With Plugin:

To restrict customers from buying a product unless they have placed an order 30 days ago in WooCommerce, you can use the following steps:

  1. Install and activate a plugin called “WooCommerce Conditional Shipping and Payments”. This plugin allows you to set conditions for products to be purchased based on various criteria.
  2. Once the plugin is activated, go to WooCommerce -> Conditional Shipping and Payments -> Payment Gateways.
  3. Under Payment Gateways, select the payment method that you want to restrict, and click on the “Add Condition” button.
  4. In the “Condition” field, select “Order Date”.
  5. In the “Operator” field, select “Greater Than”.
  6. In the “Value” field, enter “30 days ago”.
  7. Save your changes.

With these steps, the payment gateway you selected will now only be available to customers who have placed an order in the last 30 days. If a customer tries to purchase the product without meeting this condition, they will receive an error message indicating that they do not meet the purchase criteria.

With Custom Code:

To restrict customers from buying a product unless they have placed an order 30 days ago and only allow them to purchase after 30 days of their last order in WooCommerce, you can use the following custom code:

function restrict_product_purchase_by_order_date( $purchasable, $product ) {
    $order_date_limit = strtotime( '-30 days' ); // Set order date limit to 30 days ago
    $customer_id = get_current_user_id(); // Get current customer ID

    // Check if the customer has placed an order in the last 30 days
    if ( wc_get_orders( array(
        'customer' => $customer_id,
        'status' => array( 'wc-completed', 'wc-processing' ),
        'date_paid' => '>' . date( 'Y-m-d', $order_date_limit )
    ) ) ) {

        // If the customer has placed an order in the last 30 days, check if 30 days have passed since their last order
        $last_order_date = wc_get_customer_last_order_date( $customer_id );
        $time_since_last_order = strtotime( '+30 days', strtotime( $last_order_date ) );
        $current_time = current_time( 'timestamp' );

        // If 30 days have passed since the customer's last order, allow them to purchase the product
        if ( $current_time > $time_since_last_order ) {
            return $purchasable;
        }
        // If 30 days have not passed since the customer's last order, restrict the purchase
        else {
            wc_add_notice( __( 'You can only purchase this product after 30 days of your last order.', 'woocommerce' ), 'error' );
            return false;
        }
    }
    // If the customer has not placed an order in the last 30 days, restrict the purchase
    else {
        wc_add_notice( __( 'You must have placed an order in the last 30 days to purchase this product.', 'woocommerce' ), 'error' );
        return false;
    }
}

add_filter( 'woocommerce_is_purchasable', 'restrict_product_purchase_by_order_date', 10, 2 );

Function to check cusotmer last order date:

function get_customer_last_order_date( $customer_id ) {
    global $wpdb;
    $date_paid = $wpdb->get_var( $wpdb->prepare(
        "SELECT MAX(post_date) FROM {$wpdb->prefix}posts WHERE post_type = 'shop_order' AND post_status IN ( 'wc-completed', 'wc-processing' ) AND post_author = %d",
        $customer_id
    ) );
    return $date_paid;
}

This code should apply the restriction correctly and prevent the customer from adding the restricted product to the cart and proceeding to checkout. If the customer is still able to purchase the product, you may need to check if another plugin or function is interfering with the restriction.

Leave a Reply

Your email address will not be published. Required fields are marked *