Skip to content

Commit

Permalink
BUGFIX product availability doesn’t check product options (#380)
Browse files Browse the repository at this point in the history
* BUGFIX product availability doesn’t check product options

* REMOVE console.log()
  • Loading branch information
muskie9 authored Jan 18, 2019
1 parent df28576 commit 2b4027b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 99 deletions.
97 changes: 47 additions & 50 deletions javascript/product_options.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
;(function ($) {

var trigger = 'select.product-options',
formName = "#$FormName",
shownPrice = '[id*="_submitPrice"]',
selects = '#$FormName select',
initialPrice = $(shownPrice).html().replace('$', '');

$(trigger).change(function () {
refreshCartPrice();
});

var refreshCartPrice = function refreshAddToCartPrice()
{
var price = $(shownPrice).html();
var newProductPrice = parseFloat(initialPrice);

$(selects).each(function () {

if ($(this).attr('id') == 'qty') {
// todo: modify newProductPrice by Quantity?
} else {
var currentOption = $(this).val();
//get an array of the modifiers
currentOption = currentOption.substring(currentOption.lastIndexOf('{') + 1, currentOption.lastIndexOf('}')).split('|');

//build a different array of key-value pairs, options[p,c,w] = value
//more reliable than hoping price is the first array index of currentOption..
var options = [];
for (i = 0; i < currentOption.length; i++) {
var k = currentOption[i].substr(0, 1);
var val = currentOption[i].substr(1);
options[k] = val;
}

if (typeof options['p'] != 'undefined') {
var pricemodifier = options['p'].substr(0, 1); // return +,-,:
if (pricemodifier == ':') {
newProductPrice = parseFloat(options['p'].substr(1));
} else {
newProductPrice = newProductPrice + parseFloat(options['p']);
}
}
}
});
$(shownPrice).html('$' + newProductPrice.toFixed(2));
};

if ($(trigger).length > 0) {
refreshCartPrice();
}
var shownPrice = $('[id*="_submitPrice"]'),
trigger = $('.product-options'),
isAvailable = $('[name="action_x:submit"]').length ? true : false,
unavailable = trigger.closest('form').find('[id*="_unavailableText"]');

$('option:disabled').each(function () {
if ($(this).prop('disabled')) {
$(this).addClass('outOfStock').append(document.createTextNode(" (out of stock)"));
}
});

trigger.on('change', function () {
var options = [],
selected = $(this).val();

if (selected.length > 0) {
selected = selected.substring(selected.lastIndexOf('{') + 1, selected.lastIndexOf('}')).split('|')[0].split(':')[1];
}

$(this).each(function () {
var currentOption = $(this).val();
currentOption = currentOption.substring(currentOption.lastIndexOf('{') + 1, currentOption.lastIndexOf('}')).split('|');

if (currentOption.length) {
$.each(currentOption, function (k, v) {
if (v !== '') {
console.log(v.split(':')[1]);
options[v.split(':')[1]] = v.split(':')[1];
}
});
}
});

if (selected in options) {
shownPrice.html('$' + Number.parseFloat(options[selected]).toFixed(2));
}
});

if (isAvailable === false) {
shownPrice.addClass('hidden');
unavailable.removeClass('hidden');
} else {
if (trigger.length > 0) {
trigger.change();
}

}
})(jQuery);
12 changes: 3 additions & 9 deletions src/Controller/ProductPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,14 @@ public function init()
Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js');
if ($this->data()->Available && $this->ProductOptions()->exists()) {
$formName = $this->PurchaseForm()->FormName();
Requirements::javascriptTemplate(
/*Requirements::javascriptTemplate(
'dynamic/foxystripe: javascript/out_of_stock.js',
[
'FormName' => $formName,
],
'foxystripe.out_of_stock'
);
Requirements::javascriptTemplate(
'dynamic/foxystripe: javascript/product_options.js',
[
'FormName' => $formName,
],
'foxystripe.product_options'
);
);*/
Requirements::javascript('dynamic/foxystripe: javascript/product_options.js');
}

Requirements::customScript(<<<JS
Expand Down
78 changes: 38 additions & 40 deletions src/Form/FoxyStripePurchaseForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,14 @@ protected function getProductFields(FieldList $fields)
->addExtraClass('submit-price')
);
$fields->push(
HeaderField::create('unavailableText', 'Selection unavailable', 4)
->addExtraClass('hidden unavailable-text')
$unavailable = HeaderField::create('unavailableText', 'Selection unavailable', 4)
->addExtraClass('unavailable-text')
);

if (!empty(trim($this->getSiteConfig()->StoreName)) && $this->getProduct()->getIsAvailable()) {
$unavailable->addExtraClass('hidden');
}

$this->extend('updatePurchaseFormFields', $fields);
} else {
$fields->push(HeaderField::create('submitPrice', 'Currently Out of Stock', 4));
Expand All @@ -246,24 +250,13 @@ protected function getProductFields(FieldList $fields)
*/
protected function getProductActions(FieldList $actions)
{
$actions->push($submit = FormAction::create(
'',
_t('ProductForm.AddToCart', 'Add to Cart')
));
$submit->setAttribute(
'name',
ProductPage::getGeneratedValue(
$this->product->Code,
'Submit',
_t('ProductForm.AddToCart', 'Add to Cart')
)
)->addExtraClass('fs-add-to-cart-button');
if (!$this->site_config->StoreName ||
$this->site_config->StoreName == '' ||
!isset($this->site_config->StoreName) ||
!$this->product->Available
) {
$submit->setAttribute('Disabled', true);
if (!empty(trim($this->getSiteConfig()->StoreName)) && $this->getProduct()->getIsAvailable()) {
$actions->push(
$submit = FormAction::create(
'x:submit',
_t('ProductForm.AddToCart', 'Add to Cart')
)->addExtraClass('fs-add-to-cart-button')
);
}

$this->extend('updateFoxyStripePurchaseFormActions', $actions);
Expand All @@ -276,12 +269,6 @@ protected function getProductActions(FieldList $actions)
*/
protected function getProductOptionSet()
{
$assignAvailable = function ($self) {
/** @var OptionItem $self */
$this->extend('updateFoxyStripePurchaseForm', $form);
$self->Available = ($self->getAvailability()) ? true : false;
};

$options = $this->product->ProductOptions();
$groupedOptions = new GroupedList($options);
$groupedBy = $groupedOptions->groupBy('ProductOptionGroupID');
Expand All @@ -294,38 +281,49 @@ protected function getProductOptionSet()
$group = OptionGroup::get()->byID($id);
$title = $group->Title;
$name = preg_replace('/\s/', '_', $title);
$set->each($assignAvailable);
$disabled = [];
$fullOptions = [];

foreach ($set as $item) {
$fullOptions[ProductPage::getGeneratedValue(
$item = $this->setAvailability($item);

$name = ProductPage::getGeneratedValue(
$this->product->Code,
$group->Title,
$item->getGeneratedValue(),
'value'
)] = $item->getGeneratedTitle();
);

$fullOptions[$name] = $item->getGeneratedTitle();
if (!$item->Availability) {
array_push(
$disabled,
ProductPage::getGeneratedValue(
$this->product->Code,
$group->Title,
$item->getGeneratedValue(),
'value'
)
);
array_push($disabled, $name);
}
}

$optionsSet->push(
$dropdown = DropdownField::create($name, $title, $fullOptions)->setTitle($title)
);
$dropdown->setDisabledItems($disabled);

$dropdown->addExtraClass("product-options product-options-{$name}");
if (!empty($disabled)) {
$dropdown->setDisabledItems($disabled);
}

$dropdown->addExtraClass("product-options");
}

$optionsSet->addExtraClass('foxycartOptionsContainer');

return $optionsSet;
}

/**
* @param OptionItem $option
* @return OptionItem
*/
protected function setAvailability(OptionItem $option)
{
$option->Available = ($option->getAvailability()) ? true : false;

return $option;
}
}
18 changes: 18 additions & 0 deletions src/Page/ProductPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,22 @@ public function providePermissions()
'Product_CANCRUD' => 'Allow user to manage Products and related objects',
];
}

/**
* @return bool
*/
public function getIsAvailable()
{
if (!$this->Available) {
return false;
}

foreach ($this->ProductOptions() as $option) {
if ($option->Available) {
return true;
}
}

return false;
}
}

0 comments on commit 2b4027b

Please sign in to comment.