Home > Blockchain >  Wordpress/WooCommerce cron troubles
Wordpress/WooCommerce cron troubles

Time:01-19

I am trying to create a cron job that turns WooCommerce products from publish to draft, and to draft from publish depending on stock and the low stock amount field. At first i get it to run, i had to tweak it a bit and now nothing happens. I adjusted some code i found here: How to auto draft WooCommerce product on specific date?

Here is the code i have been using:

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'check_daily_for_change_product_status' ) ) {
    wp_schedule_event( time(), 'daily', 'check_daily_for_change_product_status' );
}

// Hook into that action that'll fire every three minutes
add_action( 'check_daily_for_change_product_status', 'check_daily_for_change_product_status_func' );
function check_daily_for_change_product_status_func() {
    
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
        'post_status' => array("publish", "draft")
    );

    $get_products = new WP_Query( $args );

    if( $get_products->have_posts() ){ while ( $get_products->have_posts() ) { $get_products->the_post();

        $produkt_kat =  get_the_terms( get_the_ID(), 'product_cat' );
        foreach ($produkt_kat as $kat_id)   
        {   
            $lagerstyring = false;
            $slaa_til = get_field( "lavt_lager", $kat_id );
            if ($slaa_til != false)
            {
                $lagerstyring = true;
                $to = get_field("deaktiverings_emails", $kat_id);
                break;
            }
                
        }
                                                                              
            $product_id = get_the_ID();
            $admin_lager = $product_id->get_manage_stock();
            $low_stock_amount = $product_id->get_low_stock_amount();
            $lager = $product_id->get_stock_quantity();
            $produktnavn = $product_id->get_name();
            $produkturl = get_permalink($product_id);
            $tidspunkt = current_time();
            
            if ($admin_lager == true && $lagerstyring == true)
            {
                if ($low_stock_amount >= $lager)
                    {
                    $my_post = array(
                    'ID'           => get_the_ID(),
                    'post_status'  => 'draft',
                    );
                    wp_update_post( $my_post );
                    wp_mail( $to, "Vare slået fra", "Denne vare er blevet automatisk slået fra da den har en lav lagerbeholdning: <br /><b>" . $produktnavn . "</b><br />" . $produkturl . "<br />" . $tidspunkt, 'Content-type: text/html', ); 
                    }
                
                if ($lager > $low_stock_amount && $low_stock_amount != null)
                    {
                    $my_post = array(
                    'ID'           => get_the_ID(),
                    'post_status'  => 'publish',
                    );
                    wp_update_post( $my_post );
                    wp_mail( $to, "Vare slået til", "Denne vare er blevet automatisk udgivet, da dens lagerbeholdning er øget: <br /><b>" . $produktnavn . "</b><br />" . $produkturl . "<br />" . $tidspunkt , 'Content-type: text/html', );   
                    }
                
                    
        }
                                                                                
    } wp_reset_postdata(); }

}

CodePudding user response:

To anyone who might need this code, I figured it out and it works now.

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'check_daily_for_change_product_status' ) ) {
    wp_schedule_event( time(), 'daily', 'check_daily_for_change_product_status' );
}

// Hook into that action that'll fire every three minutes
add_action( 'check_daily_for_change_product_status', 'check_daily_for_change_product_status_func' );
function check_daily_for_change_product_status_func() {
    
    $args = array(
        'post_type'      => "product",
        'posts_per_page' => -1,
        'post_status' => array("publish", "draft")
    );

    $get_products = new WP_Query( $args );

    if( $get_products->have_posts() ){ while ( $get_products->have_posts() ) { $get_products->the_post();

        $produkt_kat =  get_the_terms( get_the_ID(), 'product_cat' );
        foreach ($produkt_kat as $kat_id)   
        {   
            $lagerstyring = false;
            $slaa_til = get_field( "lavt_lager", $kat_id );
            if ($slaa_til != false)
            {
                $lagerstyring = true;
                $to = get_field("deaktiverings_emails", $kat_id);
                break;
            }
                
        }
                                                                              
            global $product;                                                      
            $product_id = get_the_ID();
            $admin_lager = $product->get_manage_stock();
            $low_stock_amount = $product->get_low_stock_amount();
            $lager = $product->get_stock_quantity();
            $produktnavn = $product->get_name();
            $produkturl = $product->get_permalink();
            $tidspunkt = date('d/m/Y H:i:s', current_time('timestamp'));
            
            if ($admin_lager == true && $lagerstyring == true)
            {
                if ($low_stock_amount >= $lager)
                    {
                    $my_post = array(
                    'ID'           => get_the_ID(),
                    'post_status'  => 'draft',
                    );
                    wp_update_post( $my_post );
                    wp_mail( $to, "Vare slået fra", "Denne vare er blevet automatisk slået fra da den har en lav lagerbeholdning: <br /><b>" . $produktnavn . "</b><br />" . $produkturl . "<br />" . $tidspunkt, 'Content-type: text/html', ); 
                    }
                
                if ($lager > $low_stock_amount && $low_stock_amount != null)
                    {
                    $my_post = array(
                    'ID'           => get_the_ID(),
                    'post_status'  => 'publish',
                    );
                    wp_update_post( $my_post );
                    wp_mail( $to, "Vare slået til", "Denne vare er blevet automatisk udgivet, da dens lagerbeholdning er øget: <br /><b>" . $produktnavn . "</b><br />" . $produkturl . "<br />" . $tidspunkt , 'Content-type: text/html', );   
                    }
                
                    
        }
                                                                                
    } 
                                      wp_reset_postdata(); }

}
  • Related