1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: class Mage_SalesRule_Model_Rule_Condition_Address extends Mage_Rule_Model_Condition_Abstract
29: {
30: public function loadAttributeOptions()
31: {
32: $attributes = array(
33: 'base_subtotal' => Mage::helper('salesrule')->__('Subtotal'),
34: 'total_qty' => Mage::helper('salesrule')->__('Total Items Quantity'),
35: 'weight' => Mage::helper('salesrule')->__('Total Weight'),
36: 'payment_method' => Mage::helper('salesrule')->__('Payment Method'),
37: 'shipping_method' => Mage::helper('salesrule')->__('Shipping Method'),
38: 'postcode' => Mage::helper('salesrule')->__('Shipping Postcode'),
39: 'region' => Mage::helper('salesrule')->__('Shipping Region'),
40: 'region_id' => Mage::helper('salesrule')->__('Shipping State/Province'),
41: 'country_id' => Mage::helper('salesrule')->__('Shipping Country'),
42: );
43:
44: $this->setAttributeOption($attributes);
45:
46: return $this;
47: }
48:
49: public function getAttributeElement()
50: {
51: $element = parent::getAttributeElement();
52: $element->setShowAsText(true);
53: return $element;
54: }
55:
56: public function getInputType()
57: {
58: switch ($this->getAttribute()) {
59: case 'base_subtotal': case 'weight': case 'total_qty':
60: return 'numeric';
61:
62: case 'shipping_method': case 'payment_method': case 'country_id': case 'region_id':
63: return 'select';
64: }
65: return 'string';
66: }
67:
68: public function getValueElementType()
69: {
70: switch ($this->getAttribute()) {
71: case 'shipping_method': case 'payment_method': case 'country_id': case 'region_id':
72: return 'select';
73: }
74: return 'text';
75: }
76:
77: public function getValueSelectOptions()
78: {
79: if (!$this->hasData('value_select_options')) {
80: switch ($this->getAttribute()) {
81: case 'country_id':
82: $options = Mage::getModel('adminhtml/system_config_source_country')
83: ->toOptionArray();
84: break;
85:
86: case 'region_id':
87: $options = Mage::getModel('adminhtml/system_config_source_allregion')
88: ->toOptionArray();
89: break;
90:
91: case 'shipping_method':
92: $options = Mage::getModel('adminhtml/system_config_source_shipping_allmethods')
93: ->toOptionArray();
94: break;
95:
96: case 'payment_method':
97: $options = Mage::getModel('adminhtml/system_config_source_payment_allmethods')
98: ->toOptionArray();
99: break;
100:
101: default:
102: $options = array();
103: }
104: $this->setData('value_select_options', $options);
105: }
106: return $this->getData('value_select_options');
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function validate(Varien_Object $object)
116: {
117: $address = $object;
118: if (!$address instanceof Mage_Sales_Model_Quote_Address) {
119: if ($object->getQuote()->isVirtual()) {
120: $address = $object->getQuote()->getBillingAddress();
121: }
122: else {
123: $address = $object->getQuote()->getShippingAddress();
124: }
125: }
126:
127: if ('payment_method' == $this->getAttribute() && ! $address->hasPaymentMethod()) {
128: $address->setPaymentMethod($object->getQuote()->getPayment()->getMethod());
129: }
130:
131: return parent::validate($address);
132: }
133: }
134: