最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp cron - update post every day

programmeradmin3浏览0评论

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

public function __construct( $plugin_name, $version ) {

        $this->plugin_name = $plugin_name;
        $this->version = $version;
        add_filter( 'cron_schedules', array( $this, 'Woocommerce_G2a_cron_schedule' ));
        add_action ( 'Woocommerce_G2a_cron_hook', array( $this, 'g2aProducts' ));
        if ( ! wp_next_scheduled( 'Woocommerce_G2a_cron_hook' )) {
            wp_schedule_event( time(), 'every_day', 'Woocommerce_G2a_cron_hook' );
        }
        add_action( 'admin_menu', array( $this, 'plugin_settings_page' ) );
    }

public function getProducts($id = null) {

        $g2aEmail   = '[email protected]'; // customer email
        $apiHash    = '#############'; // customer API hash
        $apiSecret  = '33333-44444-55555-66666-777777'; // customer API secret
        $apiKey     = hash('sha256', $apiHash . $g2aEmail . $apiSecret);
        $getPage = trim(isset($_GET['page'])?$_GET['page']:'1');

        if($getPage >=1 && $getPage<=500) {
            $getPageCount = $getPage;
        } else {
            $getPageCount = '1';
        }
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "=$getPageCount&currency=EUR&id=$id",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_TIMEOUT => 30,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => array(
                "Authorization: $apiHash, $apiKey",
                "Content-Type: application/json",
            )
        ));
        //Gets the API data
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        $dataget = json_decode($response,true);
        return $dataget;
    }

    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 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(";compact=y&apiKey=XXXXXXXXXXXXXXXXXXXX");
        $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;
    }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论