简体   繁体   中英

WooCommerce | Make products of category only available on one day of the week

I'm an absolute beginner in PHP and WooCommerce. Our shop sells products to businesses, but only on one day of the week each category. So I have category „Monday“, „Tuesday“ and so on. Problem is: if somebody puts a Monday-product into their shopping cart, he can buy it on Tuesday.
What I want to do is change the availability only to „available“ on the given day and change it back to „not available“ next day.

Also I don't know how to get ALL the products of one category. Here's what I tried for the Monday category. Please don't kill me …

Any help is highly appreciated! <3

function weekday_products() {
    $product_args = array(
        'post_status' => 'publish',
        'limit' => -1,
        'category' => 'Monday',
        //more options according to wc_get_products() docs
    );
    $products_monday = wc_get_products($product_args);

    if(date('D', $timestamp) === 'Mon') {
        foreach ($products_monday as $product) {
            if ( !$product->is_in_stock() ) {
                wc_update_product_stock_status( $product, 'instock' );
            }
        }
    }
    else {
        foreach ($products_monday as $product) {
            if ( $product->is_in_stock() ) {
                wc_update_product_stock_status( $product, 'outofstock' );
            }
        }
    }
}
add_action( 'only_on_weekday_products', 'weekday_products' );

At the moment I can't try the code, because the shop is in production. But I'm pretty sure, it wouldn't work anyway …

Edit: Another solution would be to clear the carts at midnight, so something like:


add_action( 'woocommerce_add_cart_item_data', 'woocommerce_clear_cart_url' );

function woocommerce_clear_cart_url() {
    $now = strtotime("now");
    $midnight = strtotime("00:00:00");
    if ( $now = $midnight ) {
        // Empty cart
        WC()->cart->empty_cart(true);
        WC()->session->set('cart', array());
    }

} 

Does this code work?

Here is how i would approach this. Place the following functions in your active theme functions.php

If you want for SEO purposes or something else to keep showing all products as they are then skip hide_products_per_day function. Only check your cart what is added to it.

//Show products only from specific category
function hide_products_per_day( $q ) {
    $day = date('l');
    $tax_query = $q->get( 'tax_query' );
    $tax_query[] = array(
        'taxonomy' => 'product_cat',
        'field'    => 'slug', // Query by term slug
        'terms'    => array( $day ), // In our case its current day
        'operator' => 'IN',
    );
    $q->set( 'tax_query', $tax_query );

}
add_action( 'woocommerce_product_query', 'hide_products_per_day' );

function check_products_in_cart() {
    if ( WC()->cart->is_empty() ) return; // Skip if cart is empty
    $day = date('l');
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( !has_term( $day, 'product_cat', $cart_item['product_id'] ) ) {
            WC()->cart->remove_cart_item( $cart_item_key );
            wc_print_notice( $cart_item['data']->get_name().' cant be purchased today!', 'error' ); // Change type of notice if you want either error, success or notice (or custome).
        }
    }
}
add_action( 'woocommerce_before_checkout', 'check_products_in_cart' ); // In case we go straight to checkout
add_action( 'woocommerce_before_cart', 'check_products_in_cart' ); // We want to check what is in cart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM