duanjian7343 2015-12-20 21:33
浏览 42

使用来自import.io的API更新woocommerce中的单个产品库存

I have a problem.

I'm trying to use an API from import.io for manage automatically the stock of products I reselling.

The API returns a value to me and I use this value to determine if the product is in stock or not.

Each group/category uses a different API (because each group is a different origin shop. This code is only for one category, I need to figure out how to make that works for dev the others).

I created this code and I'm trying to make it work in function.php

I have a custom field that has the url source to each product and I want to use it as a parameter to the api.

After checking which group is the product, it gets the data so I can update the stock.

The problem is that I do not know how to update the stock of WooCommerce in this way :O And I'd like to have that verification was done when the single product page was opened by the customer.

Can someone take a look at my code and give me some suggestions?

Thank you!

$userGuid = "c5ed744c-7c10-46d1-9c43-22c6eef5aaca";
$apiKey = "private";

// Issues a query request to import.io
function query($connectorGuid, $input, $userGuid, $apiKey) {

    $url = "https://query.import.io/store/connector/" . $connectorGuid . "/_query?_user=" . urlencode($userGuid) . "&_apikey=" . urlencode($apiKey);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Content-Type: application/json",
        "import-io-client: import.io PHP client",
        "import-io-client-version: 2.0.0"
    ));
    curl_setopt($ch, CURLOPT_POSTFIELDS,  json_encode(array("input" => $input)));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $result = curl_exec($ch);
    curl_close($ch);

    return json_decode($result);
}
//This is a custom field 
    $url_font = apply_filters('custom_url_font', get_post_meta($post->ID, '_url_font', true));

    add_action('woocommerce_before_shop_loop_item_title','category_stock');

function category_stock() {

    global $woocommerce,$product, $post;
    $post_id = $post->ID;
    $groupcat = 608; // category id for the this api
    $terms = get_the_terms( $post_id, 'product_cat' ); //get taxonamy of the products

        if ( $terms && ! is_wp_error( $terms ) ) :
            foreach ( $terms as $term ) {
                $catid = $term->term_id;
                if($groupcat == $catid) {
                    $result = query("d9df1137-b402-40ef-b472-35db84684fbe", array(
                    "webpage/url" => $url_font,//this is the url from the custom field
                    ), $userGuid, $apiKey, false);
                        if(($result->results[0]->stock)=="INSTOCK")
                            {
                             update_post_meta($post->ID, '_stock', '10');
                            }
                        else{
                             update_post_meta($post->ID, '_stock', '0');
                             }
                  }
          }endif;  
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?