0

i have made a cron job to run a function but the function wont run. i did run the code that is inside the function, outside to see if it works and it did, it just wont do nothing if inside the function.

my hook add_action( 'Stock_Sync_Hook', 'Full_Stock_Sync' );

and my function

 function Full_Stock_Sync() {

                    $MasterwayRequest = new MasterwayRequest($this->APIKey, $this->APISecret);
                    $CompanyCode = $this->CompanyCode;

                    $CodArmazem = get_option('wc_mw_warehouse');
                    $CodLocalizacao = get_option('wc_mw_location');

                    $Stocks = $MasterwayRequest->stock_sync($CompanyCode, $CodArmazem, $CodLocalizacao);

                    if ($Stocks) {

                        global $woocommerce;

                        foreach ( $Stocks as $Stock ) {
                            $StockDisponivel = $Stock['StockDisponivel'];
                            $CodProduto = $Stock['CodProduto'];
                            $ID = wc_get_product_id_by_sku($CodProduto);

                            if ($ID)
                            {
                                $Product = new WC_Product( $ID );
                                $NewStock = wc_update_product_stock( $Product, $StockDisponivel);
                                echo $NewStock;
                                unset($ID);
                            }
                            else
                            {
                                $Product = new WC_Product( $CodProduto );
                                $NewStock = wc_update_product_stock( $Product, $StockDisponivel);
                                echo $NewStock;
                            }

                            $Product = new WC_Product( $CodProduto );
                            $wc_sku = $Product->get_sku();

                            if($wc_sku){
                                $ID = wc_get_product_id_by_sku($wc_sku);
                                $Product = new WC_Product( $ID );
                                unset($wc_sku);
                            }
                            $NewStock = wc_update_product_stock( $Product, $StockDisponivel);
                            echo $NewStock;

                        }//#FOREACH

                    }//#IF STOCKS
                }//#StockSync

The cron job is active but the function wont run, ive tried to change the time to like 10 sec and still nothing. Any ideias why it wont run?

1
  • Are you using a WP cron job or a server cron? WP cron jobs only run if someone is actively using the site. Basic troubleshooting should help - set up a much simpler function, run it as a cron job, and once you get that up and running, build your more complex actual function.
    – WebElaine
    Oct 21, 2019 at 14:03

1 Answer 1

0

I got it.

Im developing a plugin for woocommerce, so i was adding my Function to my plugin page instead of my include of the functions. Just put it on the functions and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.