1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
class EcommerceAlsoRecommendedDOD extends DataExtension |
6
|
|
|
{ |
7
|
|
|
private static $many_many = array( |
8
|
|
|
'EcommerceRecommendedProducts' => 'Product' |
9
|
|
|
); |
10
|
|
|
|
11
|
|
|
private static $belongs_many_many = array( |
12
|
|
|
'RecommendedFor' => 'Product' |
13
|
|
|
); |
14
|
|
|
|
15
|
|
|
public function updateCMSFields(FieldList $fields) |
16
|
|
|
{ |
17
|
|
|
if ($this->owner instanceof Product) { |
18
|
|
|
$fields->addFieldToTab( |
19
|
|
|
'Root.Links', |
20
|
|
|
GridField::create( |
21
|
|
|
'EcommerceRecommendedProducts', |
22
|
|
|
'Also Recommended Products', |
23
|
|
|
$this->owner->EcommerceRecommendedProducts(), |
24
|
|
|
$config = GridFieldBasicPageRelationConfig::create() |
25
|
|
|
) |
26
|
|
|
); |
27
|
|
|
$component = $config->getComponentByType('GridFieldAddExistingAutocompleter'); |
28
|
|
|
$component->setSearchFields(array("InternalItemID", "Title")); |
29
|
|
|
|
30
|
|
|
$fields->addFieldToTab( |
31
|
|
|
'Root.Links', |
32
|
|
|
GridField::create( |
33
|
|
|
'RecommendedFor', |
34
|
|
|
'Recommended For', |
35
|
|
|
$this->owner->RecommendedFor(), |
36
|
|
|
$config = GridFieldBasicPageRelationConfig::create() |
37
|
|
|
) |
38
|
|
|
); |
39
|
|
|
$component = $config->getComponentByType('GridFieldAddExistingAutocompleter'); |
40
|
|
|
$component->setSearchFields(array("InternalItemID", "Title")); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* small cleanup |
47
|
|
|
*/ |
48
|
|
|
public function onAfterWrite() |
49
|
|
|
{ |
50
|
|
|
$products = $this->owner->EcommerceRecommendedProducts(); |
51
|
|
View Code Duplication |
if ($products->count()) { |
|
|
|
|
52
|
|
|
foreach ($products as $product) { |
53
|
|
|
if (!$product instanceof Product) { |
54
|
|
|
$products->remove($product); |
55
|
|
|
} elseif (!$product->AllowPurchase) { |
56
|
|
|
$products->remove($product); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
$products = $this->owner->RecommendedFor(); |
61
|
|
View Code Duplication |
if ($products->count()) { |
|
|
|
|
62
|
|
|
foreach ($products as $product) { |
63
|
|
|
if (!$product instanceof Product) { |
64
|
|
|
$products->remove($product); |
65
|
|
|
} elseif (!$product->AllowPurchase) { |
66
|
|
|
$products->remove($product); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* only returns the products that are for sale |
74
|
|
|
* if only those need to be showing. |
75
|
|
|
* @return DataList |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function EcommerceRecommendedProductsForSale() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
if ($this->owner->EcomConfig()->OnlyShowProductsThatCanBePurchased) { |
80
|
|
|
return $this->owner->EcommerceRecommendedProducts()->filter(array("AllowPurchase" => 1)); |
81
|
|
|
} else { |
82
|
|
|
return $this->owner->EcommerceRecommendedProducts(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* only returns the products that are for sale |
88
|
|
|
* if only those need to be showing. |
89
|
|
|
* @return DataList |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function RecommendedForForSale() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
if ($this->owner->EcomConfig()->OnlyShowProductsThatCanBePurchased) { |
94
|
|
|
return $this->owner->RecommendedFor()->filter(array("AllowPurchase" => 1)); |
95
|
|
|
} else { |
96
|
|
|
return $this->owner->RecommendedFor(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.