Skip to content

Commit

Permalink
Fix issue in VariationForm.
Browse files Browse the repository at this point in the history
Make sure a VariationForm is being used for products with variations (instead of `AddProductForm`).
No longer use a public property that is being modified on the controller. Use a getter with extension hook instead.
  • Loading branch information
bummzack committed Apr 10, 2018
1 parent ecaba6d commit 00a1458
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/Extension/ProductVariationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,7 @@

namespace SilverShop\Extension;

use SilverShop\Forms\VariationForm;
use SilverShop\Model\Variation\AttributeType;
use SilverShop\Model\Variation\AttributeValue;
use SilverShop\Model\Variation\Variation;
Expand Down Expand Up @@ -237,10 238,10 @@ public function onAfterDelete()
}
}

public function contentcontrollerInit($controller)
public function updateFormClass(&$formClass)
{
if ($this->owner->Variations()->exists()) {
$controller->formclass = 'VariationForm';
$formClass = VariationForm::class;
}
}
}
6 changes: 3 additions & 3 deletions src/Forms/VariationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 154,9 @@ protected function getFormFields($controller = null)
}

foreach ($query->execute()->column('ID') as $variationID) {
$query2->setSelect('ProductAttributeValueID')
->setFrom('ProductVariation_AttributeValues')
->setWhere(array('ProductVariationID' => $variationID));
$query2->setSelect('SilverShop_AttributeValueID')
->setFrom('SilverShop_Variation_AttributeValues')
->setWhere(array('SilverShop_VariationID' => $variationID));
$vararray[$variationID] = $query2->execute()->keyedColumn();
}

Expand Down
12 changes: 12 additions & 0 deletions src/Page/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 6,7 @@
use SilverShop\Cart\ShoppingCart;
use SilverShop\Cart\ShoppingCartController;
use SilverShop\Extension\ProductVariationsExtension;
use SilverShop\Forms\AddProductForm;
use SilverShop\Model\Buyable;
use SilverShop\Model\Order;
use SilverShop\Model\Product\OrderItem;
Expand Down Expand Up @@ -496,4 497,15 @@ public function removeallLink()
{
return ShoppingCartController::remove_all_item_link($this);
}

/**
* Get the form class to use to edit this product in the frontend
* @return string FQCN
*/
public function getFormClass()
{
$formClass = AddProductForm::class;
$this->extend('updateFormClass', $formClass);
return $formClass;
}
}
3 changes: 2 additions & 1 deletion src/Page/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,7 @@

use PageController;
use SilverShop\Forms\AddProductForm;
use SilverStripe\Core\Injector\Injector;

class ProductController extends PageController
{
Expand All @@ -14,7 15,7 @@ class ProductController extends PageController

public function Form()
{
$form = AddProductForm::create($this, 'Form');
$form = Injector::inst()->create($this->getFormClass(), $this, 'Form');
$this->extend('updateForm', $form);
return $form;
}
Expand Down

0 comments on commit 00a1458

Please sign in to comment.