Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Last active September 16, 2024 07:30
Show Gist options
  • Save plugin-republic/79d14b9c832c978b2101b0e41cfa99c5 to your computer and use it in GitHub Desktop.
Save plugin-republic/79d14b9c832c978b2101b0e41cfa99c5 to your computer and use it in GitHub Desktop.
<?php
function pewc_ajax_add_child_product_to_cart() {
$result = WC()->cart->add_to_cart( intval( $_POST['product_id'] ), intval( $_POST['quantity'] ) );
$result ? wp_send_json_success() : wp_send_json_error();
}
add_action('wp_ajax_pewc_add_child_product_to_cart', 'pewc_ajax_add_child_product_to_cart');
add_action('wp_ajax_nopriv_pewc_add_child_product_to_cart', 'pewc_ajax_add_child_product_to_cart');
add_action( "wp_footer", function() {
?>
<script>
jQuery(document).ready(function($) {
$('.pewc-column-wrapper.child-product-wrapper .pewc-add-button:not(.pewc-added)').off("click").text("<?php echo __('Add to cart', 'pewc') ?>");
$('.pewc-column-wrapper.child-product-wrapper .pewc-add-button:not(.pewc-added)').parent()
.find('.pewc-child-quantity-field').removeClass('pewc-child-quantity-field').attr('min', 1).val(1).css('width', '75px');
$('.pewc-column-wrapper.child-product-wrapper .pewc-variable-child-select').off("change click");
$('.pewc-column-wrapper.child-product-wrapper').on('click', function(e) {
// only allow 'Add' button to select child product, allow quickview
if ( ! $(e.target).hasClass('pewc-child-quantity-field') && ! $(e.target).hasClass('pewc-show-quickview') ) {
e.preventDefault();
e.stopPropagation();
// reverse auto-selection when opening quickview
if ( $(e.target).hasClass('pewc-column-form-field') ) {
$checkbox_image_wrapper = $(e.target).closest('.pewc-checkbox-image-wrapper');
if ( $checkbox_image_wrapper.hasClass('checked') ) {
$checkbox_image_wrapper.find('.pewc-add-button.pewc-added').click();
} else {
$checkbox_image_wrapper.find('.pewc-add-button:not(.pewc-added)').click();
}
}
}
// quantity field - prevent form submission on enter key
if ( $(e.target).hasClass('pewc-form-field') && $(e.target).parent().hasClass('pewc-quantity-wrapper') ) {
$('form.cart').on('click keydown', $(e.target), function(event) {
if ( event.key === 'Enter' ) {
event.preventDefault();
event.stopPropagation();
}
});
}
});
$('.pewc-column-wrapper.child-product-wrapper .pewc-add-button:not(.pewc-added)').on('click', function(e) {
let $child_product_add_btn = $(this);
let quantity = $(this).closest('.pewc-checkbox-image-wrapper').find('.pewc-quantity-wrapper input').val();
if ( $(this).closest('.child-product-wrapper').hasClass('products-quantities-linked') ) {
quantity = $(this).closest('form.cart').find('input.qty').val();
} else if ( ! quantity ) {
quantity = 1;
}
let product_id = $(this).closest('.pewc-checkbox-image-wrapper').find('.pewc-column-form-field').val();
if ( $(this).closest('.pewc-checkbox-image-wrapper').hasClass('pewc-variable-child-product-wrapper') ) {
product_id = $(this).closest('.pewc-checkbox-image-wrapper').find('.pewc-variable-child-select').val();
}
// prevent multiple clicks
$child_product_add_btn.css('pointer-events', 'none');
$.ajax({
url: pewc_vars.ajaxurl,
type: 'POST',
data: {
action: 'pewc_add_child_product_to_cart',
product_id: product_id,
quantity: quantity
},
success: function( response ) {
if ( response.success ) {
$child_product_add_btn.css({
"color": "#333333",
"background-color": "#eeeeee",
"border-color": "#eeeeee"
}).text("<?php echo __('Added to cart', 'pewc') ?>");
$child_product_add_btn.parent().find('input').prop('disabled', true);
$('body').trigger("wc_fragment_refresh");
} else {
$child_product_add_btn.css('pointer-events', 'auto');
console.log('Failed to add product to cart.');
}
}
});
});
});
</script>
<?php
}, 9999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment