From f7a767988c8f7688567cd7814b5fa7e846290d6d Mon Sep 17 00:00:00 2001 From: "Nihal M. Kelanthodika" Date: Thu, 3 Feb 2022 18:35:05 +0530 Subject: [PATCH 1/9] Add preferred_product_stock_display to Enterprise model, serializer and permitted attributes. (Fixed alignment of guest orders radio button as well) --- app/models/enterprise.rb | 1 + .../api/admin/enterprise_serializer.rb | 3 ++- .../permitted_attributes/enterprise.rb | 1 + .../form/_shop_preferences.html.haml | 22 +++++++++---------- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 9503c8dd02..ee4a2c75c6 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -20,6 +20,7 @@ class Enterprise < ApplicationRecord preference :shopfront_order_cycle_order, :string, default: "orders_close_at" preference :shopfront_product_sorting_method, :string, default: "by_category" preference :invoice_order_by_supplier, :boolean, default: false + preference :product_stock_display, :boolean, default: false # Allow hubs to restrict visible variants to only those in their inventory preference :product_selection_from_inventory_only, :boolean, default: false diff --git a/app/serializers/api/admin/enterprise_serializer.rb b/app/serializers/api/admin/enterprise_serializer.rb index 9248002a9e..8527531fc4 100644 --- a/app/serializers/api/admin/enterprise_serializer.rb +++ b/app/serializers/api/admin/enterprise_serializer.rb @@ -13,7 +13,8 @@ module Api :default_tag_group, :require_login, :allow_guest_orders, :allow_order_changes, :logo, :promo_image, :terms_and_conditions, :terms_and_conditions_file_name, :terms_and_conditions_updated_at, - :preferred_invoice_order_by_supplier + :preferred_invoice_order_by_supplier, + :preferred_product_stock_display has_one :owner, serializer: Api::Admin::UserSerializer has_many :users, serializer: Api::Admin::UserSerializer diff --git a/app/services/permitted_attributes/enterprise.rb b/app/services/permitted_attributes/enterprise.rb index fca5e1b708..7cbc5499bd 100644 --- a/app/services/permitted_attributes/enterprise.rb +++ b/app/services/permitted_attributes/enterprise.rb @@ -34,6 +34,7 @@ module PermittedAttributes :preferred_shopfront_producer_order, :preferred_shopfront_order_cycle_order, :show_customer_names_to_suppliers, :preferred_shopfront_product_sorting_method, :preferred_invoice_order_by_supplier, + :preferred_product_stock_display ] end end diff --git a/app/views/admin/enterprises/form/_shop_preferences.html.haml b/app/views/admin/enterprises/form/_shop_preferences.html.haml index b10914bdff..601bd1ed10 100644 --- a/app/views/admin/enterprises/form/_shop_preferences.html.haml +++ b/app/views/admin/enterprises/form/_shop_preferences.html.haml @@ -61,18 +61,16 @@ %label= t '.allow_guest_orders' %div{'ofn-with-tip' => t('.allow_guest_orders_tip')} %a= t 'admin.whats_this' - .eight.columns.omega - .row - .three.columns.alpha - = f.radio_button :allow_guest_orders, true, "ng-model" => "Enterprise.allow_guest_orders", "ng-value" => "true" - = f.label :allow_guest_orders, t('.allow_guest_orders_true'), value: :true - .five.columns.omega - = f.radio_button :allow_guest_orders, false, "ng-model" => "Enterprise.allow_guest_orders", "ng-value" => "false" - = f.label :allow_guest_orders, t('.allow_guest_orders_false'), value: :false - .row.warning{ng: {show: 'Enterprise.allow_guest_orders && Enterprise.allow_order_changes'}} - .eight.columns.alpha.omega - %i.icon-warning-sign - = t '.recommend_require_login' + .three.columns + = f.radio_button :allow_guest_orders, true, "ng-model" => "Enterprise.allow_guest_orders", "ng-value" => "true" + = f.label :allow_guest_orders, t('.allow_guest_orders_true'), value: :true + .five.columns.omega + = f.radio_button :allow_guest_orders, false, "ng-model" => "Enterprise.allow_guest_orders", "ng-value" => "false" + = f.label :allow_guest_orders, t('.allow_guest_orders_false'), value: :false + .row.warning{ng: {show: 'Enterprise.allow_guest_orders && Enterprise.allow_order_changes'}} + .eight.columns.alpha.omega + %i.icon-warning-sign + = t '.recommend_require_login' .row .three.columns.alpha %label= t '.allow_order_changes' From 356d759dbef68c83c051b30ded757215a66fe711 Mon Sep 17 00:00:00 2001 From: "Nihal M. Kelanthodika" Date: Thu, 3 Feb 2022 20:03:05 +0530 Subject: [PATCH 2/9] Add Display stock option to enterprises form --- .../enterprises/form/_shop_preferences.html.haml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/views/admin/enterprises/form/_shop_preferences.html.haml b/app/views/admin/enterprises/form/_shop_preferences.html.haml index 601bd1ed10..6410ce7b2b 100644 --- a/app/views/admin/enterprises/form/_shop_preferences.html.haml +++ b/app/views/admin/enterprises/form/_shop_preferences.html.haml @@ -71,6 +71,19 @@ .eight.columns.alpha.omega %i.icon-warning-sign = t '.recommend_require_login' + +.row + .three.columns.alpha + %label= t '.preferred_enterprise_stock_display' + %div{'ofn-with-tip' => t('.preferred_enterprise_stock_display_tip')} + %a= t 'admin.whats_this' + .three.columns + = f.radio_button :preferred_product_stock_display, true, 'ng-model' => 'Enterprise.preferred_product_stock_display', 'ng-value' => 'true' + = f.label :preferred_product_stock_display, t('.enabled'), value: :true + .five.columns.omega + = f.radio_button :preferred_product_stock_display, false, 'ng-model' => 'Enterprise.preferred_product_stock_display', 'ng-value' => 'false' + = f.label :preferred_product_stock_display, t('.disabled'), value: :false + .row .three.columns.alpha %label= t '.allow_order_changes' From 143c977a19d895b042dd73ff82031f22394f75db Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Tue, 29 Mar 2022 14:40:27 +0200 Subject: [PATCH 3/9] i18n for shop preferences --- app/views/admin/enterprises/form/_shop_preferences.html.haml | 4 ++-- config/locales/en.yml | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/views/admin/enterprises/form/_shop_preferences.html.haml b/app/views/admin/enterprises/form/_shop_preferences.html.haml index 6410ce7b2b..af468047de 100644 --- a/app/views/admin/enterprises/form/_shop_preferences.html.haml +++ b/app/views/admin/enterprises/form/_shop_preferences.html.haml @@ -74,8 +74,8 @@ .row .three.columns.alpha - %label= t '.preferred_enterprise_stock_display' - %div{'ofn-with-tip' => t('.preferred_enterprise_stock_display_tip')} + %label= t '.display_remaining_stock' + %div{'ofn-with-tip' => t('.display_remaining_stock_tip')} %a= t 'admin.whats_this' .three.columns = f.radio_button :preferred_product_stock_display, true, 'ng-model' => 'Enterprise.preferred_product_stock_display', 'ng-value' => 'true' diff --git a/config/locales/en.yml b/config/locales/en.yml index 5e0cc2918e..b2ce5e425e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -977,6 +977,8 @@ en: shopfront_sort_by_producer: "By producer" shopfront_sort_by_category_placeholder: "Category" shopfront_sort_by_producer_placeholder: "Producer" + display_remaining_stock: "Display remaining stock in shopfront" + display_remaining_stock_tip: "Display remaining stock in shopfront" social: twitter_placeholder: "eg. @the_prof" instagram_placeholder: "eg. the_prof" From 1c68293caccd96bdd8acaf75600286edeb744bbe Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Tue, 29 Mar 2022 15:18:20 +0200 Subject: [PATCH 4/9] Display remaining stock for variant in the shopfront Display only if all conditions are met: - Variant is not a bulk buy - Shop preference is true for `preferred_product_stock_display` - Only 3 (or less) items are remaining in stock - No item for this variant is already to cart --- .../controllers/shop_variant_controller.js.coffee | 5 ++++- .../api/enterprise_shopfront_serializer.rb | 2 +- .../products/_shop_variant_no_group_buy.html.haml | 2 ++ app/webpacker/css/darkswarm/_shop-inputs.scss | 11 +++++++++-- config/locales/en.yml | 1 + 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee index d6772d7e89..b47cba4aef 100644 --- a/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee @@ -1,4 +1,4 @@ -angular.module('Darkswarm').controller "ShopVariantCtrl", ($scope, $modal, Cart) -> +angular.module('Darkswarm').controller "ShopVariantCtrl", ($scope, $modal, Cart, Shopfront) -> $scope.updateCart = (line_item) -> Cart.adjust($scope.variant.line_item) @@ -69,3 +69,6 @@ angular.module('Darkswarm').controller "ShopVariantCtrl", ($scope, $modal, Cart) $scope.addBulk = (quantity) -> $scope.add(quantity) $modal.open(templateUrl: "bulk_buy_modal.html", scope: $scope, windowClass: "product-bulk-modal") + + $scope.displayRemainingInStock = -> + Shopfront.shopfront.preferred_product_stock_display && $scope.available() <= 3 && !$scope.variant.line_item.quantity diff --git a/app/serializers/api/enterprise_shopfront_serializer.rb b/app/serializers/api/enterprise_shopfront_serializer.rb index 29197dcb38..e777318127 100644 --- a/app/serializers/api/enterprise_shopfront_serializer.rb +++ b/app/serializers/api/enterprise_shopfront_serializer.rb @@ -9,7 +9,7 @@ module Api :instagram, :linkedin, :twitter, :facebook, :is_primary_producer, :is_distributor, :phone, :visible, :email_address, :hash, :logo, :promo_image, :path, :category, :active, :producers, :orders_close_at, :hubs, :taxons, :supplied_taxons, :pickup, - :delivery + :delivery, :preferred_product_stock_display has_one :address, serializer: Api::AddressSerializer has_many :supplied_properties, serializer: Api::PropertySerializer diff --git a/app/views/shop/products/_shop_variant_no_group_buy.html.haml b/app/views/shop/products/_shop_variant_no_group_buy.html.haml index 2157f548c7..7885a2e6b4 100644 --- a/app/views/shop/products/_shop_variant_no_group_buy.html.haml +++ b/app/views/shop/products/_shop_variant_no_group_buy.html.haml @@ -13,6 +13,8 @@ %button.variant-quantity{type: "button", ng: {click: "add(1)", disabled: "!canAdd(1)"}} -# U+FF0B Fullwidth Plus Sign + + .variant-remaining-stock{ng: {if: "displayRemainingInStock()"}} + {{ "js.shopfront.variant.remaining_in_stock" | t:{quantity: available()} }} .variant-quantity-display{ng: {class: "{visible: variant.line_item.quantity}"}} {{ "js.shopfront.variant.quantity_in_cart" | t:{quantity: variant.line_item.quantity || 0} }} %input{type: :hidden, diff --git a/app/webpacker/css/darkswarm/_shop-inputs.scss b/app/webpacker/css/darkswarm/_shop-inputs.scss index bf902f55b9..98f4f7fecf 100644 --- a/app/webpacker/css/darkswarm/_shop-inputs.scss +++ b/app/webpacker/css/darkswarm/_shop-inputs.scss @@ -71,12 +71,15 @@ button.variant-quantity { } } -.variant-quantity-display { - display: inline-block; +.variant-quantity-display, .variant-remaining-stock { font-size: 0.875em; margin-top: 0.25em; text-align: center; width: 7rem; + display: inline-block; +} + +.variant-quantity-display { visibility: hidden; &.visible { @@ -84,6 +87,10 @@ button.variant-quantity { } } +.variant-remaining-stock { + color: $red-500; +} + button.bulk-buy.variant-quantity { background-color: transparent; border: .1em solid $grey-200; diff --git a/config/locales/en.yml b/config/locales/en.yml index b2ce5e425e..953b7afec5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3106,6 +3106,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using add_to_cart: "Add" in_cart: "in cart" quantity_in_cart: "%{quantity} in cart" + remaining_in_stock: "Only %{quantity} left" bulk_buy_modal: min_quantity: "Min quantity" max_quantity: "Max quantity" From af69064d3aea0acae5b49247b914969d27e1aea3 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Tue, 29 Mar 2022 15:24:15 +0200 Subject: [PATCH 5/9] Add mocked shopfront --- .../controllers/checkout/shop_variant_controller_spec.js.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/javascripts/unit/darkswarm/controllers/checkout/shop_variant_controller_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers/checkout/shop_variant_controller_spec.js.coffee index c560a287c4..c1e780e783 100644 --- a/spec/javascripts/unit/darkswarm/controllers/checkout/shop_variant_controller_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/controllers/checkout/shop_variant_controller_spec.js.coffee @@ -20,7 +20,7 @@ describe "ShopVariantCtrl", -> CartMock = adjust: -> true - ctrl = $controller 'ShopVariantCtrl', {$scope: scope, $modal: $modal, Cart: CartMock} + ctrl = $controller 'ShopVariantCtrl', {$scope: scope, $modal: $modal, Cart: CartMock, Shopfront: {}} it "initializes the quantity for shop display", -> expect(scope.variant.line_item.quantity).toEqual 0 From 1ea2656bfb556020878a1ad38ed6c811f215075c Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Tue, 29 Mar 2022 15:47:21 +0200 Subject: [PATCH 6/9] i18n for shop preferences in admin --- config/locales/en.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/locales/en.yml b/config/locales/en.yml index 953b7afec5..cf215b3c69 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -979,6 +979,8 @@ en: shopfront_sort_by_producer_placeholder: "Producer" display_remaining_stock: "Display remaining stock in shopfront" display_remaining_stock_tip: "Display remaining stock in shopfront" + enabled: "Enabled" + disabled: "Disabled" social: twitter_placeholder: "eg. @the_prof" instagram_placeholder: "eg. the_prof" From 66386683b6674de2ce06dd41ee8bf22bb44ad89e Mon Sep 17 00:00:00 2001 From: Nihal Mohammed Date: Mon, 11 Apr 2022 20:47:08 +0530 Subject: [PATCH 7/9] Add system spec Fix linter errors --- spec/system/consumer/shopping/shopping_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/system/consumer/shopping/shopping_spec.rb b/spec/system/consumer/shopping/shopping_spec.rb index 4a55eae83d..8dfb8e2c79 100644 --- a/spec/system/consumer/shopping/shopping_spec.rb +++ b/spec/system/consumer/shopping/shopping_spec.rb @@ -376,6 +376,16 @@ describe "As a consumer I want to shop with a distributor", js: true do end end + it "shows quantity of remaining stock for products with quantity less < 3 when product_stock_display is true" do + distributor.set_preference(:product_stock_display, true) + variant.update on_hand: 2 + visit shop_path + + within_variant(variant) do + expect(page).to have_content "Only 2 left" + end + end + it "alerts us when we enter a quantity greater than the stock available" do variant.update on_hand: 5 visit shop_path From 15e7a5a4df22ec538fa65414521b4a13aca53db3 Mon Sep 17 00:00:00 2001 From: Nihal Mohammed Date: Tue, 12 Apr 2022 12:46:52 +0530 Subject: [PATCH 8/9] Rename product_stock_display to product_low_stock_display Rename product_stock_display to product_low_stock_display in enterprise_serializer and shop_variant_controller --- .../controllers/shop_variant_controller.js.coffee | 2 +- app/models/enterprise.rb | 2 +- app/serializers/api/admin/enterprise_serializer.rb | 2 +- app/serializers/api/enterprise_shopfront_serializer.rb | 2 +- app/services/permitted_attributes/enterprise.rb | 2 +- .../admin/enterprises/form/_shop_preferences.html.haml | 8 ++++---- spec/system/consumer/shopping/shopping_spec.rb | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee index b47cba4aef..c3afec6f3a 100644 --- a/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee @@ -71,4 +71,4 @@ angular.module('Darkswarm').controller "ShopVariantCtrl", ($scope, $modal, Cart, $modal.open(templateUrl: "bulk_buy_modal.html", scope: $scope, windowClass: "product-bulk-modal") $scope.displayRemainingInStock = -> - Shopfront.shopfront.preferred_product_stock_display && $scope.available() <= 3 && !$scope.variant.line_item.quantity + Shopfront.shopfront.preferred_product_low_stock_display && $scope.available() <= 3 && !$scope.variant.line_item.quantity diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index ee4a2c75c6..d27d605d1d 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -20,7 +20,7 @@ class Enterprise < ApplicationRecord preference :shopfront_order_cycle_order, :string, default: "orders_close_at" preference :shopfront_product_sorting_method, :string, default: "by_category" preference :invoice_order_by_supplier, :boolean, default: false - preference :product_stock_display, :boolean, default: false + preference :product_low_stock_display, :boolean, default: false # Allow hubs to restrict visible variants to only those in their inventory preference :product_selection_from_inventory_only, :boolean, default: false diff --git a/app/serializers/api/admin/enterprise_serializer.rb b/app/serializers/api/admin/enterprise_serializer.rb index 8527531fc4..2cef7c454c 100644 --- a/app/serializers/api/admin/enterprise_serializer.rb +++ b/app/serializers/api/admin/enterprise_serializer.rb @@ -14,7 +14,7 @@ module Api :logo, :promo_image, :terms_and_conditions, :terms_and_conditions_file_name, :terms_and_conditions_updated_at, :preferred_invoice_order_by_supplier, - :preferred_product_stock_display + :preferred_product_low_stock_display has_one :owner, serializer: Api::Admin::UserSerializer has_many :users, serializer: Api::Admin::UserSerializer diff --git a/app/serializers/api/enterprise_shopfront_serializer.rb b/app/serializers/api/enterprise_shopfront_serializer.rb index e777318127..cee6cc5145 100644 --- a/app/serializers/api/enterprise_shopfront_serializer.rb +++ b/app/serializers/api/enterprise_shopfront_serializer.rb @@ -9,7 +9,7 @@ module Api :instagram, :linkedin, :twitter, :facebook, :is_primary_producer, :is_distributor, :phone, :visible, :email_address, :hash, :logo, :promo_image, :path, :category, :active, :producers, :orders_close_at, :hubs, :taxons, :supplied_taxons, :pickup, - :delivery, :preferred_product_stock_display + :delivery, :preferred_product_low_stock_display has_one :address, serializer: Api::AddressSerializer has_many :supplied_properties, serializer: Api::PropertySerializer diff --git a/app/services/permitted_attributes/enterprise.rb b/app/services/permitted_attributes/enterprise.rb index 7cbc5499bd..2c7346eaf7 100644 --- a/app/services/permitted_attributes/enterprise.rb +++ b/app/services/permitted_attributes/enterprise.rb @@ -34,7 +34,7 @@ module PermittedAttributes :preferred_shopfront_producer_order, :preferred_shopfront_order_cycle_order, :show_customer_names_to_suppliers, :preferred_shopfront_product_sorting_method, :preferred_invoice_order_by_supplier, - :preferred_product_stock_display + :preferred_product_low_stock_display ] end end diff --git a/app/views/admin/enterprises/form/_shop_preferences.html.haml b/app/views/admin/enterprises/form/_shop_preferences.html.haml index af468047de..be2d5d2045 100644 --- a/app/views/admin/enterprises/form/_shop_preferences.html.haml +++ b/app/views/admin/enterprises/form/_shop_preferences.html.haml @@ -78,11 +78,11 @@ %div{'ofn-with-tip' => t('.display_remaining_stock_tip')} %a= t 'admin.whats_this' .three.columns - = f.radio_button :preferred_product_stock_display, true, 'ng-model' => 'Enterprise.preferred_product_stock_display', 'ng-value' => 'true' - = f.label :preferred_product_stock_display, t('.enabled'), value: :true + = f.radio_button :preferred_product_low_stock_display, true, 'ng-model' => 'Enterprise.preferred_product_low_stock_display', 'ng-value' => 'true' + = f.label :preferred_product_low_stock_display, t('.enabled'), value: :true .five.columns.omega - = f.radio_button :preferred_product_stock_display, false, 'ng-model' => 'Enterprise.preferred_product_stock_display', 'ng-value' => 'false' - = f.label :preferred_product_stock_display, t('.disabled'), value: :false + = f.radio_button :preferred_product_low_stock_display, false, 'ng-model' => 'Enterprise.preferred_product_low_stock_display', 'ng-value' => 'false' + = f.label :preferred_product_low_stock_display, t('.disabled'), value: :false .row .three.columns.alpha diff --git a/spec/system/consumer/shopping/shopping_spec.rb b/spec/system/consumer/shopping/shopping_spec.rb index 8dfb8e2c79..1fdc0b756c 100644 --- a/spec/system/consumer/shopping/shopping_spec.rb +++ b/spec/system/consumer/shopping/shopping_spec.rb @@ -377,7 +377,7 @@ describe "As a consumer I want to shop with a distributor", js: true do end it "shows quantity of remaining stock for products with quantity less < 3 when product_stock_display is true" do - distributor.set_preference(:product_stock_display, true) + distributor.set_preference(:product_low_stock_display, true) variant.update on_hand: 2 visit shop_path From 0327cc0d0b8769b0aeb84746ebb2498e1c5ce349 Mon Sep 17 00:00:00 2001 From: Nihal Mohammed Date: Tue, 12 Apr 2022 12:48:57 +0530 Subject: [PATCH 9/9] Update i18n to be more informative --- config/locales/en.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index cf215b3c69..139b2b8315 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -977,8 +977,8 @@ en: shopfront_sort_by_producer: "By producer" shopfront_sort_by_category_placeholder: "Category" shopfront_sort_by_producer_placeholder: "Producer" - display_remaining_stock: "Display remaining stock in shopfront" - display_remaining_stock_tip: "Display remaining stock in shopfront" + display_remaining_stock: "Display remaining stock in shopfront if low on-hand" + display_remaining_stock_tip: "Let shoppers know when there are only 3 or less items left." enabled: "Enabled" disabled: "Disabled" social: