Advertisement
Guest User

Snippet scAddProduct

a guest
May 9th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. <?php
  2. /**
  3. * Adds a product to the cart
  4. *
  5. * @package simplecart
  6. * @author Bert Oost at OostDesign.nl
  7. */
  8. $cart = $modx->getService('simplecart','SimpleCart',$modx->getOption('simplecart.core_path',null,$modx->getOption('core_path').'components/simplecart/').'model/simplecart/',$scriptProperties);
  9. if (!($cart instanceof SimpleCart)) return '';
  10. $cart->initialize($modx->context->get('key'));
  11.  
  12. $submitVar = $modx->getOption('submitVar', $scriptProperties, 'addcart');
  13. $productIdVar = $modx->getOption('productIdVar', $scriptProperties, 'productid');
  14. $quantityVar = $modx->getOption('quantityVar', $scriptProperties, 'quantity');
  15. $redirectTo = $modx->getOption('redirectTo', $scriptProperties, false);
  16. if(empty($redirectTo)) { $redirectTo = $modx->resource->get('id'); }
  17. $redirectScheme = $modx->getOption('redirectScheme', $scriptProperties, 'http');
  18.  
  19. // catch the post
  20. if(
  21. isset($_POST) && !empty($_POST) && isset($_POST[$submitVar]) &&
  22. !empty($productIdVar) && !empty($quantityVar)
  23. ) {
  24.  
  25. $productId = (isset($_POST[$productIdVar]) && !empty($_POST[$productIdVar])) ? $_POST[$productIdVar] : $modx->resource->get('id');
  26. $quantity = (isset($_POST[$quantityVar]) && !empty($_POST[$quantityVar])) ? $_POST[$quantityVar] : 1;
  27.  
  28. $options = $modx->schooks->load('cart/parseoptions', array_merge(
  29. $scriptProperties,
  30. $_REQUEST,
  31. array('productid' => $productId)
  32. ));
  33.  
  34. // run the hook
  35. $added = $modx->schooks->load('cart/add', array(
  36. 'productid' => $productId,
  37. 'quantity' => $quantity,
  38. 'options' => $options
  39. ));
  40.  
  41. // redirect user to the cart or redirect back when not having an id set
  42. if($added) {
  43.  
  44. $_SESSION['simplecart_added_product'] = $productId;
  45. $url = $modx->makeUrl($redirectTo, '', '', $redirectScheme);
  46. }
  47. else {
  48.  
  49. $url = $modx->makeUrl($redirectTo, '', '&added=false', $redirectScheme);
  50. }
  51.  
  52. $modx->sendRedirect($url);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement