如何在 Google Content API(购物)中插入 customBatch

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

我正在尝试在 Google Merchant Center 上插入产品。我目前正在使用 Google API PHP 客户端,并且我无法在任何类和扩展它的类中找到 toSimpleObject 函数。

$this->service = new Google_Service_ShoppingContent($client);

$product = array("batchId" => $batchID,
                      "merchantId" => $this->googleapi->merchantID,
                      "method" => "insert",
                      "product" => array(
                        "kind" => "content#product",
                        "offerId" => $skuDetails['SKU'],
                        "title" => $skuDetails['TITLE'],
                        "description" => $skuDetails['DESCRIPTION'],
                        "imageLink" => $skuDetails['IMAGE'],
                        "contentLanguage" => "en",
                        "targetCountry" => "US",
                        "channel" => "online",
                        "availability" => ($skuDetails['QUANTITY'] > 0)?'in stock':'out of stock',
                        "brand" => $skuDetails['BRAND'],
                        "condition" => $skuDetails['CONDITION'],
                        "minHandlingTime" => $skuDetails['HANDLING_TIME'],
                        "ageGroup" => 'adult',
                        "maxHandlingTime" => ($skuDetails['HANDLING_TIME'] + 2),
                        "googleProductCategory" => (empty($skuDetails['CATEGORYID']))?$skuDetails['CATEGORYPATH']:$skuDetails['CATEGORYID'],
                        "price" => [
                          "value" => $price['lp'],
                          "currency" => "USD"
                        ]
                      )
                    );


$productObject = new Google_Service_ShoppingContent_ProductsCustomBatchRequest();
$productObject->setEntries($product);

$result = $this->service->products->custombatch($productObject);

错误:

An uncaught Exception was encountered
Type: Error

Message: Call to undefined method Google_Service_ShoppingContent_ProductsCustomBatchRequest::toSimpleObject()

Line Number: 108

Backtrace:

File: vendor/google/apiclient-services/src/Google/Service/ShoppingContent/Resource/Products.php
Line: 40
Function: call
php google-api-php-client google-shopping-api
2个回答
0
投票

您应该使用

Google_Service_ShoppingContent_Product
将数据插入到您的产品实例中,然后您可以使用 custombatch 上传它

 $product = new Google_Service_ShoppingContent_Product();

 $product->setId($id);
 $product->setTitle($title);

0
投票

以防万一 5 年后有人在寻找这个......

$productObject->setEntries($entries);
函数需要一个
ProductsCustomBatchRequestEntry
数组,而不是一个产品数组。

这就是我发送的方式:

    $batch_request = new Google\Service\ShoppingContent\ProductsCustomBatchRequest();   
    $entries = [];

    // $google_products are an array of Google\Service\ShoppingContent\Product
    foreach ($google_products as $key => $google_product) {
        $batch_entry =  new Google\Service\ShoppingContent\ProductsCustomBatchRequestEntry();
        $batch_entry->setProduct($google_product);
        $batch_entry->setMerchantId({{your merchant id}});
        $batch_entry->setMethod('insert');
        $batch_entry->setBatchId($key);
        $entries[] = $batch_entry;
   }
   $batch_request->setEntries($entries);
© www.soinside.com 2019 - 2024. All rights reserved.