Last active
May 18, 2020 07:17
-
-
Save untwal/b0086d766fcdf31d58325e6dda124722 to your computer and use it in GitHub Desktop.
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
shipping_rules = [ | |
{ | |
brand_name: 'Bella + Canvas', | |
free_shipping_threshold: 200, | |
shipping_fees: { | |
fastest: 9.95, | |
standard: 0.00, | |
economy: 5.45 | |
}, | |
discount_coupon_allowed: false, | |
free_shipping_coupon_allowed: false, | |
counts_towards_bulk_discounts: true, | |
cancellable: false, | |
returnable: false, | |
catalog_numbers: Spree::Product::FACEMASKS_CATALOG_NUMBERS | |
}, | |
{ | |
brand_name: 'Bayside', | |
free_shipping_threshold: 200, | |
shipping_fees: { | |
fastest: 9.95, | |
standard: 0.00, | |
economy: 5.45 | |
}, | |
discount_coupon_allowed: false, | |
free_shipping_coupon_allowed: false, | |
counts_towards_bulk_discounts: true, | |
cancellable: false, | |
returnable: false, | |
catalog_numbers: Spree::Product::FACEMASKS_CATALOG_NUMBERS | |
}, | |
{ | |
brand_name: 'LAT', | |
free_shipping_threshold: 200, | |
shipping_fees: { | |
fastest: 9.95, | |
standard: 7.45, | |
economy: 5.45 | |
}, | |
discount_coupon_allowed: false, | |
free_shipping_coupon_allowed: false, | |
counts_towards_bulk_discounts: true, | |
cancellable: false, | |
returnable: false, | |
catalog_numbers: Spree::Product::FACEMASKS_CATALOG_NUMBERS | |
}, | |
{ | |
brand_name: 'Colortone', | |
free_shipping_threshold: 200, | |
shipping_fees: { | |
fastest: 9.95, | |
standard: 7.45, | |
economy: 5.45 | |
}, | |
discount_coupon_allowed: false, | |
free_shipping_coupon_allowed: false, | |
counts_towards_bulk_discounts: true, | |
cancellable: false, | |
returnable: false, | |
catalog_numbers: Spree::Product::FACEMASKS_CATALOG_NUMBERS | |
}, | |
{ | |
brand_name: 'Port & Company', | |
free_shipping_threshold: 200, | |
shipping_fees: { | |
fastest: 9.95, | |
standard: 7.45, | |
economy: 5.45 | |
}, | |
discount_coupon_allowed: false, | |
free_shipping_coupon_allowed: false, | |
counts_towards_bulk_discounts: true, | |
cancellable: false, | |
returnable: false, | |
catalog_numbers: Spree::Product::FACEMASKS_CATALOG_NUMBERS | |
}, | |
{ | |
brand_name: 'Port Authority', | |
free_shipping_threshold: 200, | |
shipping_fees: { | |
fastest: 9.95, | |
standard: 7.45, | |
economy: 5.45 | |
}, | |
discount_coupon_allowed: false, | |
free_shipping_coupon_allowed: false, | |
counts_towards_bulk_discounts: true, | |
cancellable: false, | |
returnable: false, | |
catalog_numbers: Spree::Product::FACEMASKS_CATALOG_NUMBERS | |
}, | |
{ | |
brand_name: 'Delta', | |
free_shipping_threshold: 200, | |
shipping_fees: { | |
fastest: 9.95, | |
standard: 7.45, | |
economy: 5.45 | |
}, | |
discount_coupon_allowed: false, | |
free_shipping_coupon_allowed: false, | |
counts_towards_bulk_discounts: true, | |
cancellable: false, | |
returnable: false, | |
catalog_numbers: Spree::Product::FACEMASKS_CATALOG_NUMBERS | |
}, | |
{ | |
brand_name: 'Tultex', | |
free_shipping_threshold: 200, | |
shipping_fees: { | |
fastest: 9.95, | |
standard: 7.45, | |
economy: 5.45 | |
}, | |
discount_coupon_allowed: false, | |
free_shipping_coupon_allowed: false, | |
counts_towards_bulk_discounts: true, | |
cancellable: false, | |
returnable: false, | |
catalog_numbers: Spree::Product::FACEMASKS_CATALOG_NUMBERS | |
}, | |
{ | |
brand_name: 'E+P', | |
free_shipping_threshold: 200, | |
shipping_fees: { | |
fastest: 9.95, | |
standard: 7.45, | |
economy: 5.45 | |
}, | |
discount_coupon_allowed: false, | |
free_shipping_coupon_allowed: false, | |
counts_towards_bulk_discounts: true, | |
cancellable: false, | |
returnable: false, | |
catalog_numbers: Spree::Product::FACEMASKS_CATALOG_NUMBERS | |
}, | |
{ | |
brand_name: 'Rabbit Skins', | |
free_shipping_threshold: 200, | |
shipping_fees: { | |
fastest: 9.95, | |
standard: 7.45, | |
economy: 5.45 | |
}, | |
discount_coupon_allowed: false, | |
free_shipping_coupon_allowed: false, | |
counts_towards_bulk_discounts: true, | |
cancellable: false, | |
returnable: false, | |
catalog_numbers: Spree::Product::FACEMASKS_CATALOG_NUMBERS | |
} | |
] | |
brand_names = shipping_rules.map { |shipping_rules| shipping_rules[:brand_name] } | |
brands_mapped_by_names = Spree::Brand.where(name: brand_names).group_by(&:name) | |
face_mask_brand_names = Spree::Product.where(catalog_number: Spree::Product::FACEMASKS_CATALOG_NUMBERS). | |
joins(:brand). | |
pluck("DISTINCT(spree_brands.name)") | |
def shipping_rule_name(brand_name) | |
'%{brand_name} Special Order' % {brand_name: brand_name} | |
end | |
def shipping_title(brand_name) | |
'%{brand_name} Shipping' % {brand_name: brand_name} | |
end | |
def shipping_note(face_mask_brand) | |
if face_mask_brand | |
'● All sales are final. ● Free Shipping coupons do not apply. ● Not counted towards other free shipping promotions.' | |
else | |
'● Free Shipping coupons do not apply. ● Not counted towards other free shipping promotions.' | |
end | |
end | |
def shipping_note_summary(brand_name, fee, free_shipping_threshold) | |
'Ships for $%{fee}. %{brand_name} Special Order items Ship for free at $%{free_shipping_threshold}.' % { fee: fee, brand_name: brand_name, free_shipping_threshold: free_shipping_threshold } | |
end | |
def update_store_objects(spree_brand, catalog_numbers, store, shipping_rule_id) | |
store_products = StoreProduct. | |
joins(:spree_product). | |
where(spree_store: store). | |
where(spree_products: { brand_id: spree_brand.id }) | |
if catalog_numbers.present? | |
store_products = store_products.where(spree_products: {catalog_number: catalog_numbers}) | |
end | |
store_products.update_all(shipping_rule_id: shipping_rule_id) | |
StoreVariant.where(store_product: store_products, spree_store: store).update_all(shipping_rule_id: shipping_rule_id) | |
end | |
def create_or_update_shipping_rule(attributes, catalog_numbers, store=Spree::Store.usa) | |
name = attributes.delete(:name) | |
shipping_rule = ShippingRule.find_or_create_by(name: name) | |
shipping_rule.assign_attributes(attributes) | |
shipping_rule.save | |
update_store_objects(attributes[:spree_brand], catalog_numbers, store, shipping_rule.id) | |
end | |
# @param [Hash<String, Float>] | |
def remap_shipping_fees(shipping_fees) | |
shipping_method_remap = { | |
fastest: ['express_post', 'fedex_ground'], | |
standard: ['sure_post', 'usps_priority_mail'], | |
economy: ['usps_first_class'] | |
} | |
mapped_shipping_fees = {} | |
shipping_method_remap.each do |exposed_shipping_method, backend_shipping_methods| | |
fee = shipping_fees[exposed_shipping_method] | |
if fee.present? | |
backend_shipping_methods.each do |backend_shipping_method| | |
mapped_shipping_fees[backend_shipping_method] = fee | |
end | |
end | |
end | |
mapped_shipping_fees | |
end | |
shipping_rules.each do |shipping_rule| | |
brand_name = shipping_rule.delete(:brand_name) | |
shipping_rule[:spree_brand] = brands_mapped_by_names[brand_name].first | |
fee = shipping_rule[:shipping_fees][:fastest] | |
shipping_rule[:shipping_fees] = remap_shipping_fees( | |
shipping_rule.delete(:shipping_fees) | |
) | |
shipping_rule[:name] = shipping_rule_name(brand_name) | |
shipping_rule[:shipping_title] = shipping_title(brand_name) | |
shipping_rule[:note] = shipping_note(face_mask_brand_names.include?(brand_name)) | |
shipping_rule[:note_summary] = shipping_note_summary( | |
brand_name, fee, shipping_rule[:free_shipping_threshold] | |
) | |
create_or_update_shipping_rule(shipping_rule, shipping_rule.delete(:catalog_numbers)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment