2

I'd like to use the published field on products that's documented here in the Shopify API:

Hide a published product by changing the published attribute to false

I can change the flag on any product, but I have not figured out yet how to get the published field back from the API. It's not listed the list of fields of the Product object.

4

2 回答 2

3

您可以published_at直接设置单个产品的字段,如下所述

product.published_at = nil; product.save              # hides product
product.published_at = Time.now.utc; product.save     # makes product visible

您还可以通过设置published为 false 来隐藏产品集合,如下所述

collection = ShopifyAPI::CustomCollection.find(params[:collection_id])
collection.published = false
collection.save

更新

现在我更好地理解了这个问题,这就是你想要的答案。您无法真正获得“已发布”值,因为published产品上并没有真正的属性。但是,您可以检查该published_at字段并检查它是否nil(未发布)。设置published = false显然为您将其设置为 nil 。

于 2012-12-19T17:08:45.340 回答
0

您可以很容易地获得已发布的状态。它在 API 中作为端点:published_status 这返回已发布、未发布和任何。 https://help.shopify.com/api/reference/product 用法:

To get an array of all published products:
GET /admin/products.json?publish_status=published

To get an array of all unpublished products:
GET /admin/products.json?publish_status=unpublished

然后迭代数组,找到你想要的产品。一些执行此操作的示例 php:

function isPublished($shopify, $myprodid) {
  $products = $shopify('GET /admin/products.json?publish_status=unpublished');
  foreach($products as $prod) {
    if($prod['id'] == $myprodid)
      return false;
  }
  return true
}

除非您有大量未发布的产品,否则迭代未发布的产品应该很快。然后交换测试。

于 2016-05-12T05:00:09.657 回答