-
Notifications
You must be signed in to change notification settings - Fork 0
Products
Summary :
- Making a model sellable
- Configuring a sellable model
- Allow customers to add products to the cart
- Customizing the different related views
With Glysellin, products can be integrated in any model that has a :name attribute.
All you need is just to include the acts_as_sellable macro in your model :
class Tshirt < ActiveRecord::Base
acts_as_sellable
endThis macro initializes the Sellable properties of your model, and creates the needed associations to store the products-related data into Glysellin models, so your own model isn't altered.
The most important additions it makes to your model, is :
- Adding a
has_one :productrelation toGlysellin::Product, which holds the global product configuration data : the VAT rate of the product and its brand. - Adding a
has_manyrelation toGlysellin::Variant, which will be the actual products that will be sold on your application. If you're new to the concept of variants, they are just the different versions of your product. For example, if your model is a T-Shirt, you can imagine variants to be the different sizes, so your customer can choose his size directly and buy it.
The concept of Variants is useful, but it is not always needed. Sometimes you just need one variant per product. In this case, you may want to use the :simple mode of the sellable mixin :
class Tshirt < ActiveRecord::Base
acts_as_sellable :simple => true
endThis mainly changes the relation between your model and the variants to a one-to-one relation this way :
has_one :variant.
The other thing worth to note is that the relation is now called :variant, and Glysellin just handles it. If you try to call #variants on the model, it'll just return your an ActiveRecord::Relation object containing the variant wrapped in an array.
If you're selling products that don't need stock management, for example, magazine subscriptions, you may want to use the :stock option :
class MagazineSubscription < ActiveRecord::Base
acts_as_sellable :stock => false
endIt will automatically ensure that your variants have the :unlimited_stock attribute set to true, so that the :in_stock attribute doesn't have to be greater that 0 for the product to be added to cart !
If you need to handle when a specific product is sold, for example, if a downloadable file has been bought, you can pass a callback to the sellable macro :
class MagazineIssue < ActiveRecord::Base
acts_as_sellable :sold => :on_sold
def on_sold order
create_download_link_for(order.customer)
end
def create_download_link
# ...
end
endThe argument of the :sold parameter can either be the name of an instance method, as shown above, or a Proc object :
acts_as_sellable :sold => ->(order) { ... }This callback is called when an order containing such a product type is paid by the customer.
When your products are created, you can sell your products by letting customers adding them to cart with the add_to_cart_form helper.
Let's say we have a sellable Tshirt model instance that we can access in our view with @tshirt, we can do the following :
<div class="product">
<h2><%= @tshirt.name %></h2>
<%= add_to_cart_form(@tshirt) %>
</div>Customers don't want to reload the page when they add the product to their cart.
You can avoid that by using the .glysellinCart() jQuery plugin provided by the gem.
First, you need to render the cart on your page :
<%= render_cart %>Then initialize the plugin :
//= require glysellin/cart
$(function() {
$('.cart-container').glysellinCart()
});Now, when you add a product to the cart, the cart partial is automatically refreshed and a pop up warns you that the product has successfully been added to cart.
You can customize the different views in the following files :
- The add_to_cart form :
views/glysellin/products/_add_to_cart - The cart container :
views/glysellin/cart/_cart - The warning modal :
views/glysellin/cart/_added_to_cart_warning