Home > Enterprise >  How to make a Woocommerce Grouped Product not purchasable 24 hours before set date based on ACF date
How to make a Woocommerce Grouped Product not purchasable 24 hours before set date based on ACF date

Time:01-23

I think I have a pretty specific question. I have only grouped products in my WooCommerce Shop. With ACF, I added a new meta field "date". For context, my main products are actually events and the linked products under it, are food options people can pre-order for the event. However, people should only be able to pre-order food up to 24 hours before the event takes place. That's why I added the date field with ACF.

I know there is the "is_purchasable" condition but I am completely new to WooCommerce and don't even know where to start to write the function that based on the ACF date value the product should not be purchasable anymore 24 hours before the event.

Can someone help me with that or point me to a resource where I can find similar codes that I can adjust for my need?

Many thanks in advance!

CodePudding user response:

Use this "get_field" function provided by the ACF plugin to get the value of the "date" meta field. It also uses the "DateTime" class to get the current date and time and to subtract 24 hours from the event date to get the cutoff date and time. Then it compares the current date and time with the cutoff date and time and sets the "purchasable" status to false if the current date and time is greater than or equal to the cutoff date and time.

Put the below code in your theme's functions.php

add_filter( 'woocommerce_product_is_purchasable', 'my_custom_is_purchasable', 10, 2 );
function my_custom_is_purchasable( $purchasable, $product ) {
    // Get the value of the "date" meta field
    $event_date = get_field('date', $product->get_id());
    // Get the current date and time
    $current_datetime = new DateTime();
    // Get the date and time 24 hours before the event
    $cutoff_datetime = new DateTime($event_date);
    $cutoff_datetime->sub(new DateInterval('PT24H'));
    // Compare the current date and time with the cutoff date and time
    if($current_datetime >= $cutoff_datetime){
        $purchasable = false;
    }
    return $purchasable;
}

CodePudding user response:

I figured it out, here is the code that works for me:

function unpublish_24h_before() {
// Get all products
$args = array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => -1
);
$products = get_posts( $args );

// Check expiration date for each product
foreach ( $products as $product ) {
    $expiration_date = get_field( 'date', $product->ID );
    if ( !empty( $expiration_date ) ) {
        $expiration_date = strtotime( $expiration_date );
        $time_left = $expiration_date - time();
        if ( $time_left <= 24 * 60 * 60 ) {
            // Move the product to the trash
            $product->post_status = 'draft';
        wp_update_post( $product );
        }
    }
}} unpublish_24h_before();
  • Related