



add_action('wp_ajax_custom_fast_add_to_cart', 'custom_fast_add_to_cart_handler');
add_action('wp_ajax_nopriv_custom_fast_add_to_cart', 'custom_fast_add_to_cart_handler');

function custom_fast_add_to_cart_handler() {
    if (!isset($_POST['product_id']) || !class_exists('WooCommerce')) {
        wp_send_json_error(['message' => 'Invalid request']);
    }

    $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['product_id']));
    $quantity   = empty($_POST['quantity']) ? 1 : wc_stock_amount($_POST['quantity']);

    // Check if the product is already in the cart BEFORE calling WooCommerce add_to_cart
    if (WC()->cart) {
        foreach (WC()->cart->get_cart() as $cart_item) {
            if ($cart_item['product_id'] == $product_id || $cart_item['variation_id'] == $product_id) {
                // Return success immediately to avoid "quantity limit reached" error
                wp_send_json_success(['already_in_cart' => true]);
            }
        }
    }
    
    $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
    
    if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity)) {
        wp_send_json_success();
    }
    
    wp_send_json_error(['message' => 'Could not add to cart']);
}

// 2. Register the Shortcode
add_shortcode('fast_wc_buttons', 'fast_wc_buttons_shortcode');

function fast_wc_buttons_shortcode($atts) {
    if (!class_exists('WooCommerce')) return '';

    global $product;
    $atts = shortcode_atts(['id' => ''], $atts, 'fast_wc_buttons');
    $product_id = !empty($atts['id']) ? intval($atts['id']) : ($product ? $product->get_id() : 0);
    
    if (!$product_id) return '';

    $checkout_url = wc_get_checkout_url();
    
    // Check if product is already in the cart on initial page load
    $is_in_cart = false;
    if (WC()->cart) {
        foreach (WC()->cart->get_cart() as $cart_item) {
            if ($cart_item['product_id'] == $product_id || $cart_item['variation_id'] == $product_id) {
                $is_in_cart = true;
                break;
            }
        }
    }

    $add_btn_class = 'fwc-btn fwc-add-to-cart' . ($is_in_cart ? ' success-state is-in-cart' : '');
    $add_btn_text  = $is_in_cart ? 'In your Cart' : 'Add to Cart';
    $add_btn_attr  = $is_in_cart ? 'disabled' : '';

    ob_start();
    ?>
    <div class="wc-fast-actions-wrapper" itemscope itemtype="https://schema.org/Offer">
        <!-- SEO Microdata -->
        <meta itemprop="url" content="" />
        
        <button type="button" class="" data-id=""  aria-label="Add product to cart">
            <span class="fwc-text"></span>
        </button>
        
        <button type="button" class="fwc-btn fwc-buy-now" data-id="" data-checkout="" aria-label="Buy product now">
            <span class="fwc-text">Buy Now</span>
        </button>
    </div>
    <?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://simplyartique.com/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://simplyartique.com/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://simplyartique.com/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://simplyartique.com/wp-sitemap-posts-product-1.xml</loc></sitemap><sitemap><loc>https://simplyartique.com/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://simplyartique.com/wp-sitemap-taxonomies-product_cat-1.xml</loc></sitemap><sitemap><loc>https://simplyartique.com/wp-sitemap-taxonomies-product_tag-1.xml</loc></sitemap><sitemap><loc>https://simplyartique.com/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
