Skip to content

Commit

Permalink
Merge pull request #781 from boboldehampsink/fix-product-type-sources
Browse files Browse the repository at this point in the history
Fix Product Type sources
  • Loading branch information
andris-sevcenko committed Mar 13, 2019
2 parents 7c66694 + cee7edf commit 7dd97a3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/elements/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ protected static function defineSources(string $context = null): array
$sources[] = ['heading' => Craft::t('commerce', 'Product Types')];

foreach ($productTypes as $productType) {
$key = 'productType:' . $productType->id;
$key = 'productType:' . $productType->uid;
$canEditProducts = Craft::$app->getUser()->checkPermission('commerce-manageProductType:' . $productType->uid);

$sources[$key] = [
Expand Down Expand Up @@ -972,6 +972,12 @@ protected static function defineActions(string $source = null): array
if (preg_match('/^productType:(\d+)$/', $source, $matches)) {
$productType = Plugin::getInstance()->getProductTypes()->getProductTypeById($matches[1]);

if ($productType) {
$productTypes = [$productType];
}
} else if (preg_match('/^productType:(.+)$/', $source, $matches)) {
$productType = Plugin::getInstance()->getProductTypes()->getProductTypeByUid($matches[1]);

if ($productType) {
$productTypes = [$productType];
}
Expand Down
12 changes: 12 additions & 0 deletions src/services/ProductTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use craft\events\FieldEvent;
use craft\events\SiteEvent;
use craft\helpers\App;
use craft\helpers\ArrayHelper;
use craft\helpers\Db;
use craft\helpers\ProjectConfig as ProjectConfigHelper;
use craft\helpers\StringHelper;
Expand Down Expand Up @@ -782,6 +783,17 @@ public function getProductTypeById(int $productTypeId)
return $this->_productTypesById[$productTypeId];
}

/**
* Returns a product type by its UID.
*
* @param string $uid the product type's UID
* @return ProductType|null either the product type or `null`
*/
public function getProductTypeByUid(string $uid)
{
return ArrayHelper::firstWhere($this->getAllProductTypes(), 'uid', $uid, true);
}

/**
* Returns whether a product type’s products have URLs, and if the template path is valid.
*
Expand Down
2 changes: 1 addition & 1 deletion src/templates/products/_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

Craft.Commerce.editableProductTypes = [
{% for productType in craft.commerce.productTypes.editableProductTypes %}
{id: {{ productType.id }}, name: "{{ productType.name|t('commerce')|e('js') }}", handle: "{{ productType.handle|e('js') }}"}{% if not loop.last %},{% endif %}
{id: "{{ productType.uid }}", name: "{{ productType.name|t('commerce')|e('js') }}", handle: "{{ productType.handle|e('js') }}"}{% if not loop.last %},{% endif %}
{% endfor %}
];

Expand Down

0 comments on commit 7dd97a3

Please sign in to comment.