dongquexi1990 2017-09-24 23:54
浏览 52

循环数据库中的项目并更新从API中提取的变量

I'm building a script as a study project to retrieve the prices from an API and update the database every 5 min. The code below is working.
But it's a nightmare to maintain right now, as I have to list each product by its ID as well as referencing the api keys manually.

I tough about doing a foreach loop linking the products in the database and api based on their SKU and updating the price from there, but I just can't seem to pull it off.

<?php

require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;

$woocommerce = new Client(
    'http://example.com',
    'ck_xxxxxxxxxxx',
    'cs_xxxxxxxxxxx',
    [
        'wp_api' => true,
        'version' => 'wc/v2',
       // 'query_string_auth' => true
    ]
);

function parse_json( $file ) {
    $json = json_decode( file_get_contents( $file ), true );

    if ( is_array( $json ) && !empty( $json ) ) :
        return $json;  
    else :
        die( 'An error occurred while parsing ' . $file . ' file.' );

    endif;
}

$url = 'https://username:password@apiservice.com/apis/v1.0/c1212kj1n1n1b112';

$json = parse_json($url);

//Updates simple products prices. Product #586:
$data = [
    'update' => [
        [
            'id' => 586,
            'regular_price' => $json['name' == 'Product 1']['regular_price']
        ]
    ]    
];

print_r($woocommerce->post('products/batch', $data));

// Update product variations. Product #588:
$data = [
    'update' => [
        [
        'id' => 589,
        'regular_price' => $json['product_variation[0]']['regular_price']
        ],
                [
        'id' => 590,
        'regular_price' => $json['product_variation[1]']['regular_price']
        ],
        [
        'id' => 591,
        'regular_price' => $json['product_variation[2]']['regular_price']
        ],
    ]
];

print_r($woocommerce->post('products/588/variations/batch', $data));

?>

API output example:

[
    {
        "product_id": "100",
        "type": "simple",
        "parent_product_id": "",
        "name": "Product 1",
        "description": "A short description",
        "regular_price": "26.78",
        "manage_stock": "1",
        "stock": "5",
        "weight": "0",
        "attribute_name": "",
        "attribute_value": "",
        "has_variations": "",
        "image": "site.com/image1.png",
        "sku": "10010"
    },
    {
        "product_id": "200",
        "type": "variable",
        "parent_product_id": "",
        "name": "Product 2",
        "description": "A short description",
        "regular_price": "0",
        "manage_stock": "0",
        "stock": "0",
        "weight": "0",
        "attribute_name": "Color",
        "attribute_value": "",
        "has_variations": "1",
        "image": "site.com/image2.png",
        "sku": "11010"
    },
    {
        "product_id": "",
        "type": "product_variation",
        "parent_product_id": "200",
        "name": "",
        "description": "A short description",
        "regular_price": "3.70",
        "manage_stock": "1",
        "stock": "100",
        "weight": "0",
        "attribute_name": "Color",
        "attribute_value": "Red",
        "has_variations": "",
        "image": "",
        "sku": "11011"
    },

Is that possible to achieve?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效