spree 3.0 只显示有库存的产品

spree 3.0 only show products that have available stock

所以我看到这个问题之前有人问过,但答案没有 posted 即使问题得到了回答,而且由于我没有足够的分数来评论我需要问的答案再次。这是原文post

这是为 spree 3.0 准备的。当我添加显示的代码时

joins(
  variants_including_master: :stock_items
).group('spree_products.id').having("SUM(count_on_hand) > 0")

给我的产品装饰师这样

def self.available(available_on = nil, currency = nil)
    # joins(:master => :prices).where("#{Product.quoted_table_name}.available_on <= ?", available_on || Time.now)      
    joins(:master => :prices).where("#{Product.quoted_table_name}.available_on <= ?", available_on || Time.now).joins(variants_including_master: :stock_items).group('spree_products.id').having("SUM(count_on_hand) > 0")  
end
search_scopes << :available

这破坏了视图,因为据我所知,由于 GROUP BY,结果具有一些奇怪的属性。

行的 products_helper.rb 文件中出现此错误
  max_updated_at = (@products.maximum(:updated_at) || Date.today).to_s(:number)

因为@products.maximum(:updated_at) returns 一个散列

{[6, 6]=>2015-07-04 19:08:36 UTC, [3, 3]=>2015-07-04 19:07:37 UTC, [10, 10]=>2015-07-07 19:01:12 UTC}

而不是返回日期

2015-07-07 19:01:12 UTC

它通常会这样做。

此代码在类群页面上也出现以下错误:

PG::GroupingError: ERROR: 列 "spree_products_taxons.position" 必须出现在 GROUP BY 子句中或在聚合函数中使用 第 1 行:SELECT DISTINCT "spree_products"."id", spree_products_taxon... ^

: SELECT DISTINCT "spree_products"."id", spree_products_taxons.position AS alias_0 FROM "spree_products" INNER JOIN "spree_variants" ON "spree_variants"."product_id" = "spree_products"."id" AND "spree_variants"."is_master" = 't' AND "spree_variants"."deleted_at" IS NULL INNER JOIN "spree_prices" ON "spree_prices"."variant_id" = "spree_variants"."id" AND "spree_prices"."deleted_at" IS NULL INNER JOIN "spree_variants" "variants_including_masters_spree_products" ON "variants_including_masters_spree_products"."product_id" = "spree_products"."id" AND "variants_including_masters_spree_products"."deleted_at" IS NULL INNER JOIN "spree_stock_items" ON "spree_stock_items"."variant_id" = "variants_including_masters_spree_products"."id" AND "spree_stock_items"."deleted_at" IS NULL LEFT OUTER JOIN "spree_products_taxons" ON "spree_products_taxons"."product_id" = "spree_products"."id" 其中 "spree_products"."deleted_at" 为 NULL AND ("spree_products".deleted_at 为空或 "spree_products".deleted_at >= '2015-07-09 14:30:53.668422') AND ("spree_products".available_on <= ' 2015-07-09 14:30:53.669089') AND "spree_products_taxons"."taxon_id" IN (2, 7, 14) AND (spree_prices.amount IS NOT NULL) AND "spree_prices"."currency" = 'USD' GROUP BY spree_products.id HAVING SUM(count_on_hand) > 0 ORDER BY spree_products_taxons.position ASC LIMIT 12 OFFSET 0

所以必须有更好的方法来做到这一点,这样 GROUP by 子句就不会把所有东西都扔掉。

尝试这样的事情:

  joins(:master => :prices, variants: :stock_items).where("#{StockItem.quoted_table_name}.count_on_hand > 0 AND #{Product.quoted_table_name}.available_on <= ?", available_on || Time.now).uniq

您可能还想包括 backorderabletrack_inventory - 我不知道您的设置,也许您根本没有这样的变体。

编辑:添加了 uniq。如果你也想要主变体,那么只需将 variants: :stock_itemspart 更改为 variants_including_master: :stock_items.