Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Adds a product to the cart
- *
- * @package simplecart
- * @author Bert Oost at OostDesign.nl
- */
- $cart = $modx->getService('simplecart','SimpleCart',$modx->getOption('simplecart.core_path',null,$modx->getOption('core_path').'components/simplecart/').'model/simplecart/',$scriptProperties);
- if (!($cart instanceof SimpleCart)) return '';
- $cart->initialize($modx->context->get('key'));
- $submitVar = $modx->getOption('submitVar', $scriptProperties, 'addcart');
- $productIdVar = $modx->getOption('productIdVar', $scriptProperties, 'productid');
- $quantityVar = $modx->getOption('quantityVar', $scriptProperties, 'quantity');
- $redirectTo = $modx->getOption('redirectTo', $scriptProperties, false);
- if(empty($redirectTo)) { $redirectTo = $modx->resource->get('id'); }
- $redirectScheme = $modx->getOption('redirectScheme', $scriptProperties, 'http');
- // catch the post
- if(
- isset($_POST) && !empty($_POST) && isset($_POST[$submitVar]) &&
- !empty($productIdVar) && !empty($quantityVar)
- ) {
- $productId = (isset($_POST[$productIdVar]) && !empty($_POST[$productIdVar])) ? $_POST[$productIdVar] : $modx->resource->get('id');
- $quantity = (isset($_POST[$quantityVar]) && !empty($_POST[$quantityVar])) ? $_POST[$quantityVar] : 1;
- $options = $modx->schooks->load('cart/parseoptions', array_merge(
- $scriptProperties,
- $_REQUEST,
- array('productid' => $productId)
- ));
- // run the hook
- $added = $modx->schooks->load('cart/add', array(
- 'productid' => $productId,
- 'quantity' => $quantity,
- 'options' => $options
- ));
- // redirect user to the cart or redirect back when not having an id set
- if($added) {
- $_SESSION['simplecart_added_product'] = $productId;
- $url = $modx->makeUrl($redirectTo, '', '', $redirectScheme);
- }
- else {
- $url = $modx->makeUrl($redirectTo, '', '&added=false', $redirectScheme);
- }
- $modx->sendRedirect($url);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement