Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class CompositeProduct extends Product {
- public static $many_many = array(
- 'Products' => 'Product'
- );
- public static $has_one = array(
- 'TileImage' => 'Image'
- );
- /**
- * Summary fields for displaying Products in the CMS
- *
- * @var Array
- */
- public static $summary_fields = array(
- 'FirstImage' => 'Image',
- 'Title' => 'Name',
- 'Status' => 'Status',
- 'SummaryOfCategories' => 'Categories'
- );
- public function getCMSFields() {
- $fields = parent::getCMSFields();
- $fields->removeFieldFromTab('Root.Main', 'Price');
- $fields->removeFieldFromTab('Root.Main', 'Stock');
- $fields->addFieldToTab(
- 'Root.TileImage',
- $uploadField = new UploadField('TileImage', 'Upload an image for teh category tile', '', '', '', 'Categories'));
- $uploadField->allowedExtensions = array('jpg', 'gif', 'png');
- $fields->addFieldToTab('Root.Main', new HiddenField('Stock', 'Stock', -1));
- //Related products
- $fields->addFieldToTab(
- 'Root.Products',
- new HeaderField('CompositeProductHeader', 'Select the products that are part of this composite product', 4)
- );
- $gridFieldConfig = GridFieldConfig::create()->addComponents(
- new GridFieldToolbarHeader(),
- new GridFieldSortableHeader(),
- new GridFieldDataColumns(),
- new GridFieldManyRelationHandler(),
- new GridFieldPaginator(10),
- new GridFieldEditButton(),
- new GridFieldDeleteAction(),
- new GridFieldDetailForm()
- );
- $tablefield = new GridField("Products", "Products:", $this->Products(), $gridFieldConfig);
- $fields->addFieldToTab('Root.Products', $tablefield);
- return $fields;
- }
- /**
- * Get the URL for this Product, products that are not part of the SiteTree are
- * displayed by the {@link Product_Controller}.
- *
- * @see SiteTree::Link()
- * @see Product_Controller::show()
- * @return String
- */
- function Link($action = null) {
- if ($this->ParentID > -1) {
- //return Controller::join_links(Director::baseURL() . 'product/', $this->URLSegment .'/');
- return parent::Link($action);
- }
- return Controller::join_links(Director::baseURL() . 'compositeproduct/', $this->RelativeLink($action));
- }
- static $allowed_children = array(
- 'CompositeProduct',
- 'Product'
- );
- }
- class CompositeProduct_Controller extends Product_Controller {
- /**
- * Add to cart form for adding Products, to show on the Product page.
- *
- * @param Int $quantity
- * @param String $redirectURL A URL to redirect to after the product is added, useful to redirect to cart page
- */
- function ProductForm($quantity = null, $redirectURL = null) {
- $product = $this->data();
- $products = $product->Products()->sort('Title', 'ASC');
- //SS_Log::log(new Exception(print_r($products->map(), true)), SS_Log::NOTICE);
- $fields = new FieldList();
- $fields->push(new HiddenField('Redirect', 'Redirect', $redirectURL));
- if ($products && $products->exists()) foreach ($products as $product) {
- $price = $product->dbObject('Price')->Nice();
- $fields->push(new LiteralField('ProductTitle' . $product->ID, '<a href="' . $product->URLSegment . '">' . $product->Title . '</a>' , 5));
- $fields->push(new HiddenField('Products[' . $product->ID . '][ProductClass]', 'ProductClass', $product->ClassName));
- $fields->push(new HiddenField('Products[' . $product->ID . '][ProductID]', 'ProductID', $product->ID));
- $fields->push(new LiteralField('Price', '<h4>' . $product->Price()->Nice() . '</h4>'));
- $fields->push(new TextField('Products[' . $product->ID . '][Quantity]', 'Quantity:', $quantity));
- }
- $actions = new FieldList(
- new FormAction('add', 'Add To Cart')
- );
- $validator = null;
- $controller = Controller::curr();
- $form = new Form($controller, 'ProductForm', $fields, $actions, $validator);
- $form->disableSecurityToken();
- return $form;
- }
- public static $allowed_actions = array('ProductForm');
- /**
- * Add an item to the current cart ({@link Order}) for a given {@link Product}.
- *
- * @param Array $data
- * @param Form $form
- */
- function add(Array $data, Form $form) {
- $products = $data['Products'];
- foreach ($products as $productID => $productData) {
- //Get the product, the quantity and the product options
- $product = DataObject::get_by_id($productData['ProductClass'], $productData['ProductID']);
- $quantity = isset($productData['Quantity']) ? $productData['Quantity'] : 1;
- $productVariations = new Variation();
- $options = new ArrayList();
- if($quantity != 0){
- Cart::get_current_order()->addItem($product, $productVariations, $quantity, $options);
- }
- }
- //Show feedback if redirecting back to the Product page
- if (!$this->getRequest()->requestVar('Redirect')) {
- $cartPage = DataObject::get_one('CartPage');
- $message = _t('ProductForm.PRODUCT_ADDED', 'The product was added to your cart.');
- if ($cartPage->exists()) {
- $message = _t(
- 'ProductForm.PRODUCT_ADDED_LINK',
- 'The product was added to {openanchor}your cart{closeanchor}',
- array(
- 'openanchor' => "<a href=\"{$cartPage->Link()}\">",
- 'closeanchor' => "</a>"
- )
- );
- }
- $form->sessionMessage(
- DBField::create_field("HTMLText", $message),
- 'good',
- false
- );
- }
- $this->goToNextPage();
- }
- /**
- * Send user to next page based on current request vars,
- * if no redirect is specified redirect back.
- *
- * TODO make this work with AJAX
- */
- public function goToNextPage() {
- $redirectURL = $this->getRequest()->requestVar('Redirect');
- //Check if on site URL, if so redirect there, else redirect back
- if ($redirectURL && Director::is_site_url($redirectURL)) {
- $this->controller->redirect(Director::absoluteURL(Director::baseURL() . $redirectURL));
- }
- else {
- $this->redirectBack();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement