/* RTC_DRY_FISH_CUSTOM_AJAX_ADD_V1: validate and add Dry Fish variable products using WooCommerce cart API. */
function rtc_dry_fish_custom_ajax_add_to_cart() {
    if (!function_exists('WC') || !WC()->cart) {
        wp_send_json(['error' => true, 'message' => 'Cart is not available.'], 503);
    }
    $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
    if (!wp_verify_nonce($nonce, 'rtc_dry_fish_add_to_cart')) {
        wp_send_json(['error' => true, 'message' => 'Please refresh the page and try again.'], 403);
    }
    $product_id   = isset($_POST['product_id']) ? absint($_POST['product_id']) : 0;
    $variation_id = isset($_POST['variation_id']) ? absint($_POST['variation_id']) : 0;
    $quantity     = isset($_POST['quantity']) ? wc_stock_amount(wp_unslash($_POST['quantity'])) : 1;
    if (!in_array($product_id, [50, 51, 52, 53], true) || $quantity < 1) {
        wp_send_json(['error' => true, 'message' => 'Invalid Dry Fish product or quantity.'], 400);
    }
    $parent = wc_get_product($product_id);
    $variation = wc_get_product($variation_id);
    if (!$parent || !$parent->is_type('variable') || !$variation || !$variation->is_type('variation') || (int) $variation->get_parent_id() !== $product_id) {
        wp_send_json(['error' => true, 'message' => 'Please select a valid weight.'], 400);
    }
    $attributes = $variation->get_variation_attributes();
    foreach ($attributes as $key => $expected) {
        $posted = isset($_POST['variation'][$key]) ? sanitize_text_field(wp_unslash($_POST['variation'][$key])) : '';
        if ($posted === '' || $posted !== (string) $expected) {
            wp_send_json(['error' => true, 'message' => 'The selected weight does not match this variation.'], 400);
        }
    }
    $added = WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $attributes);
    if (!$added) {
        wp_send_json(['error' => true, 'message' => 'WooCommerce could not add this selection.'], 400);
    }
    wp_send_json([
        'error' => false,
        'cart_hash' => WC()->cart->get_cart_hash(),
        'cart_count' => WC()->cart->get_cart_contents_count(),
    ]);
}
add_action('wp_ajax_rtc_dry_fish_add_to_cart', 'rtc_dry_fish_custom_ajax_add_to_cart');
add_action('wp_ajax_nopriv_rtc_dry_fish_add_to_cart', 'rtc_dry_fish_custom_ajax_add_to_cart');

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://readytocooks.com/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://readytocooks.com/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://readytocooks.com/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://readytocooks.com/wp-sitemap-posts-product-1.xml</loc></sitemap><sitemap><loc>https://readytocooks.com/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://readytocooks.com/wp-sitemap-taxonomies-product_cat-1.xml</loc></sitemap><sitemap><loc>https://readytocooks.com/wp-sitemap-taxonomies-product_tag-1.xml</loc></sitemap><sitemap><loc>https://readytocooks.com/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
