模拟每个单独产品的打开

问题描述 投票:0回答:1

我有一个 woocommerce_before_single_product 操作。输入单个产品时,此操作会针对路线并提取有关数量和价格的信息并更新我的产品。如何模拟输入每个产品来更新它?

add_action('woocommerce_before_single_product', 'custom_checkout_process');
function custom_checkout_process() {
    global $wpdb;
    $parent = get_the_id();
    $variations = wc_get_product($parent)->get_available_variations();

    foreach ($variations as $variation) {
        $variation_id = $variation['variation_id'];
        $sku = $variation['sku'];
        $quantity = $variation['max_qty']; 

        $requestData = [
            "request" => [
                "header" => [
                    "auth" => [
                        "key" => "my key"
                    ]
                ],
                "body" => [
                    "items" => [
                        [
                            'sku' => $sku,
                            'qty' => $quantity
                        ]
                    ]
                ]
            ]
        ];

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'route',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($requestData),
            CURLOPT_HTTPHEADER => array(
                "cache-control: no-cache",
                "content-type: application/json"
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        $responseData = json_decode($response, true);

        if ($responseData && isset($responseData['response']['body']['items'])) {
            foreach ($responseData['response']['body']['items'] as $item) {
                if ($item['sku'] == $sku) {
                    $fulfillableQty = $item['fulfillableQty'];
                    $listPrice = $item['price']['listPrice']; 

                   
                    update_post_meta($variation_id, '_regular_price', $listPrice);
                    update_post_meta($variation_id, '_price', $listPrice);
                    update_post_meta($variation_id, '_stock', $fulfillableQty);
                }
            }
        }
    }
}

我想将其写在一个单独的文件中,当我调用它时在每个产品上执行此操作。

php wordpress woocommerce simulation action
1个回答
0
投票

您可以直接获取您商店中所有已发布的版本并进行更新。但这仅适用于有限批次的产品,因为脚本将在 PHP 超时时停止。

代码:

function update_product_variations() {
    // get all variations
    $variations = wc_get_products( array(
        'limit'         => -1,
        'status'        => 'publish',
        'type'          => 'product_variation',
    ) );

    // Loop through variation products (objects)
    foreach ($variations as $variation) {
        
        $sku      = $variation->get_sku();
        $quantity = 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : ''; 

        $requestData = [
            "request" => [
                "header" => [
                    "auth" => [
                        "key" => "my key"
                    ]
                ],
                "body" => [
                    "items" => [
                        [
                            'sku' => $sku,
                            'qty' => $quantity
                        ]
                    ]
                ]
            ]
        ];

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'route',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($requestData),
            CURLOPT_HTTPHEADER => array(
                "cache-control: no-cache",
                "content-type: application/json"
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        $responseData = json_decode($response, true);

        if ($responseData && isset($responseData['response']['body']['items'])) {
            foreach ($responseData['response']['body']['items'] as $item) {
                if ($item['sku'] == $sku) {
                    $fulfillableQty = $item['fulfillableQty'];
                    $listPrice = $item['price']['listPrice']; 

                    update_post_meta($variation_id, '_regular_price', $listPrice);
                    update_post_meta($variation_id, '_price', $listPrice);
                    update_post_meta($variation_id, '_stock', $fulfillableQty);
                }
            }
        }
    }
}

您需要使用

limit
page
和/或
offset
wc_get_products
参数来调整批量产品的代码以进行更新。

请参阅

wc_get_products
WC_Product_Query
文档

© www.soinside.com 2019 - 2024. All rights reserved.