'Batch changing products status if their price are bigger than a certain amount

I want to create a cron automatization script that changes my product status from published to pending when the displayed price of the product is bigger than a certain amount that i set and from pending to published if the product's price is below or equal to the amount set (technically i want to hide the products that are too expensive, so i use the pending status as a tool to hide, ideally i would use a custom status, not the draft one).

How the script should work: If the displayed price of the product becomes 110 and the amount set to check the status is set to 100, the script will compare the product's displayed price and the amount set and after that check, the product that is published will get the pending status (because 110>100). For the moment the problem with my script, is that it is supposed also to set the products status from pending to published if they are below the amount set, and that doesn't happen, they are only forced to pending and not vice-versa (the displayed price is changed daily basis because it depends on the conversion rates - the price input is in Euro and the displayed price is in national currency).

Beside the problem with the script not returning the products from pending to publish, i also want to implement a function where products with a certain tag (e.g. "mustshow") will not change the status and will remain in the publish status ignoring the rules reffered in the 1st paragraph. Below you can see the code of the script.

I want to run this script every night.

public function updateProducts($product) {
        
        /* execution link https://website.com?update-products=1877890

        $getTrigger = $_REQUEST['update-products'] ?? null;
        $cronState = get_option('update-product-cron-state');
        $runningCode = '1877890';
       
        if ($getTrigger !== $runningCode) {
            return;
        }

      
        if ($cronState !== 'idle') {
            return;
        }

     
        // check if the script running
        update_option('update-product-cron-state', 'running');
        $changedProducts = [];
        $startTime = microtime(true);

        $args = [
            'post_type' => 'product',
            'posts_per_page' => -1
        ];
       
        $loop = new WP_Query($args);
        if ($loop->have_posts()) {
            while ($loop->have_posts()):$loop->the_post();

                $status = $loop->post->post_status;
       
                $product = wc_get_product($loop->post->ID);
               $normalPrice = $product->get_price();
           $regularPrice = $product->get_regular_price();
           $salePrice = $product->get_sale_price(); 
               
               
               /* Check if the price of the product is below the amount and if the status of the product is pending will change to published */
 
                if ($normalPrice <= 100 && $status === 'pending') {
                    $changedProducts[] = [
                        'id' => $loop->post->ID,
                        'status' => [
                            'from' => 'pending',
                            'to' => 'publish'
                        ]
                    ];
                    $product->set_status('publish');
                    $product->save();
                    continue;
                }

          
          /* Check if the price of the product is above the amount and if the status of the product is published it will change to pending */
          
                if ($normalPrice > 100 && $status === 'publish') {
                    $changedProducts[] = [
                        'id' => $loop->post->ID,
                        'status' => [
                            'from' => 'publish',
                            'to' => 'pending'
                        ]
                    ];
                    $product->set_status('pending');
                    $product->save();
                }
            endwhile;
        }
        wp_reset_postdata();

        /* execution details */
        
        $details = [
            'lastRun' => date('d-mm-y H:i:s'),
            'executionTime' => microtime(TRUE) - $startTime,
            'changedProducts' => $changedProducts,
        ];
       
        update_option('update-product-cron', $details);
        update_option('update-product-cron-state', 'idle');
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source