<?php Director::addRules(50, array('createecommercevariations/$Action/$ProductID' => 'CreateEcommerceVariations', 'createecommercevariationsbatch/$Action' => 'CreateEcommerceVariations_Batch')); Buyable::add_class("ProductVariation"); Object::add_extension("Product", "ProductWithVariationDecorator"); Object::add_extension("Product_Controller", "ProductWithVariationDecorator_Controller"); Object::add_extension("ProductBulkLoader", "ProductVariationBulkLoader"); Product_Controller::$allowed_actions[] = 'VariationForm'; Product_Controller::$allowed_actions[] = 'addvariation'; LeftAndMain::require_javascript(THIRDPARTY_DIR . "/jquery/jquery.js"); LeftAndMain::require_javascript(THIRDPARTY_DIR . "/jquery-livequery/jquery.livequery.js"); LeftAndMain::require_javascript("ecommerce_product_variation/javascript/CreateEcommerceVariationsField.js"); LeftAndMain::require_themed_css("CreateEcommerceVariationsField"); ProductsAndGroupsModelAdmin::$model_importers['ProductVariation'] = null; //copy the lines between the START AND END line to your /mysite/_config.php file and choose the right settings // __________________________________ START ECOMMERCE PRODUCT VARIATIONS MODULE CONFIG __________________________________ //____________HIGHLY RECOMMENDED //ProductsAndGroupsModelAdmin::add_managed_model("ProductAttributeValue"); //ProductsAndGroupsModelAdmin::add_managed_model("ProductAttributeType"); //ProductsAndGroupsModelAdmin::add_managed_model("ProductVariation"); //____________ADD TO CART FORM INTERACTION //ProductWithVariationDecorator_Controller::set_use_js_validation(false); //ProductWithVariationDecorator_Controller::set_alternative_validator_class_name("MyValidatorClass"); //____________EASY SORTING - REQUIRES: http://sunny.svnrepository.com/svn/sunny-side-up-general/dataobjectsorter //Object::add_extension('ProductAttributeValue', 'DataObjectSorterDOD'); //Object::add_extension('ProductAttributeType', 'DataObjectSorterDOD'); //DataObjectSorterDOD::set_also_update_sort_field(true); //DataObjectSorterDOD::set_do_not_add_alternative_sort_field(true); //____________CUSTOMISED CMS INTERACTION //LeftAndMain::require_javascript("mysite/javascript/MyCreateEcommerceVariationsField.js"); //____________COLOUR OPTIONS
/** * Apply the discount from this or any parent object to * a given price. * * @param ProductCategory|Buyable $obj * @param $price * @return bool - was any discount applied? */ protected function applyPromoFrom($obj, &$price) { if (!$obj->hasValidPromotion($obj)) { return false; } // Apply the price if ($obj->PromoType == 'Percent') { $price -= $price * $obj->PromoPercent; } else { $price -= $obj->PromoAmount; } // there can be issues with the charged total being different // from the saved Total - sometimes by several cents - if // we don't round here. $precision = (int) Config::inst()->get('Order', 'rounding_precision'); $price = round($price, $precision ? $precision : 2); if ($price < 0) { $price = 0; } return true; }
/** * Ensure the proper buyable will be returned for a given buyable… * This is being used to ensure a product with variations cannot be added to the cart… * a Variation has to be added instead! * @param Buyable $buyable * @return Buyable */ public function getCorrectBuyable(Buyable $buyable) { if ($buyable instanceof Product && $buyable->hasExtension('ProductVariationsExtension') && $buyable->Variations()->count() > 0) { foreach ($buyable->Variations() as $variation) { if ($variation->canPurchase()) { return $variation; } } } return $buyable; }
/** * Add one of an item to a cart (Product Page) * * @see the addtocart function within AddProductForm class * @param SS_HTTPRequest $request * @param AjaxHTTPResponse $response * @param Buyable $buyable [optional] * @param int $quantity [optional] * @param AddProductForm $form [optional] */ public function updateAddProductFormResponse(&$request, &$response, $buyable, $quantity, $form) { if ($request->isAjax()) { if (!$response) { $response = $this->owner->getController()->getResponse(); } $response->removeHeader('Content-Type'); $response->addHeader('Content-Type', 'application/json; charset=utf-8'); $shoppingcart = ShoppingCart::curr(); $shoppingcart->calculate(); // recalculate the shopping cart $data = array('id' => (string) $buyable->ID, 'internalItemID' => $buyable->InternalItemID, 'title' => $buyable->Title, 'url' => $buyable->URLSegment, 'categories' => $buyable->getCategories()->column('Title'), 'addLink' => $buyable->addLink(), 'removeLink' => $buyable->removeLink(), 'removeallLink' => $buyable->removeallLink(), 'setquantityLink' => $buyable->Item()->setquantityLink(), 'message' => array('content' => $form->Message(), 'type' => $form->MessageType())); $form->clearMessage(); // include totals if required if ($shoppingcart) { $data['subTotal'] = $shoppingcart->SubTotal(); $data['grandTotal'] = $shoppingcart->GrandTotal(); } $this->owner->extend('updateAddProductFormResponseShopJsonResponse', $data, $request, $response, $buyable, $quantity, $form); $response->setBody(json_encode($data)); } }