Woocommerce - add and remove product automaticly from specific category when product's sale schedule start and ended

Viewed 117

I have written a code to add product to specific category when product's sale schedule start and remove from those category when product's sale schedule ended. This is my code but not working. Could anybody help me?

function zt_wc_scheduled_sales() {
    $data_store = WC_Data_Store::load( 'product' );

    // Sales which are due to start.
    $product_ids = $data_store->get_starting_sales();
    if ( $product_ids ) {
        do_action( 'wc_before_products_starting_sales', $product_ids );
        foreach ( $product_ids as $product_id ) {
            $product = wc_get_product( $product_id );

            if ( $product ) {
                $sale_price = $product->get_sale_price();
                wp_set_object_terms( $product_id, 'pishnahad', 'product_cat', true );

                $product->save();
            }
        }
        do_action( 'wc_after_products_starting_sales', $product_ids );

        WC_Cache_Helper::get_transient_version( 'product', true );
        delete_transient( 'wc_products_onsale' );
    }

    // Sales which are due to end.
    $product_ids = $data_store->get_ending_sales();
    if ( $product_ids ) {
        do_action( 'wc_before_products_ending_sales', $product_ids );
        foreach ( $product_ids as $product_id ) {
            $product = wc_get_product( $product_id );

            if ( $product ) {
                wp_remove_object_terms( $product_id, 'pishnahad', 'product_cat' );
                $product->save();
            }
        }
        do_action( 'wc_after_products_ending_sales', $product_ids );

        WC_Cache_Helper::get_transient_version( 'product', true );
        delete_transient( 'wc_products_onsale' );
    }
}
add_action( 'woocommerce_scheduled_sales', 'zt_wc_scheduled_sales' );

1 Answers

I have fixed bugs in your code.

Firts i have replaced get_starting_sales() and get_ending_sales() with custom query. so now you can get product ids

then I rearrange and removed unused code.

function zt_wc_scheduled_sales() {
    $data_store = WC_Data_Store::load( 'product' );
    global $wpdb;
   
    // Sales which are due to start.
    $product_ids = $wpdb->get_col(
        $wpdb->prepare(
            "SELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta
            LEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id
            LEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id
            WHERE postmeta.meta_key = '_sale_price_dates_from'
                AND postmeta_2.meta_key = '_price'
                AND postmeta_3.meta_key = '_sale_price'
                AND postmeta.meta_value > 0
                AND postmeta.meta_value < %s",
            time()
        )
    );
    
    if ( $product_ids ) {
        foreach ( $product_ids as $product_id ) {
            $product = wc_get_product( $product_id );
            if ( $product ) {
                wp_set_object_terms( $product_id, 'pishnahad', 'product_cat', true );
                $product->save();
            }
        }
    }

    // Sales which are due to end.
    $product_ids = $wpdb->get_col(
        $wpdb->prepare(
            "SELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta
            LEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id
            LEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id
            WHERE postmeta.meta_key = '_sale_price_dates_to'
                AND postmeta_2.meta_key = '_price'
                AND postmeta_3.meta_key = '_regular_price'
                AND postmeta.meta_value > 0
                AND postmeta.meta_value < %s",
            time()
        )
    );

    if ( $product_ids ) {
        foreach ( $product_ids as $product_id ) {
            $product = wc_get_product( $product_id );
            if ( $product ) {
                wp_remove_object_terms( $product_id, 'pishnahad', 'product_cat' );
                $product->save();
            }
        }
    }
}
add_action( 'woocommerce_scheduled_sales', 'zt_wc_scheduled_sales' );

Tested and works.

Related