Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. <?php
  2.  
  3. class CompositeProduct extends Product {
  4.  
  5. public static $many_many = array(
  6. 'Products' => 'Product'
  7. );
  8.  
  9. public static $has_one = array(
  10. 'TileImage' => 'Image'
  11. );
  12.  
  13. /**
  14. * Summary fields for displaying Products in the CMS
  15. *
  16. * @var Array
  17. */
  18. public static $summary_fields = array(
  19. 'FirstImage' => 'Image',
  20. 'Title' => 'Name',
  21. 'Status' => 'Status',
  22. 'SummaryOfCategories' => 'Categories'
  23. );
  24.  
  25. public function getCMSFields() {
  26. $fields = parent::getCMSFields();
  27.  
  28. $fields->removeFieldFromTab('Root.Main', 'Price');
  29. $fields->removeFieldFromTab('Root.Main', 'Stock');
  30.  
  31. $fields->addFieldToTab(
  32. 'Root.TileImage',
  33. $uploadField = new UploadField('TileImage', 'Upload an image for teh category tile', '', '', '', 'Categories'));
  34. $uploadField->allowedExtensions = array('jpg', 'gif', 'png');
  35.  
  36. $fields->addFieldToTab('Root.Main', new HiddenField('Stock', 'Stock', -1));
  37.  
  38. //Related products
  39. $fields->addFieldToTab(
  40. 'Root.Products',
  41. new HeaderField('CompositeProductHeader', 'Select the products that are part of this composite product', 4)
  42. );
  43.  
  44. $gridFieldConfig = GridFieldConfig::create()->addComponents(
  45. new GridFieldToolbarHeader(),
  46. new GridFieldAddNewButton('toolbar-header-right'),
  47. new GridFieldSortableHeader(),
  48. new GridFieldDataColumns(),
  49. new GridFieldManyRelationHandler(),
  50. new GridFieldPaginator(10),
  51. new GridFieldEditButton(),
  52. new GridFieldDeleteAction(),
  53. new GridFieldDetailForm()
  54. );
  55.  
  56. $tablefield = new GridField("Products", "Products:", $this->Products(), $gridFieldConfig);
  57. $fields->addFieldToTab('Root.Products', $tablefield);
  58.  
  59. return $fields;
  60. }
  61.  
  62. /**
  63. * Get the URL for this Product, products that are not part of the SiteTree are
  64. * displayed by the {@link Product_Controller}.
  65. *
  66. * @see SiteTree::Link()
  67. * @see Product_Controller::show()
  68. * @return String
  69. */
  70. function Link($action = null) {
  71.  
  72. if ($this->ParentID > -1) {
  73. //return Controller::join_links(Director::baseURL() . 'product/', $this->URLSegment .'/');
  74. return parent::Link($action);
  75. }
  76. return Controller::join_links(Director::baseURL() . 'compositeproduct/', $this->RelativeLink($action));
  77. }
  78.  
  79. static $allowed_children = array('CompositeProduct');
  80.  
  81.  
  82.  
  83. }
  84.  
  85. class CompositeProduct_Controller extends Product_Controller {
  86. /**
  87. * Add to cart form for adding Products, to show on the Product page.
  88. *
  89. * @param Int $quantity
  90. * @param String $redirectURL A URL to redirect to after the product is added, useful to redirect to cart page
  91. */
  92. function ProductForm($quantity = null, $redirectURL = null) {
  93. $product = $this->data();
  94.  
  95. $products = $product->Products();
  96. //SS_Log::log(new Exception(print_r($products->map(), true)), SS_Log::NOTICE);
  97.  
  98.  
  99. $fields = new FieldList();
  100. $fields->push(new HiddenField('Redirect', 'Redirect', $redirectURL));
  101.  
  102. if ($products && $products->exists()) foreach ($products as $product) {
  103. $fields->push(new LiteralField('ProductTitle' . $product->ID, '<a href="' . $product->URLSegment . '">' . $product->Title . '</a>' , 5));
  104. $fields->push(new HiddenField('Products[' . $product->ID . '][ProductClass]', 'ProductClass', $product->ClassName));
  105. $fields->push(new HiddenField('Products[' . $product->ID . '][ProductID]', 'ProductID', $product->ID));
  106. $fields->push(new LiteralField('Price', '<h4>' . $product->Price . '</h4>'));
  107. $fields->push(new TextField('Products[' . $product->ID . '][Quantity]', 'Quantity:', $quantity));
  108. }
  109.  
  110. $actions = new FieldList(
  111. new FormAction('add', 'Add To Cart')
  112. );
  113.  
  114. $validator = null;
  115.  
  116. $controller = Controller::curr();
  117. $form = new Form($controller, 'ProductForm', $fields, $actions, $validator);
  118. $form->disableSecurityToken();
  119.  
  120. return $form;
  121. }
  122.  
  123. public static $allowed_actions = array('ProductForm');
  124. /**
  125. * Add an item to the current cart ({@link Order}) for a given {@link Product}.
  126. *
  127. * @param Array $data
  128. * @param Form $form
  129. */
  130. function add(Array $data, Form $form) {
  131. $products = $data['Products'];
  132. foreach ($products as $productID => $productData) {
  133. //Get the product, the quantity and the product options
  134. $product = DataObject::get_by_id($productData['ProductClass'], $productData['ProductID']);
  135. $quantity = isset($productData['Quantity']) ? $productData['Quantity'] : 1;
  136. $productVariations = new Variation();
  137. $options = new ArrayList();
  138. if($quantity != 0){
  139. Cart::get_current_order()->addItem($product, $productVariations, $quantity, $options);
  140. }
  141. }
  142.  
  143. //Show feedback if redirecting back to the Product page
  144. if (!$this->getRequest()->requestVar('Redirect')) {
  145. $cartPage = DataObject::get_one('CartPage');
  146. $message = ($cartPage)
  147. ? 'The products were added to <a href="' . $cartPage->Link() . '">your cart</a>'
  148. : "The products were added to your cart.";
  149. $form->sessionMessage(
  150. $message,
  151. 'good'
  152. );
  153. }
  154. $this->goToNextPage();
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement