इस समस्या को हल करने के लिए मैंने यही किया है। यह मूल रूप से variant_id
पैरामीटर लेता है जिसे रेडियो बटन द्वारा नियंत्रित किया गया था और इसे अतिरिक्त अधिसूचनाओं के साथ jQuery और AJAX द्वारा नियंत्रित एक छिपे हुए क्षेत्र में बदल देता है।
मुझे आशा है कि इससे किसी की मदद मिलेगी।
config/routes.rb
# Mount the core routes
Rails.application.routes.draw do
mount Spree::Core::Engine, at: '/'
end
# Create new routes
Spree::Core::Engine.routes.draw do
post "products/:product_id/get_variant",
to: "products#toggle_like",
as: "get_variant",
constraints: { :format => /(js)/ }
end
एप्लिकेशन/मॉडल/होड़/product_decorator.rb
Spree::Product.class_eval do
# Find the Product's Variant from an array of OptionValue ids
def find_variant_by_options(array)
option_values = Spree::OptionValue.where(id: array)
variants = []
option_values.each do |option_value|
variants.push(option_value.variants.ids)
end
self.variants.find_by(:id => variants.inject(:&).first)
end
end
एप्लिकेशन/नियंत्रक/होड़/products_controller_decorator.rb
Spree::ProductsController.class_eval do
# Get the Variant from params[:ids], respond with JavaScript
def get_variant
@product = Spree::Product.find_by :slug => params[:product_id]
@variant = @product.find_variant_by_options(params[:ids].split(','))
respond_to do |format|
format.js
end
end
end
एप्लिकेशन/विचारों/होड़/उत्पादों/get_variant.js.erb
// Update hidden field #varient_id's value.
$("#variant_id").val("<%= @variant.id %>")
// Update price
$(".price.selling").html("<%= number_to_currency @variant.price %>");
<% if @variant.in_stock? && @variant.available? %>
// If in stock and available
$("#add-to-cart-button").prop("disabled", false); // Enable button
$(".out-of-stock").hide(); // Hide 'out of stock' message
<% else %>
// Otherwise
$("#add-to-cart-button").prop("disabled", true); // Disable button
$(".out-of-stock").show(); // Show 'out of stock' message
<% end %>
एप्लिकेशन/विचारों/होड़/उत्पादों/_cart_form.html.erb के लिए
<%= form_for order, url: populates_orders_path do |f| %>
...
<% if @product.variants_and_option_values(current_currency).any? %>
<div id="product_variants" class="col-md-6">
<h3 class="product-section-title"><%= Spree.t(:variants) %></h3>
<% @product.option_types.each do |option_type| %>
<%= f.label "option_type_#{option_type.id}", option_type.name %>
<br>
<%= f.select "option_type_value_#{option_type.id}",
option_type.option_values.all.collect { |v| [ v.name, v.id ] },
{ include_blank: true },
{ class: "form-control" } %>
<br>
<% end %>
<%= hidden_field_tag "variant_id", value: "0" %>
...
</div>
<% else %>
<%= hidden_field_tag "variant_id", @product.master.id %>
<% end %>
...
<span class="price selling"
itemprop="price"
content="<%= @product.price_in(current_currency).amount.to_d %>">
<%= display_price(@product) %>
</span>
...
<%= button_tag class: "btn btn-success",
id: "add-to-cart-button",
disabled: @product.variants.any?,
type: :submit do %>
<%= Spree.t(:add_to_cart) %>
<% end %>
...
<span class="out-of-stock" style="display: none;">
<%= Spree.(:out_of_stock) %>
</span>
<% end %>
<script>
// Call AJAX if all options are selected, otherwise clean up.
$("#product-variants select").change(function(){
var option_value_ids = [];
$("#product-variants select").each(function(){
option_value_ids.push($(this).val());
});
if(option_value_ids.indexOf("") == -1){
$.ajax({
url: "<%= get_variant_path(@product) %>?ids=" + option_value_ids.join(','),
method: "post"
});
}else{
$("#variant_id").val("0");
$("#add-to-cart-button").prop("disabled", true);
$(".out-of-stock").hide();
}
});
</script>
धन्यवाद प्रतिक्रिया फैबियन। यह मुश्किल लग रहा है। मैं विशेष रूप से ड्रॉपडाउन बक्से देख रहा था। शायद, एक जावास्क्रिप्ट समाधान रेडियो हो सकता है, रेडियो छुपा। –
रेडियो को आसानी से चयन में परिवर्तित किया जा सकता है। इसकी एक ही तरह की चीज, किसी संदर्भ में विकल्पों की सरणी से एक विकल्प चुनें। मैंने इसे पहले एसपीए में कार्यान्वित किया है और मैंने एपीआई का उपयोग किसी उत्पाद से सभी प्रकारों को प्राप्त करने के लिए किया था, फिर उन संस्करणों के लिए जहां 'in_stock: true' मैंने विकल्प मान एकत्र किए थे और उपयोगकर्ता को केवल उन विकल्प_वॉल प्रदर्शित किए थे। या शायद स्टॉक वेरिएंट्स –
के विकल्पों के लिए विकल्पों को भूरे रंग से निकाल दें, फिर से धन्यवाद @ फ़ैबियन डी पाबियन, मुझे इसे पचाने और इसे कार्यान्वित करने के तरीके को समझने की आवश्यकता होगी। मैं बस रेडियो को ड्रॉपडाउन में परिवर्तित कर सकता हूं लेकिन इसमें रंग के लिए पृथक ड्रॉपडाउन की बजाय एक ड्रॉपडाउन पर प्रति उत्पाद विकल्प विकल्प हैं; आकार; आदि .. –