Created
July 19, 2016 18:51
-
-
Save ronzalo/9feffacdd3e62a7b90587103a693f904 to your computer and use it in GitHub Desktop.
Spree promotion rule for properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- admin/promotions/rules/_property.html.erb --> | |
<div class="panel-body"> | |
<div class="form-group property_rule_property_id"> | |
<%= label_tag "#{param_prefix}_property_id_string", Spree.t('property_rule.choose_property') %> | |
<%= select_tag "#{param_prefix}[preferred_property_id]", options_from_collection_for_select(Spree::Property.all, "id", "presentation", promotion_rule.preferred_property_id), class: 'select2' %> | |
<%= label_tag "#{param_prefix}_value_string", Spree.t('property_rule.insert_value') %> | |
<%= text_field_tag "#{param_prefix}[preferred_value]", promotion_rule.preferred_value, class: 'form-control' %> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/locales/es.yml | |
es: | |
spree: | |
promotion_rule_types: | |
property: | |
description: La orden incluye productos específicos con las propiedades correspondientes | |
name: Propiedades | |
property_rule: | |
choose_property: Seleccione propiedad | |
insert_value: Escriba valor para propiedad (nombre autor o editorial) | |
label: Orden debe contener una cantidad x de estas categorías |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Spree | |
class Promotion | |
module Rules | |
class Property < Spree::PromotionRule | |
preference :property_id, :integer | |
preference :value, :string | |
def applicable?(promotable) | |
promotable.is_a?(Spree::Order) | |
end | |
def eligible?(order, options = {}) | |
order.line_items.any? { |item| actionable?(item) } | |
end | |
def actionable?(line_item) | |
property = Spree::Property.find(preferred_property_id) | |
line_item.product.property(property.name).downcase == preferred_value.downcase | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment