Skip to content

Commit 91603be

Browse files
author
Josh Smith
committed
First implementation of the pricing matrix plugin.
1 parent 0c29844 commit 91603be

14 files changed

+397
-217
lines changed

.craftplugin

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"pluginName":"Craft Commerce Pricing Matrix","pluginDescription":"Craft Commerce Pricing Matrices. Adds a custom field type that can be used to upload a pricing matrix for products in a CSV format.","pluginVersion":"1.0.0","pluginAuthorName":"Josh Smith <[email protected]>","pluginVendorName":"platocreative","pluginAuthorUrl":"https://www.platocreative.co.nz/","pluginAuthorGithub":"platocreative","codeComments":"yes","pluginComponents":["controllers","fieldtypes","models","records","services"],"consolecommandName":"","controllerName":"pricingmatrix","cpsectionName":"","elementName":"","fieldName":"pricingmatrix","modelName":"pricingmatrix","purchasableName":"","recordName":"pricingmatrix","serviceName":"pricingmatrix","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
1+
{"pluginName":"Pricing Matrix for Craft Commerce","pluginDescription":"Craft Commerce Pricing Matrices. Adds a custom field type that can be used to upload a pricing matrix for products in a CSV format.","pluginVersion":"1.0.0","pluginAuthorName":"Josh Smith <[email protected]>","pluginVendorName":"platocreative","pluginAuthorUrl":"https://www.platocreative.co.nz/","pluginAuthorGithub":"platocreative","codeComments":"yes","pluginComponents":["controllers","fieldtypes","models","records","services"],"consolecommandName":"","controllerName":"pricingmatrix","cpsectionName":"","elementName":"","fieldName":"pricingmatrix","modelName":"pricingmatrix","purchasableName":"","recordName":"pricingmatrix","serviceName":"pricingmatrix","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
}
3131
},
3232
"extra": {
33-
"name": "Craft Commerce Pricing Matrix",
33+
"name": "Pricing Matrix for Craft Commerce",
3434
"handle": "craft-commerce-pricing-matrix",
3535
"hasCpSettings": false,
3636
"hasCpSection": false,

resources/img/plugin-logo.png

17.3 KB
Loading

src/CraftCommercePricingMatrix.php

+77-16
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@
1212

1313
use platocreative\craftcommercepricingmatrix\services\Pricingmatrix as PricingmatrixService;
1414
use platocreative\craftcommercepricingmatrix\fields\Pricingmatrix as PricingmatrixField;
15+
use platocreative\craftcommercepricingmatrix\queue\SavePricingMatrixJob;
16+
use craft\commerce\services\Variants;
17+
use craft\commerce\events\LineItemEvent;
18+
use craft\commerce\services\LineItems;
19+
use craft\commerce\elements\Product;
1520

1621
use Craft;
1722
use craft\base\Plugin;
23+
use craft\services\Elements;
1824
use craft\services\Plugins;
1925
use craft\events\PluginEvent;
26+
use craft\helpers\Assets;
2027
use craft\web\UrlManager;
2128
use craft\services\Fields;
2229
use craft\events\RegisterComponentTypesEvent;
2330
use craft\events\RegisterUrlRulesEvent;
31+
use craft\events\RegisterAssetFileKindsEvent;
2432

2533
use yii\base\Event;
2634

@@ -82,24 +90,64 @@ public function init()
8290
parent::init();
8391
self::$plugin = $this;
8492

85-
// Register our site routes
93+
// // Register our site routes
94+
// Event::on(
95+
// UrlManager::class,
96+
// UrlManager::EVENT_REGISTER_SITE_URL_RULES,
97+
// function (RegisterUrlRulesEvent $event) {
98+
// $event->rules['siteActionTrigger1'] = 'craft-commerce-pricing-matrix/pricingmatrix';
99+
// }
100+
// );
101+
102+
// Register CSV files as an allowed file type
86103
Event::on(
87-
UrlManager::class,
88-
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
89-
function (RegisterUrlRulesEvent $event) {
90-
$event->rules['siteActionTrigger1'] = 'craft-commerce-pricing-matrix/pricingmatrix';
104+
Assets::class,
105+
Assets::EVENT_REGISTER_FILE_KINDS,
106+
function (RegisterAssetFileKindsEvent $event) {
107+
$event->fileKinds['csv'] = [
108+
'label' => 'CSV',
109+
'extensions' => ['csv']
110+
];
91111
}
92112
);
93113

94-
// Register our CP routes
114+
// Register a line item population event to handle pricing matrix lookups.
95115
Event::on(
96-
UrlManager::class,
97-
UrlManager::EVENT_REGISTER_CP_URL_RULES,
98-
function (RegisterUrlRulesEvent $event) {
99-
$event->rules['cpActionTrigger1'] = 'craft-commerce-pricing-matrix/pricingmatrix/do-something';
116+
LineItems::class,
117+
LineItems::EVENT_POPULATE_LINE_ITEM,
118+
function(LineItemEvent $e) {
119+
120+
$lineItem = $e->lineItem;
121+
$snapshot = $lineItem->snapshot;
122+
$options = $snapshot['options'];
123+
124+
// Check this product has a pricing matrix associated with it, then get the product price
125+
if( self::$plugin->pricingmatrix->hasPricingMatrix($snapshot['productId']) ){
126+
127+
// Fetch the product record
128+
$record = self::$plugin->pricingmatrix->getProductPriceRecord(
129+
$snapshot['productId'], $options['width'], $options['height']
130+
);
131+
132+
// Update line item properies
133+
$lineItem->price = $record->price;
134+
$lineItem->height = $record->height;
135+
$lineItem->width = $record->width;
136+
}
137+
138+
// Check if product is on sale here, and set on sale price
100139
}
101140
);
102141

142+
// // Register our CP routes
143+
// Event::on(
144+
// UrlManager::class,
145+
// UrlManager::EVENT_REGISTER_CP_URL_RULES,
146+
// function (RegisterUrlRulesEvent $event) {
147+
// $event->rules['cpActionTrigger1'] = 'craft-commerce-pricing-matrix/pricingmatrix/do-something';
148+
// }
149+
// );
150+
103151
// Register our fields
104152
Event::on(
105153
Fields::class,
@@ -109,17 +157,30 @@ function (RegisterComponentTypesEvent $event) {
109157
}
110158
);
111159

112-
// Do something after we're installed
160+
// Detect when commerce products are saved and save the pricing matrix
113161
Event::on(
114-
Plugins::class,
115-
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
116-
function (PluginEvent $event) {
117-
if ($event->plugin === $this) {
118-
// We were just installed
162+
Elements::class,
163+
Elements::EVENT_AFTER_SAVE_ELEMENT,
164+
function(Event $event) {
165+
if( is_a($event->element, "craft\\commerce\\elements\\Variant") ) {
166+
Craft::$app->queue->push(new SavePricingMatrixJob([
167+
'variantId' => $event->element->id,
168+
]));
119169
}
120170
}
121171
);
122172

173+
// // Do something after we're installed
174+
// Event::on(
175+
// Plugins::class,
176+
// Plugins::EVENT_AFTER_INSTALL_PLUGIN,
177+
// function (PluginEvent $event) {
178+
// if ($event->plugin === $this) {
179+
// // We were just installed
180+
// }
181+
// }
182+
// );
183+
123184
/**
124185
* Logging in Craft involves using one of the following methods:
125186
*

src/fields/Pricingmatrix.php

+22-42
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
use Craft;
1717
use craft\base\ElementInterface;
1818
use craft\base\Field;
19+
use craft\fields\Assets;
1920
use craft\helpers\Db;
2021
use yii\db\Schema;
2122
use craft\helpers\Json;
23+
use craft\elements\Asset as AssetElement;
2224

2325
/**
2426
* Pricingmatrix Field
@@ -33,18 +35,11 @@
3335
* @package CraftCommercePricingMatrix
3436
* @since 1.0.0
3537
*/
36-
class Pricingmatrix extends Field
38+
class Pricingmatrix extends Assets
3739
{
3840
// Public Properties
3941
// =========================================================================
4042

41-
/**
42-
* Some attribute
43-
*
44-
* @var string
45-
*/
46-
public $someAttribute = 'Some Default';
47-
4843
// Static Methods
4944
// =========================================================================
5045

@@ -55,7 +50,7 @@ class Pricingmatrix extends Field
5550
*/
5651
public static function displayName(): string
5752
{
58-
return Craft::t('craft-commerce-pricing-matrix', 'Pricingmatrix');
53+
return Craft::t('craft-commerce-pricing-matrix', 'Pricing Matrix');
5954
}
6055

6156
// Public Methods
@@ -73,12 +68,7 @@ public static function displayName(): string
7368
*/
7469
public function rules()
7570
{
76-
$rules = parent::rules();
77-
$rules = array_merge($rules, [
78-
['someAttribute', 'string'],
79-
['someAttribute', 'default', 'value' => 'Some Default'],
80-
]);
81-
return $rules;
71+
return parent::rules();
8272
}
8373

8474
/**
@@ -112,7 +102,7 @@ public function getContentColumnType(): string
112102
*/
113103
public function normalizeValue($value, ElementInterface $element = null)
114104
{
115-
return $value;
105+
return parent::normalizeValue($value, $element);
116106
}
117107

118108
/**
@@ -133,6 +123,20 @@ public function serializeValue($value, ElementInterface $element = null)
133123
return parent::serializeValue($value, $element);
134124
}
135125

126+
/**
127+
* Taps into the beforeSave method of the Savable Component Interface
128+
* We use this to set custom properties on file types and limits.
129+
*/
130+
public function beforeSave(bool $isNew): bool
131+
{
132+
// Default model properties
133+
$this->allowedKinds = ['csv'];
134+
$this->limit = 1;
135+
$this->restrictFiles = true;
136+
137+
return parent::beforeSave($isNew);
138+
}
139+
136140
/**
137141
* Returns the component’s settings HTML.
138142
*
@@ -232,6 +236,7 @@ public function getSettingsHtml()
232236
'craft-commerce-pricing-matrix/_components/fields/Pricingmatrix_settings',
233237
[
234238
'field' => $this,
239+
'elementType' => AssetElement::class
235240
]
236241
);
237242
}
@@ -337,31 +342,6 @@ public function getInputHtml($value, ElementInterface $element = null): string
337342
{
338343
// Register our asset bundle
339344
Craft::$app->getView()->registerAssetBundle(PricingmatrixFieldAsset::class);
340-
341-
// Get our id and namespace
342-
$id = Craft::$app->getView()->formatInputId($this->handle);
343-
$namespacedId = Craft::$app->getView()->namespaceInputId($id);
344-
345-
// Variables to pass down to our field JavaScript to let it namespace properly
346-
$jsonVars = [
347-
'id' => $id,
348-
'name' => $this->handle,
349-
'namespace' => $namespacedId,
350-
'prefix' => Craft::$app->getView()->namespaceInputId(''),
351-
];
352-
$jsonVars = Json::encode($jsonVars);
353-
Craft::$app->getView()->registerJs("$('#{$namespacedId}-field').CraftCommercePricingMatrixPricingmatrix(" . $jsonVars . ");");
354-
355-
// Render the input template
356-
return Craft::$app->getView()->renderTemplate(
357-
'craft-commerce-pricing-matrix/_components/fields/Pricingmatrix_input',
358-
[
359-
'name' => $this->handle,
360-
'value' => $value,
361-
'field' => $this,
362-
'id' => $id,
363-
'namespacedId' => $namespacedId,
364-
]
365-
);
345+
return parent::getInputHtml($value, $element);
366346
}
367347
}

0 commit comments

Comments
 (0)