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

Php 使用import.io中的API更新woocommerce中的单个产品库存,php,wordpress,woocommerce,import.io,Php,Wordpress,Woocommerce,Import.io,我有个问题 我正在尝试使用import.io中的API自动管理我转售的产品的库存 API向我返回一个值,我使用该值来确定产品是否有库存 每个组/类别使用不同的API(因为每个组都是不同的原始商店。此代码仅适用于一个类别,我需要弄清楚如何使其适用于其他类别) 我创建了这段代码,并试图让它在function.php中工作 我有一个自定义字段,其中包含每个产品的url源,我想将其用作api的参数 在检查哪个组是产品后,它会获取数据,以便我可以更新库存 问题是,我不知道如何以这种方式更新WooComme

我有个问题

我正在尝试使用import.io中的API自动管理我转售的产品的库存

API向我返回一个值,我使用该值来确定产品是否有库存

每个组/类别使用不同的API(因为每个组都是不同的原始商店。此代码仅适用于一个类别,我需要弄清楚如何使其适用于其他类别)

我创建了这段代码,并试图让它在function.php中工作

我有一个自定义字段,其中包含每个产品的url源,我想将其用作api的参数

在检查哪个组是产品后,它会获取数据,以便我可以更新库存

问题是,我不知道如何以这种方式更新WooCommerce的库存:O我希望在客户打开单个产品页面时进行验证

有人能看看我的代码并给我一些建议吗

谢谢大家!

$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;  
}