function new_scripts() {
// Scripts
wp_enqueue_script( 'popper-js', 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js', array( 'jquery' ), '1.14.7', true );
wp_enqueue_script( 'bootstrap-js', 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js', array( 'jquery' ), '4.3.1', true );
wp_enqueue_script( 'aaa-js', content_url() . '/themes/milena/js/ajax_add_to_cart.js', array( 'jquery' ), '4.3.1', true );
wp_localize_script( 'bootstrap-js', 'ajax_object',
array(
'url' => admin_url('admin-ajax.php'),
)
);
}
add_action( 'wp_enqueue_scripts', 'new_scripts' );
add_action( 'woocommerce_before_add_to_cart_button', 'action_woocommerce_before_variations_form', 10, 0 );
function action_woocommerce_before_variations_form( ) {
global $product;
$variations = $product->get_available_variations();
$variation_ids = '';
foreach ($variations as $variation_one) {
$variation_ids .= $variation_one['variation_id'].':';
}
?>
<div class="checkbox_wrapp" >
<div class="checkbox_sizes_all">
<input type="checkbox" id="check-sizes-all" value="<?php echo $variation_ids; ?>">
<label for="check-sizes-all">
<span class="option-name">Весь размерный ряд</span>
</label>
</div>
<div id="all_into_cart" style="">В корзину</div>
</div>
<?php
};
add_action('wp_ajax_ajax_cstm_add_to_cart', 'ajax_cstm_add_to_cart' );
add_action('wp_ajax_nopriv_ajax_cstm_add_to_cart', 'ajax_cstm_add_to_cart' );
function ajax_cstm_add_to_cart() {
global $woocommerce;
$variations = $_POST['variations_ids'];
$variations_kol = $_POST['variations_kol'];
$variations_ids = explode( ":", $variations );
foreach ($variations_ids as $variation) {
$woocommerce->cart->add_to_cart( $variation, $variations_kol );
}
$return = array(
'success' => true,
);
wp_send_json($return);
}
jQuery(document).ready(function(){
jQuery("#check-sizes-all").on("click", function () {
if( jQuery('#all_into_cart').hasClass('active') ){
jQuery('#all_into_cart').removeClass('active');
jQuery('#all_into_cart').fadeOut(10);
} else{
jQuery('#all_into_cart').fadeIn(10);
jQuery('#all_into_cart').addClass('active');
}
});
jQuery("#all_into_cart").on("click", function () {
if( jQuery('#check-sizes-all').prop("checked") == true ){
var variations_ids = jQuery('#check-sizes-all').val();
var variations_kol = jQuery('.product .cart .quantity .input-text').val();
let data = {
action: "ajax_cstm_add_to_cart",
variations_ids: variations_ids,
variations_kol: variations_kol,
};
jQuery.ajax({
url: ajax_object.url,
data: data,
dataType: 'JSON',
type: "POST",
success: function (response) {
if (response) {
location.reload();
}
}
});
}
});
});