private function addspecialprice() { $task = new EcommerceTaskCreateMemberGroups(); $task->run(false); $customerGroup = EcommerceRole::get_customer_group(); if (!$customerGroup) { die("could not create customer group"); } $group = new Group(); $group->Title = "Discount Customers"; $group->Code = "discountcustomers"; $group->ParentID = $customerGroup->ID; $group->write(); $member = new Member(); $member->FirstName = 'Bob'; $member->Surname = 'Jones'; $member->Email = '*****@*****.**'; $member->Password = '******'; $member->write(); $member->Groups()->add($group); $products = Product::get()->where("ClassName = 'Product'")->sort("RAND()")->limit(2); $this->addExamplePages(4, "Special price for particular customers", $products); $i = 0; foreach ($products as $product) { $i++; $complexObjectPrice = new ComplexPriceObject(); if ($i == 1) { $complexObjectPrice->Price = $product->Price - 1.5; } elseif ($i == 2) { $complexObjectPrice->Percentage = 10; $complexObjectPrice->Reduction = 2.5; } else { $complexObjectPrice->Price = $product->Price - 1.5; $complexObjectPrice->Percentage = 10; $complexObjectPrice->Reduction = 2.5; } $complexObjectPrice->From = date("Y-m-d h:n:s", strtotime("now")); $complexObjectPrice->Until = date("Y-m-d h:n:s", strtotime("next year")); $complexObjectPrice->ProductID = $product->ID; $complexObjectPrice->write(); $complexObjectPrice->Groups()->add($group); $product->Content = "<p><a href=\"Security/login/?BackURL=" . $product->Link() . "\">Login</a> as bob@silverstripe-ecommerce.com, password: test123 to get a special price. You can then <a href=\"Security/logout/?BackURL=" . $product->Link() . "\">log out</a> again to see the original price.</p>"; $this->addToTitle($product, "member price", true); } $variations = ProductVariation::get()->where("ClassName = 'ProductVariation'")->sort("RAND()")->limit(2); $i = 0; foreach ($variations as $variation) { $i++; $complexObjectPrice = new ComplexPriceObject(); if ($i == 1) { $complexObjectPrice->Price = $product->Price - 1.5; } elseif ($i == 2) { $complexObjectPrice->Percentage = 10; $complexObjectPrice->Reduction = 2.5; } else { $complexObjectPrice->Price = $product->Price - 1.5; $complexObjectPrice->Percentage = 10; $complexObjectPrice->Reduction = 2.5; } $complexObjectPrice->Price = $variation->Price - 1.5; $complexObjectPrice->From = date("Y-m-d h:n:s", strtotime("now")); $complexObjectPrice->Until = date("Y-m-d h:n:s", strtotime("next year")); $complexObjectPrice->ProductVariationID = $variation->ID; $complexObjectPrice->write(); $complexObjectPrice->Groups()->add($group); $product = $variation->Product(); $this->addExamplePages(4, "Special price for particular customers for product variations {$i}", $product); $product->Content = "<p><a href=\"Security/login/?BackURL=" . $product->Link() . "\">Login</a> as bob@jones.com, password: test123 to get a special price</p>"; $this->addToTitle($product, "member price", true); } }
function updateCalculatedPrice(&$startingPrice) { $newPrice = -1; $fieldName = $this->owner->ClassName . "ID"; $singleton = ComplexPriceObject::get()->first(); if ($singleton) { // Check that ComplexPriceObject can be joined to this type of object if (!$singleton->hasField($fieldName)) { $ancestorArray = ClassInfo::ancestry($this->owner, true); foreach ($ancestorArray as $ancestor) { $fieldName = $ancestor . "ID"; if ($singleton->hasField($fieldName)) { break; } } } // Load up the alternate prices for this product $prices = ComplexPriceObject::get()->filter(array($fieldName => $this->owner->ID, "NoLongerValid" => 0))->sort("NewPrice", "DESC"); $memberGroupsArray = array(); if ($prices->count()) { // Load up the groups for the current memeber, if any if ($member = Member::currentUser()) { if ($memberGroupComponents = $member->getManyManyComponents('Groups')) { if ($memberGroupComponents && $memberGroupComponents->count()) { $memberGroupsArray = $memberGroupComponents->column("ID"); if (!is_array($memberGroupsArray)) { $memberGroupsArray = array(); } } } } $countryID = EcommerceCountry::get_country_id(); // Look at each price and see if it can be used foreach ($prices as $price) { $priceCanBeUsed = true; // Does it pass the group filter? if ($priceGroupComponents = $price->getManyManyComponents('Groups')) { if ($priceGroupComponents && $priceGroupComponents->count()) { $priceCanBeUsed = false; $priceGroupArray = $priceGroupComponents->column("ID"); if (!is_array($priceGroupArray)) { $priceGroupArray = array(); } $interSectionArray = array_intersect($priceGroupArray, $memberGroupsArray); if (is_array($interSectionArray) && count($interSectionArray)) { $priceCanBeUsed = true; } } } // Does it pass the country filter? if ($priceCanBeUsed) { if ($priceCountryComponents = $price->getManyManyComponents('EcommerceCountries')) { if ($priceCountryComponents && $priceCountryComponents->count()) { $priceCanBeUsed = false; $priceCountryArray = $priceCountryComponents->column("ID"); if (!is_array($priceCountryArray)) { $priceCountryArray = array(); } if ($countryID && in_array($countryID, $priceCountryArray)) { $priceCanBeUsed = true; } } } } // Does it pass the date filter? if ($priceCanBeUsed) { $nowTS = strtotime("now"); if ($price->From) { $priceCanBeUsed = false; $fromTS = strtotime($price->From); if ($fromTS && $fromTS < $nowTS) { $priceCanBeUsed = true; } } } if ($priceCanBeUsed) { if ($price->Until) { $priceCanBeUsed = false; $untilTS = strtotime($price->Until); if ($untilTS && $untilTS > $nowTS) { $priceCanBeUsed = true; } } } // If so, apply the price if ($priceCanBeUsed) { $newPrice = $price->getCalculatedPrice(); } } } } if ($newPrice > -1) { $startingPrice = $newPrice; } return $startingPrice; }