1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* a class to copy prices from country A to country |
4
|
|
|
* This can be added to Products, but also to Product Groups |
5
|
|
|
* and other pages ... |
6
|
|
|
* |
7
|
|
|
* |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
class CountryPrice_CopyPrices extends DataExtension |
11
|
|
|
{ |
12
|
|
|
private static $db = array( |
13
|
|
|
"AllowCopying" => "Boolean" |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
public function updateCMSFields(FieldList $fields) |
17
|
|
|
{ |
18
|
|
|
if ($this->owner->ID) { |
|
|
|
|
19
|
|
|
$page = is_a($this->owner, 'SiteTree'); // We use singleton to skip the different is_a php versions issues |
|
|
|
|
20
|
|
|
$tab = 'Root.Countries.Pricing'; |
21
|
|
|
$fromCountries = CountryPrice_EcommerceCountry::get_real_countries_list(); |
22
|
|
|
//->where('CountryPrice.ObjectClass = \''.$this->owner->ClassName.'\' AND CountryPrice.ObjectID = '.$this->owner->ID.'') |
23
|
|
|
if ($fromCountries && $fromCountries->count()) { |
24
|
|
|
$fromCountriesArray = $fromCountries->map('Code', 'Name')->toArray(); |
25
|
|
|
} else { |
26
|
|
|
$fromCountriesArray = array(); |
27
|
|
|
} |
28
|
|
|
$allCountries = EcommerceCountry::get(); |
29
|
|
|
$toCountries = array(); |
30
|
|
|
foreach ($allCountries as $country) { |
31
|
|
|
$country = CountryPrice_EcommerceCountry::get_real_country($country); |
32
|
|
|
$toCountries[$country->Code] = $country->Name . ($country->DoNotAllowSales ? ' (Sales not allowed)' : ''); |
33
|
|
|
} |
34
|
|
|
$countryCurrencies = CountryPrice_EcommerceCurrency::get_currency_per_country(); |
35
|
|
|
$link = CountryPrice_CopyPrices_Controller::get_link($this->owner); |
|
|
|
|
36
|
|
|
$fields->addFieldToTab( |
37
|
|
|
$tab, |
38
|
|
|
$allowCopyingField = new CheckboxField("AllowCopying", "Allow copying") |
39
|
|
|
); |
40
|
|
|
$allowCopyingField->setRightTitle("Turn this on only when you like to copy a bunch of prices. Otherwise just leave it turned off to avoid accidental copies and speed up the CMS loading times."); |
41
|
|
|
if (count($fromCountriesArray) && count($toCountries) && $this->owner->AllowCopying) { |
|
|
|
|
42
|
|
|
$fields->addFieldsToTab($tab, array( |
43
|
|
|
new HeaderField('Copy Prices'), |
44
|
|
|
new DropdownField('From', 'From', $fromCountriesArray), |
45
|
|
|
new CheckboxSetField('To', 'To', $toCountries), |
46
|
|
|
new HiddenField('CountryCurrencies', '', Convert::array2json($countryCurrencies)), |
47
|
|
|
new EcommerceCMSButtonField( |
48
|
|
|
'UpdatePriceLink', |
49
|
|
|
$link, |
50
|
|
|
'Copy Prices' |
51
|
|
|
) |
52
|
|
|
)); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* update all child buyables and the current buyable prices |
59
|
|
|
* based on $fromCountry and applied to ALL $toCountries |
60
|
|
|
* @param string $fromCountryCode the country code to copy from |
61
|
|
|
* @param array $toCountriesArray the country code to copy to |
62
|
|
|
*/ |
63
|
|
|
public function updatePrices($fromCountryCode, array $toCountriesArray) |
64
|
|
|
{ |
65
|
|
|
$fromCountryObject = EcommerceCountry::get()->filter(array("Code" => $fromCountryCode)); |
66
|
|
|
if ($fromCountryObject) { |
67
|
|
|
$fromCountryObject = CountryPrice_EcommerceCountry::get_real_country($fromCountryObject); |
68
|
|
|
} else { |
69
|
|
|
user_error('From Country is not valid'); |
70
|
|
|
} |
71
|
|
|
$currencyObject = $fromCountryObject->EcommerceCurrency(); |
72
|
|
|
if ($currencyObject && $currencyObject->Code) { |
73
|
|
|
$values = $this->getUpdatePriceValues($fromCountryCodeA, $currencyObject->Code, array()); |
|
|
|
|
74
|
|
|
foreach ($toCountriesArray as $toCountryCode) { |
75
|
|
|
$toCountryCode = CountryPrice_EcommerceCountry::get_real_country($toCountryCode)->Code; |
76
|
|
|
foreach ($values as $value) { |
77
|
|
|
$sqlValues[] = "(NOW(),NOW(),{$value[0]},'$toCountryCode','".$currencyObject->Code."','{$value[1]}',{$value[2]})"; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
if (isset($sqlValues)) { |
81
|
|
|
$sqlValues = implode(',', $sqlValues); |
82
|
|
|
$sql = " |
83
|
|
|
INSERT INTO \"CountryPrice\" (\"Created\",\"LastEdited\",\"Price\",\"Country\",\"Currency\",\"ObjectClass\",\"ObjectID\") |
84
|
|
|
VALUES $sqlValues |
85
|
|
|
ON DUPLICATE KEY |
86
|
|
|
UPDATE \"LastEdited\" = VALUES(\"LastEdited\"), \"Price\" = VALUES(\"Price\")"; |
87
|
|
|
DB::query($sql); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* returns an array of |
94
|
|
|
* - Price |
95
|
|
|
* - ClassName |
96
|
|
|
* - ID |
97
|
|
|
* searches through children, until all all childpages have been added |
98
|
|
|
* |
99
|
|
|
* @param string $country [description] |
|
|
|
|
100
|
|
|
* @param array $values [description] |
101
|
|
|
* @return array [description] |
102
|
|
|
*/ |
103
|
|
|
public function getUpdatePriceValues($fromCountryCode, $currencyCode, array $values) |
104
|
|
|
{ |
105
|
|
|
$fromCountryCode = CountryPrice_EcommerceCountry::get_real_country($fromCountryCode)->Code; |
106
|
|
|
if ($this->owner->hasExtension('CountryPrice_BuyableExtension')) { |
107
|
|
|
$countryPrice = $this->owner->CountryPriceForCountryAndCurrency($fromCountry, $currency); |
|
|
|
|
108
|
|
|
if ($countryPrice) { |
109
|
|
|
$price = $countryPrice->First()->Price; |
110
|
|
|
} else { |
111
|
|
|
$countryPrices = $this->owner->CountryPriceForCountryAndCurrency($fromCountryCode, $currencyCode, $values); |
112
|
|
|
if ($countryPrices && $countryPrices->count()) { |
113
|
|
|
$price = $countryPrices->First()->Price; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
if (isset($price)) { |
117
|
|
|
$values[] = array( |
118
|
|
|
$price, |
119
|
|
|
$this->owner->ClassName, |
|
|
|
|
120
|
|
|
$this->owner->ID |
|
|
|
|
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
View Code Duplication |
if ($this->owner->hasExtension('ProductWithVariationDecorator')) { |
|
|
|
|
125
|
|
|
$variations = $this->owner->Variations(); |
126
|
|
|
foreach ($variations as $variation) { |
127
|
|
|
$values = array_merge($values, $variation->getUpdatePriceValues($fromCountryCode, $currencyCode, $values)); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
View Code Duplication |
if (is_a($this->owner, 'SiteTree')) { |
|
|
|
|
131
|
|
|
$pages = $this->owner->AllChildren(); |
132
|
|
|
if ($pages) { |
133
|
|
|
foreach ($pages as $page) { |
134
|
|
|
$values = array_merge($values, $page->getUpdatePriceValues($fromCountryCode, $currencyCode, $values)); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
return $values; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.