It says There has been a critical error on this website. Please review my code
cart.php:
<?php
/**
* The template for displaying the cart page.
*
* This file has been modified to support selective checkout with checkboxes.
*
* @package woodmart
*/
defined( 'ABSPATH' ) || exit;
do_action( 'woocommerce_before_cart' ); ?>
<form class="woocommerce-cart-form" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' ); ?>
<table class="shop_table shop_table_responsive cart woocommerce-cart-form__contents" cellspacing="0">
<thead>
<tr>
<th class="product-select"><input type="checkbox" id="select-all-items"> Select All</th>
<th class="product-name"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>
<th class="product-price"><?php esc_html_e( 'Price', 'woocommerce' ); ?></th>
<th class="product-quantity"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></th>
<th class="product-subtotal"><?php esc_html_e( 'Subtotal', 'woocommerce' ); ?></th>
<th class="product-remove"> </th>
</tr>
</thead>
<tbody>
<?php do_action( 'woocommerce_before_cart_contents' ); ?>
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<tr class="woocommerce-cart-form__cart-item <?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-select" data-title="<?php esc_attr_e( 'Select', 'woocommerce' ); ?>">
<label>
<input type="checkbox" name="selected_items[]" value="<?php echo esc_attr( $cart_item_key ); ?>" checked>
</label>
functions.php:
// Add checkbox to each cart item
add_filter('woocommerce_cart_item_name', 'add_checkbox_to_cart_item', 10, 3);
function add_checkbox_to_cart_item($item_name, $cart_item, $cart_item_key) {
$checked = WC()->session->__isset('selected_items') && in_array($cart_item_key, WC()->session->get('selected_items')) ? 'checked' : 'checked';
$checkbox = '<label><input type="checkbox" name="selected_items[]" value="' . esc_attr($cart_item_key) . '" ' . $checked . '> ' . $item_name . '</label>';
return $checkbox;
}
// Handle AJAX request to update selected items
add_action('wp_ajax_update_selected_cart_items', 'handle_update_selected_cart_items');
add_action('wp_ajax_nopriv_update_selected_cart_items', 'handle_update_selected_cart_items');
function handle_update_selected_cart_items() {
if (isset($_POST['selected_items']) && is_array($_POST['selected_items'])) {
$selected_items = array_map('sanitize_text_field', $_POST['selected_items']);
WC()->session->set('selected_items', $selected_items);
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
if (!in_array($cart_item_key, $selected_items)) {
WC()->cart->set_quantity($cart_item_key, 0);
} else {
WC()->cart->set_quantity($cart_item_key, $cart_item['quantity']);
}
}
wp_send_json_success();
}
wp_send_json_error();
}
// Only include selected items in order
add_filter('woocommerce_add_to_order_from_cart_item', 'filter_items_during_checkout', 10, 3);
function filter_items_during_checkout($bool, $cart_item_key, $order) {
$selected_items = WC()->session->get('selected_items', []);
if (!in_array($cart_item_key, $selected_items)) {
return false;
}
return $bool;
}
// Reset session after checkout
add_action('woocommerce_thankyou', 'reset_selected_items_session');
function reset_selected_items_session($order_id) {
WC()->session->__unset('selected_items');
}
js/custom-cart.js
jQuery(document).ready(function($) {
// Add "Select All" checkbox dynamically to cart (only if not already present)
if ($('#select-all-items').length === 0) {
$('.woocommerce-cart-form__contents').before(
'<div class="select-all-wrapper" style="margin: 15px 0;">' +
' <label><input type="checkbox" id="select-all-items"> Select All</label>' +
'</div>'
);
}
// Handle checkbox changes for individual items
$(document).on('change', 'input[name="selected_items[]"]', function() {
updateSelectedItemsAndSyncCart();
});
// Handle "Select All" checkbox change
$(document).on('change', '#select-all-items', function() {
var isChecked = $(this).is(':checked');
$('input[name="selected_items[]"]').prop('checked', isChecked);
updateSelectedItemsAndSyncCart();
});
// Function to collect selected items and send AJAX request
function updateSelectedItemsAndSyncCart() {
let selectedItems = [];
// Collect selected item keys
$('input[name="selected_items[]"]:checked').each(function() {
selectedItems.push($(this).val());
});
// Send AJAX request to update cart
$.ajax({
url: wc_cart_params.ajax_url,
type: 'POST',
data: {
action: 'update_selected_cart_items',
selected_items: selectedItems
},
success: function(response) {
if (response) {
// Refresh cart fragments and update checkout
$('body').trigger('wc_fragment_refresh');
$('body').trigger('update_checkout');
// Update "Select All" checkbox state
const allCheckboxes = $('input[name="selected_items[]"]');
const checkedCheckboxes = allCheckboxes.filter(':checked');
$('#select-all-items').prop('checked', allCheckboxes.length === checkedCheckboxes.length);
}
}
});
}
});
whats wrong?