Home > Software engineering >  Set the product category to New in for imported products automatically in woocommerce
Set the product category to New in for imported products automatically in woocommerce

Time:10-22

I will try to explain what my goal is and what i already did.

I am importing a bunch of products into WooCommerce, but now i would like to add a function, that does set the product category automatically to "NEW IN" if the product is not older than 60 days. After the 60 days it should be moved out of that category again.

To be said, i am in the learning process of php with WordPress and WooCommerce, so i am sorry if i don't know certain things.

   function add_product_to_new_in()
{
    global $product;

    // Get the date for the product published and current date
    $start = date('n/h/Y', strtotime($product->get_date_created()));
    $today = date('n/j/Y');


    // Get the date for the start of the event and today's date
    $start = new \DateTime($start);
    $end = new \DateTime($today);

    //FInd the difference
    $difference = $start->diff($end);
    $days = $difference->d;

    // If the difference is less than 60, apply "NEW IN cat"
    if ($days = (60 < $days)) {
        wp_set_object_terms($product, 40, 'product_cat');
    }
}

If you can help me in any way, i would appreciate it!

CodePudding user response:

You can do this on the init hook that fires every time on page load.

First, you have to get all products.

$args = array(
    'post_type'   => 'product',
    'posts_per_page' => -1
);

$products = new WP_Query( $args );
 
if ( $products->have_posts() ) {  while ( $products->have_posts() ) {  $products->the_post();
    // your logic will go here.
} wp_reset_postdata(); }

Now calculate days.

$product  = wc_get_product( get_the_ID() );
$datetime = $product->get_date_created();
$timezone = $datetime->getTimezone();
$now_time = new WC_DateTime();

$now_time->setTimezone($timezone);

$timestamp_diff = $now_time->getTimestamp() - $datetime->getTimestamp();
$data           = timestamp_to_array( $timestamp_diff );
$days           = $data['d'];

Then you can use wp_set_object_terms and wp_remove_object_terms to set and remove category.

// If the difference is less than 60, apply "NEW IN cat"
if ( $days < 60 ) {
    wp_set_object_terms( get_the_ID(), 40, 'product_cat', true );
}else{
    wp_remove_object_terms( get_the_ID(), 40, 'product_cat' );
}

Complete code. add this below code in your active theme functions.php file.

function add_category_to_product_for_certain_days(){

    $args = array(
        'post_type'   => 'product',
        'posts_per_page' => -1
    );

    $products = new WP_Query( $args );
     
    if ( $products->have_posts() ) {  while ( $products->have_posts() ) {  $products->the_post();

        $product  = wc_get_product( get_the_ID() );
        $datetime = $product->get_date_created();
        $timezone = $datetime->getTimezone();
        $now_time = new WC_DateTime();

        $now_time->setTimezone($timezone);

        $timestamp_diff = $now_time->getTimestamp() - $datetime->getTimestamp();
        $data           = timestamp_to_array( $timestamp_diff );
        $days           = $data['d'];
        
        // If the difference is less than 60, apply "NEW IN cat"
        if ( $days < 60 ) {
            wp_set_object_terms( get_the_ID(), 40, 'product_cat', true );
        }else{
            wp_remove_object_terms( get_the_ID(), 40, 'product_cat' );
        }

    } wp_reset_postdata(); }

}

add_action( 'init', 'add_category_to_product_for_certain_days', 10, 1 );

function timestamp_to_array( $timestamp ) {
    $d = floor($timestamp/86400);
    $_d = ($d < 10 ? '0' : '').$d;

    $h = floor(($timestamp-$d*86400)/3600);
    $_h = ($h < 10 ? '0' : '').$h;

    $m = floor(($timestamp-($d*86400 $h*3600))/60);
    $_m = ($m < 10 ? '0' : '').$m;

    $s = $timestamp-($d*86400 $h*3600 $m*60);
    $_s = ($s < 10 ? '0' : '').$s;

    return array('d' => $_d, 'h' => $_h, 'm' => $_m, 's' => $_s);
}

Tested and works.

  • Related