Last active
June 14, 2021 22:10
-
-
Save ig0r74/359ad8a5f231a8dc6e0dd7a1b17ec430 to your computer and use it in GitHub Desktop.
Добавление нескольких товаров в корзину MODX miniShop2
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
$('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); | |
}) |
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 | |
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