Skip to content

Instantly share code, notes, and snippets.

@ig0r74
Last active June 14, 2021 22:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ig0r74/359ad8a5f231a8dc6e0dd7a1b17ec430 to your computer and use it in GitHub Desktop.
Save ig0r74/359ad8a5f231a8dc6e0dd7a1b17ec430 to your computer and use it in GitHub Desktop.
Добавление нескольких товаров в корзину MODX miniShop2
$('button#cart_add').click(function(e){ // не забудьте изменить селектор
// Добавляем товары в корзину
var products = [];
$('input[name="price"]:checked').each(function () {
var product = {};
product['id'] = $(this).data('dop');
product['count'] = $(this).parents('.tm-dop').find('input.number-input').val();
products.push(product);
});
// Добавляем услуги в корзину
var services = [];
$('.add-serv input:checked').each(function () {
var service = {};
service['id'] = $(this).data('dop');
service['count'] = 1;
services.push(service);
});
var merged_products = $.merge(products, services);
merged_products = JSON.stringify(merged_products);
setTimeout(function() {
// данные принимаются в плагине MODX
$.ajax({
type: "POST",
url: document.location.href,
data: {
custom_ms2_action: 'cart/add',
products: merged_products,
count: 1,
options: {}
}
});
}, 500);
})
<?php
switch ($modx->event->name) {
case 'OnHandleRequest':
// Handle ajax requests
$isAjax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
if (empty($_REQUEST['custom_ms2_action']) || !$isAjax) {
return;
}
// Данные формируются в js-файле
/** @var miniShop2 $miniShop2 */
if ($miniShop2 = $modx->getService('miniShop2')) {
$miniShop2->initialize($modx->context->key);
if ($_REQUEST['custom_ms2_action'] == 'cart/add') {
$products = json_decode($_POST['products']);
foreach ($products as $product) {
$miniShop2->cart->add($product->id, $product->count, array());
}
}
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment