From c73a84e2851b15dd5209fa1b84ec849e716c8da7 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Mon, 12 Nov 2012 19:48:48 +0000 Subject: [PATCH] Free delivery price for shopping_cart --- plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb | 1 + plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb | 1 + plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb | 32 +++++++++++++++++++++++--------- plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb | 13 ++++++++++++- plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb | 12 +++++++----- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb index de7ce05..96ea956 100644 --- a/plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb @@ -8,6 +8,7 @@ class ShoppingCartPluginMyprofileController < MyProfileController params[:settings][:enabled] = params[:settings][:enabled] == '1' params[:settings][:delivery] = params[:settings][:delivery] == '1' params[:settings][:delivery_price] = params[:settings][:delivery_price].to_d + params[:settings][:free_delivery_price] = params[:settings][:free_delivery_price].to_d end @settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin, params[:settings]) diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb index b465465..b8f94c1 100644 --- a/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb @@ -85,6 +85,7 @@ class ShoppingCartPluginProfileController < ProfileController def buy @environment = profile.environment + @settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin) render :layout => false end diff --git a/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb b/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb index d847b25..aacbf8b 100644 --- a/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb +++ b/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb @@ -8,22 +8,26 @@ module ShoppingCartPlugin::CartHelper end def get_price(product, environment, quantity=1) - float_to_currency_cart(sell_price(product)*quantity, environment) + float_to_currency_cart(price_with_quantity(product,quantity), environment) end - def get_total(items, environment) - float_to_currency_cart(items.map { |id, quantity| sell_price(Product.find(id)) * quantity}.sum, environment) + def price_with_quantity(product, quantity=1) + quantity = 1 if !quantity.kind_of?(Numeric) + sell_price(product)*quantity + end + + def get_total(items) + items.map { |id, quantity| price_with_quantity(Product.find(id),quantity)}.sum + end + + def get_total_on_currency(items, environment) + float_to_currency_cart(get_total(items), environment) end def items_table(items, profile, by_mail = false) environment = profile.environment settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin) items = items.to_a - if settings.delivery - delivery = Product.new(:name => _('Delivery'), :price => settings.delivery_price) - delivery.save(false) - items << [delivery.id, 1] - end quantity_opts = { :class => 'cart-table-quantity' } quantity_opts.merge!({:align => 'center'}) if by_mail @@ -31,6 +35,16 @@ module ShoppingCartPlugin::CartHelper price_opts.merge!({:align => 'right'}) if by_mail items.sort! {|a, b| Product.find(a.first).name <=> Product.find(b.first).name} + if settings.delivery + if settings.free_delivery_price && get_total(items) >= settings.free_delivery_price + delivery = Product.new(:name => _('Free delivery'), :price => 0) + else + delivery = Product.new(:name => _('Delivery'), :price => settings.delivery_price) + end + delivery.save(false) + items << [delivery.id, ''] + end + table = '' + @@ -48,7 +62,7 @@ module ShoppingCartPlugin::CartHelper ) end.join("\n") - total = get_total(items, environment) + total = get_total_on_currency(items, environment) delivery.destroy if settings.delivery table + diff --git a/plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb b/plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb index 34194b9..6b409dc 100644 --- a/plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb +++ b/plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb @@ -3,7 +3,12 @@ <% form_for(:settings, @settings, :url => {:action => 'edit'}, :html => {:method => 'post'}) do |f| %> <%= labelled_form_field(_('Enabled?'), f.check_box(:enabled)) %> <%= labelled_form_field(_('Delivery?'), f.check_box(:delivery)) %> - <%= labelled_form_field(_('Delivery price:'), f.text_field(:delivery_price)) %> + <% display_delivery_options = @settings.delivery ? 'auto' : 'none' %> +
<%=_('Delivery Options')%> + <%= labelled_form_field(_('Price:'), f.text_field(:delivery_price)) %> + <%= labelled_form_field(_('Free delivery price:'), f.text_field(:free_delivery_price)) %> + <%= content_tag('small', _('Empty stands for no free delivery price.')) %> +


@@ -11,3 +16,9 @@ <%= button :back, _('Back to control panel'), :controller => 'profile_editor' %>
<% end%> + + diff --git a/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb b/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb index 9db0672..c068acb 100644 --- a/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb +++ b/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb @@ -7,11 +7,13 @@ <%= labelled_form_field('* ' + _("Email"), f.text_field(:email, :class => 'required email') ) %> <%= labelled_form_field('* ' + _("Contact phone"), f.text_field(:contact_phone, :class => 'required') ) %> -
<%=_('Delivery Address')%> - <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %> - <%= labelled_form_field( _("City"), f.text_field(:city)) %> - <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %> -
+ <% if @settings.delivery %> +
<%=_('Delivery Address')%> + <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %> + <%= labelled_form_field( _("City"), f.text_field(:city)) %> + <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %> +
+ <% end %>
<%= submit_button(:send, _('Send buy request')) %>
-- libgit2 0.21.2