update post every day

Question

I’m trying to update the post information every 24 hours, and I don’t know why this is not working?

public function add_g2a_product() {

        $product = array(
            'post_content' => $_POST['description'],
            'post_name'    => $_POST['name'], 
            'post_title'   => $_POST['name'],
            /*'post_excerpt' => excerpt($_POST['description']),*/
            'post_status'  => 'publish', 
            'post_type'    => 'product', 
            'post_author'  => 1
        );

        $product_id = wp_insert_post( $product );
        add_post_meta( $product_id, '_sku', $_POST['sku'] );
        add_post_meta( $product_id, '_price', $_POST['price'] );
        add_post_meta( $product_id, '_regular_price', $_POST['price'] );
        add_post_meta( $product_id, '_visibility', 'visible' );
        add_post_meta( $product_id, '_stock_status', 'instock');
        add_post_meta( $product_id, '_stock', $_POST['quantity'] );
        add_post_meta( $product_id, '_g2a_product', 1 );
        return json_encode(array("error" => false, "message" => "Product added to store."));
    }

    public function g2aProducts() {

        $sekRate = file_get_contents("http://free.currencyconverterapi.com/api/v5/convert?q=EUR_SEK&compact=y&apiKey=535f6713b880e61832e0");
        $sekRate = json_decode($sekRate);
        $SEK = $sekRate->EUR_SEK->val;

        $args = array(
            'meta_query' => array(
                array(
                    'key' => '_g2a_product',
                    'value' => 1,
                ),
            ),
            'post_type' => 'product',
            'posts_per_page' => -1,
        );
        $products = get_posts($args);
        foreach ($products as $product) {
            $post_id = $product->ID;
            $sku = get_post_meta($post_id, '_sku');
            $g2a_products = $this->getProducts($sku[0]);
            foreach($g2a_products['docs'] as $g2a_product) {
                $price = round($g2a_product['retail_min_price'] * $SEK, 2);
                update_post_meta($post_id, '_price', $price);
                update_post_meta($post_id, '_regular_price', $price);
            }
        }
    }
    public function Woocommerce_G2a_cron_schedule( $schedules ) {
        $schedules['every_day'] = array(
            'interval' => 86400, // Every 24 hours
            'display'  => __( 'Every 24 hours' ),
        );
        return $schedules;
    }

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/update-post-every-day/">

update post every day</a>
0
, , Nerdsbelike.com 2 months 0 Answers 44 views 0

Leave an answer