Sha256: 70f6bf1639aacdb02039ddb7a1ac1d341c9f2659adcddb2d5b4f8566ef34f435

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Spree
  module OrdersControllerDecorator
    def self.prepended(base)
      base.class_eval do
        around_action :calculate_cart_diff, only: :populate
      end
    end

    private

    def calculate_cart_diff
      previous_cart_items = order_contents_hash

      yield

      current_cart_items = order_contents_hash

      cart_diff = current_cart_items.map do |variant_sku, quantity|
        added_quantity = quantity - (previous_cart_items[variant_sku] || 0)

        [variant_sku, added_quantity.positive? ? added_quantity : nil]
      end.to_h.compact

      cart_diff = cart_diff.map do |variant_sku, quantity|
        variant = Spree::Variant.find_by(sku: variant_sku)

        [
          variant_sku,
          {
            id: variant.product.master.sku,
            name: variant.product.name,
            variant: variant.options_text,
            price: variant.price,
            quantity: quantity
          }
        ]
      end.to_h

      flash[:added_to_cart] = cart_diff if cart_diff.present?
    end

    def order_contents_hash
      return {} if current_order.blank?

      current_order.line_items.each_with_object({}) do |li, acc|
        acc[li.variant.sku] ||= 0
        acc[li.variant.sku] += li.quantity
      end
    end

    ::Spree::StoreController.prepend(self)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solidus_seo-1.0.8 app/decorators/controllers/spree/orders_controller_decorator.rb