【问题标题】:How can I get the URL for product's page using the Shopify API?如何使用 Shopify API 获取产品页面的 URL?
【发布时间】:2012-08-07 11:02:59
【问题描述】:

我检查了 Shopify 产品 API,但结果似乎没有返回产品页面 URL。 URL 看起来像是在对标题进行一些转换后产生的。是否有可靠的、明确定义的逻辑来获取产品页面 URL?

【问题讨论】:

    标签: shopify


    【解决方案1】:

    您可以将其作为“产品/”。我不确定是否有更好的方法,但它确实有效。

    【讨论】:

    • 这是正确的方法。 product.handle 是 URL 中使用的产品标题的 URL 安全版本。
    • 干杯,帮我省了一些麻烦。
    • 只有当商店 URL 路由为:products/< product.handle > 时才有效,否则失败。一些商店已配置为使用类似collections/accessories/<product.handle>
    • @HuckleberryFinn - 你是对的。我们有什么办法得到网址吗? shopify不应该在api调用中返回产品url吗?
    • @HuckleberryFinn 这是不正确的 - 产品的规范 URL 始终是 products/< product.handle >,但有些主题使用 within: 帮助器作为产品 URL,因此产品页面上的面包屑可以出现显示用户来自的集合,但这始终是可选的。用户可能来自任何集合,没有与产品关联的主要集合。
    【解决方案2】:

    我通常在config/initializers/shopify_api.rb添加这个:

    module ShopifyAPI
      class Shop
        def store_url_for(entity)
          return "http://#{self.domain}/#{entity.class.element_name}/#{entity.handle}"
        end
      end
    end
    

    【讨论】:

      【解决方案3】:

      为了完成 Michael Johnston 的回答,您需要先调用 ShopifyAPI::Shop.current,然后才能使用 store_url_for(entity)。

      例如,在控制器中获取产品的 url:

      product = ShopifyAPI::Product.find(@product_id)
      shop = ShopifyAPI::Shop.current
      product_url = shop.store_url_for(product)
      

      【讨论】:

        【解决方案4】:

        这是相当老的帖子,也许 api 已经升级,因为有 def。其他 url 结构,例如 collections/collection-name/products/product-name。

        仍在寻找是否有办法获得唯一的产品 URI

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多