-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultisiteVariants.php
More file actions
executable file
·193 lines (162 loc) · 6.39 KB
/
Copy pathMultisiteVariants.php
File metadata and controls
executable file
·193 lines (162 loc) · 6.39 KB
1
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* Multisite Variants plugin for Craft CMS 3.x
*
* Enable or disable variants based on site
*
* @link https://webdna.co.uk
* @copyright Copyright (c) 2021 Web DNA
*/
namespace webdna\multisitevariants;
use webdna\multisitevariants\services\MultisiteVariantsService as MultisiteVariantsServiceService;
use webdna\multisitevariants\behaviors\MultisiteVariantBehavior;
use Craft;
use craft\base\Plugin;
use craft\base\Element;
use craft\elements\db\ElementQuery;
use craft\events\DefineBehaviorsEvent;
use craft\events\DefineRulesEvent;
use craft\events\ModelEvent;
use craft\events\PluginEvent;
use craft\helpers\ElementHelper;
use craft\services\Plugins;
use craft\commerce\elements\Variant;
use craft\commerce\elements\Order;
use craft\commerce\models\LineItem;
use yii\base\Event;
/**
* Class MultisiteVariants
*
* @author Web DNA
* @package MultisiteVariants
* @since 1.0.0
*
* @property MultisiteVariantsServiceService $multisiteVariantsService
*/
class MultisiteVariants extends Plugin
{
// Static Properties
// =========================================================================
/**
* @var MultisiteVariants
*/
public static $plugin;
// Public Properties
// =========================================================================
/**
* @var string
*/
public string $schemaVersion = '1.0.0';
/**
* @var bool
*/
public bool $hasCpSettings = false;
/**
* @var bool
*/
public bool $hasCpSection = false;
// Public Methods
// =========================================================================
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
self::$plugin = $this;
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
function (PluginEvent $event) {
if ($event->plugin === $this) {
}
}
);
$this->setComponents([
'service' => MultisiteVariantsServiceService::class
]);
Event::on(
LineItem::class,
LineItem::EVENT_DEFINE_RULES,
function(DefineRulesEvent $event) {
$lineItem = $event->sender;
$variant = $lineItem->purchasable;
$qtyRuleIndex = null;
foreach ($event->rules as $key => $value) {
if ($value[0] == 'qty' ?? null) {
$qtyRuleIndex = $key;
}
}
// Remove stock check rule if item has site stock
if($variant->hasSiteStock()) {
unset($event->rules[$qtyRuleIndex]);
}
}
);
Event::on(
Variant::class,
Variant::EVENT_DEFINE_BEHAVIORS,
function(DefineBehaviorsEvent $event) {
$event->sender->attachBehaviors([
MultisiteVariantBehavior::class,
]);
}
);
// create the stock records before the variant is saved to pass the correct total stock
Event::on(Variant::class, Element::EVENT_BEFORE_SAVE, function(ModelEvent $e) {
$request = Craft::$app->getRequest();
// This is only for CP saves, need to exclude queue jobs
if ($request->getIsCpRequest() && $request->getBodyParam('variants')) {
$variant = $e->sender;
$postData = $request->getBodyParam('variants');
if (array_key_exists($variant->id, $postData)) {
// Need to think about how to save stock for new variant, other than wait for first load
$variantData = $request->getBodyParam('variants')[$variant->id];
$siteId = (int)$request->getBodyParam('siteId');
$stock = (int)(array_key_exists('stock',$variantData) ? $variantData['stock'] : 0); //should this be zero just because its missing from the POST?
$unlimited = (bool)(array_key_exists('hasUnlimitedStock',$variantData) ? $variantData['hasUnlimitedStock'] : false);
$this->service->saveVariantSiteStock($variant->id, $stock, $unlimited, $siteId);
// $variant->stock = $variant->getTotalStock(); -- separate total stock from site stock for the time being
$e->sender = $variant;
}
}
});
// update the site settings after variant save to make sure there is an existing site settings record.
Event::on(Variant::class, Element::EVENT_AFTER_SAVE, function(ModelEvent $e) {
$request = Craft::$app->getRequest();
if ($request->getIsCpRequest() && $request->getBodyParam('variants')) {
$variant = $e->sender;
$postData = $request->getBodyParam('variants');
if (array_key_exists($variant->id, $postData)) {
$siteId = (int)$request->getBodyParam('siteId');
$variantData = $request->getBodyParam('variants')[$variant->id];
$this->service->saveVariantSiteSettings($variant->id, [$siteId => (bool)$variantData['enabledForSite']]);
}
}
});
// Update the site stock to match the reduced total stock on order complete
Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, function(Event $e) {
// $order = $e->sender;
// foreach ($order->lineItems as $lineItem) {
// $this->service->afterOrderComplete($lineItem->getPurchasable(), $lineItem->qty);
// }
});
// Make sure the rows are deleted on variant deletion, cascade should take care of this
Event::on(Variant::class, Element::EVENT_AFTER_DELETE, function(Event $e) {
$this->service->deleteVariantSiteStock($e->sender->id);
});
if (Craft::$app->getRequest()->getIsCpRequest()) {
Craft::$app->getView()->hook('cp.commerce.product.edit.details', [$this->service, 'addVariantEnabledFields']);
}
Craft::info(
Craft::t(
'multisite-variants',
'{name} plugin loaded',
['name' => $this->name]
),
__METHOD__
);
}
// Protected Methods
// =========================================================================
}