Class: EPlat::Woocommerce::Product::Variant

Inherits:
Product::Variant show all
Defined in:
lib/e_plat/resource/platform_specific/woocommerce/product/variant.rb,
lib/e_plat/resource/platform_specific/woocommerce/product/variant/option_value.rb

Defined Under Namespace

Classes: OptionValue

Constant Summary

Constants inherited from Base

Base::PROTECTED_ATTRIBUTES

Constants included from Concerns::GraphQLable

Concerns::GraphQLable::FILTER_ARGS, Concerns::GraphQLable::QUERY_ARG_ARGS

Instance Attribute Summary collapse

Attributes inherited from Base

#mapped_attributes, #mapping

Attributes included from Concerns::Aliases

#type_coercer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Product::Variant

#image_src

Methods inherited from Base

cached_shopify_webhook?, client, #client, element_name_plural?, exclude_from_json, #formatted_id, #graphql_input, #headers, #include_root_in_json, inherited, initialize_singleton!, #mapped?, #native_keys, platform_specific_class?, prefix=, #read_only?, #string_to_consistent_id

Methods included from Countable

#count

Methods included from Concerns::GraphQLable

#graphql_mutation_string, #mutate_graphql, #remove_mutation_root_from

Methods included from Concerns::Aliases

#add_aliases!

Methods included from Concerns::FullJson

#as_full_json, #to_full_json

Methods included from Concerns::OverwriteRequestMethods

#collection_path, #element_path, included

Methods included from Concerns::OverwriteInstanceMethods

#as_eplat_json, #as_json, #create, #create_resource_for, #to_eplat_json, #to_json, #update

Constructor Details

#initialize(attributes = {}, persisted = false) ⇒ Variant

Returns a new instance of Variant.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/e_plat/resource/platform_specific/woocommerce/product/variant.rb', line 20

def initialize(attributes = {}, persisted = false)
	super
	
	# Ensure option_values have stable IDs based on name and option value combined
	if self.option_values.present?
		self.option_values.each do |option_value|
			if (option_value.attributes["id"].nil? || option_value.attributes["id"] == 0) && option_value.name.present?
				# Generate a unique ID based on both name and option value
				# This ensures options with the same name but different values get different IDs
				name_option_key = "#{option_value.name}_#{option_value.option}"
				option_value.attributes["id"] = string_to_consistent_id(name_option_key)
			end
		end
	end
end

Instance Attribute Details

#_is_virtualObject Also known as: new?, virtual?

Returns the value of attribute _is_virtual.



7
8
9
# File 'lib/e_plat/resource/platform_specific/woocommerce/product/variant.rb', line 7

def _is_virtual
  @_is_virtual
end

Class Method Details

.generate_virtual_variant(attributes) ⇒ Object



12
13
14
15
16
17
# File 'lib/e_plat/resource/platform_specific/woocommerce/product/variant.rb', line 12

def generate_virtual_variant(attributes)
	instance = new attributes
	instance._is_virtual = true
	instance.id = 0
	instance
end

Instance Method Details

#delete(id, options = {}) ⇒ Object



61
62
63
64
# File 'lib/e_plat/resource/platform_specific/woocommerce/product/variant.rb', line 61

def delete(id, options = {})
	raise "EPlat: Can not delete _is_virtual variant" if _is_virtual
	super
end

#exists?(id, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/e_plat/resource/platform_specific/woocommerce/product/variant.rb', line 66

def exists?(id, options = {})
	(_is_virtual) ? false : super
end

#saveObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/e_plat/resource/platform_specific/woocommerce/product/variant.rb', line 36

def save
	if _is_virtual
		EPlat.config.mute_woocommerce_variant_warnings || warn("updating product as this is a non-variant product. this variant is generated for interface purposes only")

		pass_through_attributes = {}
		product.common_product_variant_attributes.each do |attr|
			next unless changed_attributes.key? attr
			pass_through_attributes[attr] = send(attr)
		end

		pass_through_attributes = mapping.via_native_attributes_where_possible(pass_through_attributes)
		pass_through_attributes.each do |attr, value|
			product.send("#{attr}=", value)
		end

		if product.type == "variable" and product.variants.empty?
			EPlat.config.mute_woocommerce_variant_warnings || warn("Variable product with no variants found, attributes like price can't be set without variants")
		end

		product.save
	else 
		super
	end
end