Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted to PSR-2 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions code/extensions/BulkPriceCatalogueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
* @author i-lateral (http://www.i-lateral.com)
* @package commerce-bulkprice
*/
class BulkPriceCatalogueController extends Extension {
class BulkPriceCatalogueController extends Extension
{

public function BulkPriceTable() {
if($this->owner->dataRecord instanceOf Product)
public function BulkPriceTable()
{
if ($this->owner->dataRecord instanceof Product) {
$prices = $this->owner->dataRecord->BulkPrices();
else
} else {
$prices = ArrayList::create();
}

$vars = array(
"BulkPrices" => $prices,
Expand All @@ -24,5 +27,4 @@ public function BulkPriceTable() {
->owner
->renderWith("BulkPriceTable", $vars);
}

}
12 changes: 7 additions & 5 deletions code/extensions/BulkPriceProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
* @author i-lateral (http://www.i-lateral.com)
* @package commerce-bulkprice
*/
class BulkPriceProduct extends DataExtension {
class BulkPriceProduct extends DataExtension
{

private static $has_many = array(
"BulkPrices" => "BulkPrice"
);

public function updateCMSFields(FieldList $fields) {
public function updateCMSFields(FieldList $fields)
{

// Deal with product features
$add_button = new GridFieldAddNewInlineButton('toolbar-header-left');
Expand All @@ -35,11 +37,11 @@ public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab('Root.BulkPrices', $bulk_field);
}

public function onBeforeDelete() {
public function onBeforeDelete()
{
// Clean database before deletion
foreach($this->owner->BulkPrices() as $object) {
foreach ($this->owner->BulkPrices() as $object) {
$object->delete();
}
}

}
18 changes: 9 additions & 9 deletions code/extensions/BulkPriceProductCSV.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php

class BulkPriceProductCSV extends Extension {
class BulkPriceProductCSV extends Extension
{

public function onAfterProcess($record, $object) {
public function onAfterProcess($record, $object)
{

// Setup bulk prices for this object
if(isset($record['BulkPrices']) && $record['BulkPrices']) {
$prices = explode(";",$record["BulkPrices"]);
if (isset($record['BulkPrices']) && $record['BulkPrices']) {
$prices = explode(";", $record["BulkPrices"]);

if(count($prices)) {
foreach($prices as $price) {
$price_data = explode("=",$price);
if (count($prices)) {
foreach ($prices as $price) {
$price_data = explode("=", $price);

// Setup new bulk price object and link
$bulk_price = BulkPrice::create();
Expand All @@ -21,7 +23,5 @@ public function onAfterProcess($record, $object) {
}
}
}

}

}
36 changes: 22 additions & 14 deletions code/extensions/BulkPriceShoppingCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
* @author i-lateral (http://www.i-lateral.com)
* @package commerce-bulkprice
*/
class BulkPriceShoppingCart extends Extension {
class BulkPriceShoppingCart extends Extension
{

private function calculate_bulk_price(Product $product, $item) {
private function calculate_bulk_price(Product $product, $item)
{
$price = 0;

foreach($product->BulkPrices() as $bulk_price) {
foreach ($product->BulkPrices() as $bulk_price) {
$range = array();

// Determine whaty type of price we are dealing with
if(strpos($bulk_price->Quantity,"-") !== false) { // We are looking for a range
if (strpos($bulk_price->Quantity, "-") !== false) { // We are looking for a range
$range = explode("-", $bulk_price->Quantity);
} elseif(strpos($bulk_price->Quantity,"+") !== false) { // We are looking this number or greater
} elseif (strpos($bulk_price->Quantity, "+") !== false) { // We are looking this number or greater
$range[0] = str_replace("+", "", $bulk_price->Quantity);
$range[1] = -1; // -1 means no upper limit
} else { // Assume we are dealing with a single price
Expand All @@ -32,17 +34,20 @@ private function calculate_bulk_price(Product $product, $item) {

// Finally check if the current quantity sits in the
// current range and amend price
if(
if (
($range[1] == -1 && $item->Quantity >= $range[0]) ||
($item->Quantity >= $range[0] && $item->Quantity <= $range[1])
)
) {
$price = $bulk_price->Price;
}
}

if(!$price) $price = $product->Price;
if (!$price) {
$price = $product->Price;
}

// Check for customisations that modify price
foreach($item->Customised as $custom_item) {
foreach ($item->Customised as $custom_item) {
// If a customisation modifies price, adjust the price
$price = (float)$price + (float)$custom_item->ModifyPrice;
}
Expand All @@ -54,17 +59,20 @@ private function calculate_bulk_price(Product $product, $item) {
/**
* Calculate the item price, based on any bulk discounts set
*/
public function onBeforeAdd($item) {
if($product = Product::get()->byID($item->ProductID))
public function onBeforeAdd($item)
{
if ($product = Product::get()->byID($item->ProductID)) {
$item->Price = $this->calculate_bulk_price($product, $item);
}
}

/**
* Calculate the item price, based on any bulk discounts set
*/
public function onAfterUpdate($item) {
if($product = Product::get()->byID($item->ProductID))
public function onAfterUpdate($item)
{
if ($product = Product::get()->byID($item->ProductID)) {
$item->Price = $this->calculate_bulk_price($product, $item);
}
}

}
19 changes: 12 additions & 7 deletions code/model/BulkPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* @author i-lateral (http://www.i-lateral.com)
* @package commerce-bulkprice
*/
class BulkPrice extends DataObject {
class BulkPrice extends DataObject
{

private static $db = array(
"Quantity" => "Varchar",
Expand All @@ -30,27 +31,31 @@ class BulkPrice extends DataObject {
"Price"
);

public function onBeforeWrite() {
public function onBeforeWrite()
{
parent::onBeforeWrite();

// Ensure we strip any white space from the quantity field
$this->Quantity = str_replace(" ", "", $this->Quantity);
}

public function canView($member = false) {
public function canView($member = false)
{
return $this->Parent()->canView($member);
}

public function canCreate($member = null) {
public function canCreate($member = null)
{
return $this->Parent()->canCreate($member);
}

public function canEdit($member = null) {
public function canEdit($member = null)
{
return $this->Parent()->canEdit($member);
}

public function canDelete($member = null) {
public function canDelete($member = null)
{
return $this->Parent()->canDelete($member);
}

}