Created
June 19, 2017 09:33
-
-
Save anonymous/fa3ceac295db3b28894271a0e0b30f8c to your computer and use it in GitHub Desktop.
modmore SimpleCart - Adjust cart quantities based on stock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: Mat | |
* Date: 6/19/2017 | |
* Time: 3:33 AM | |
*/ | |
$sc = $modx->getService('simplecart','SimpleCart',$modx->getOption('simplecart.core_path',null,$modx->getOption('core_path').'components/simplecart/').'model/simplecart/',$scriptProperties); | |
if (!($sc instanceof SimpleCart)) return ''; | |
$controller = $sc->loadController('Cart', 'ContentsController'); | |
$controller->initialize(); | |
$controller->loadProducts(); | |
foreach ($controller->getProducts() as $product) { | |
$productid = $product->id; | |
$resource = $modx->getObject('modResource',$productid); | |
$template = $resource->get('template'); | |
if($template == 11){ | |
$title = $resource->get('pagetitle'); | |
} | |
$meta = (($resource instanceof scProductResource) ? $resource->getProductMeta() : $sc->getDeprecatedProductMeta($resource)); | |
$modx->log(xPDO::LOG_LEVEL_ERROR, $product->totals['quantity'] .' '. $meta['product_stock']); | |
if( | |
$product->totals['quantity'] > $meta['product_stock'] && $meta['product_stock'] !== null | |
){ | |
foreach($controller->storageData['products'] as $key=>$value){ | |
if($value['productid'] == $productid){ | |
$controller->storageData['products'][$key]['quantity'] = $meta['product_stock']; | |
if($meta['product_stock'] <= 0){ | |
unset($controller->storageData['products'][$key]); | |
} | |
$modx->log(xPDO::LOG_LEVEL_ERROR, $productid); | |
$controller->setStorageData(); | |
$url = $modx->makeUrl($modx->resource->get('id'), '', array('quantity'=>'adjusted'), 'https'); | |
$modx->sendRedirect($url); | |
} | |
} | |
} | |
} | |
if(isset($_GET['quantity']) && $_GET['quantity'] == 'adjusted'){ | |
echo '<div class="btn success">Product quantities have been adjusted based on current availability.</div>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment