Module: BaSpree::ProductPricingDecorator
- Defined in:
- app/decorators/ba_spree/product_pricing_decorator.rb
Overview
価格表(price_list)解決の上書きのみを担う decorator。ba_spree には association / slug 生成を 担う BaSpree::ProductDecorator も別にあるため、同一定数にならないよう名前を分けている。 @gem-override spree_core-5.3.6/app/models/spree/product.rb#lowest_price, #on_sale?, #price_varies? reason: 本家のこれら3メソッドは prices_including_master (-> { non_zero } のみ) を直接走査し、 pricing context (user / customer_group / price_list の status) を一切考慮しない。 そのため価格表(price_list)機能で以下の不具合が起きる (Issue #1219, Spree 5.4 でも未修正): - 草案(draft)の price_list でも一覧・関連商品で最安値が表示される - ゲストや顧客グループ外ユーザにも、他グループ向けの安い単価が一覧で表示される - 「価格」と「価格で比較」の両方設定で、対象外ユーザにもセールラベルが表示される 商品詳細の本体価格は price_for(Resolver 経由) で正しく解決されているため、一覧・関連・ ラベルでも同じ Resolver を通すよう上書きする。user は Spree::Current.pricing_user (store_controller_decorator が before_action で設定) から取得する。
ただし Resolver を variant ごとに呼ぶと多 variant 商品で描画が極端に重くなるため、
解決処理は Spree::Pricing::BatchResolver に切り出してバッチ化・メモ化してある (Issue #1290)。
BatchResolver は Resolver の選択規則を複製しているので、Spree アップグレード時は
BatchResolver 側の @gem-override マーカーも確認すること。
Instance Method Summary collapse
-
#lowest_price(currency) ⇒ Spree::Price?
指定通貨における最安価格(ログイン中ユーザに適用される price_list を考慮).
-
#on_sale?(currency) ⇒ Boolean
いずれかの variant が(ユーザに適用される価格で)セール中か.
-
#price_varies?(currency) ⇒ Boolean
複数 variant 間で(ユーザに適用される)価格が異なるか.
Instance Method Details
#lowest_price(currency) ⇒ Spree::Price?
指定通貨における最安価格(ログイン中ユーザに適用される price_list を考慮)
25 26 27 |
# File 'app/decorators/ba_spree/product_pricing_decorator.rb', line 25 def lowest_price(currency) resolved_prices_for(currency).min_by(&:amount) end |
#on_sale?(currency) ⇒ Boolean
いずれかの variant が(ユーザに適用される価格で)セール中か
39 40 41 |
# File 'app/decorators/ba_spree/product_pricing_decorator.rb', line 39 def on_sale?(currency) resolved_prices_for(currency).any?(&:discounted?) end |
#price_varies?(currency) ⇒ Boolean
複数 variant 間で(ユーザに適用される)価格が異なるか
32 33 34 |
# File 'app/decorators/ba_spree/product_pricing_decorator.rb', line 32 def price_varies?(currency) resolved_prices_for(currency).map(&:amount).uniq.size > 1 end |