/*!
Theme Name: Hello Elementor Child
Theme URI:a https://elementor.com/hello-theme/?utm_source=wp-themes&utm_campaign=theme-uri&utm_medium=wp-dash
Description: A plain-vanilla & lightweight theme for Elementor page builder
Author: Elementor Team
Author URI: https://elementor.com/?utm_source=wp-themes&utm_campaign=author-uri&utm_medium=wp-dash
Template: hello-elementor
Version: 1.0.0
License: GNU General Public License v3 or later.
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Text Domain: hello-elementor
*/

/*  
 * 
 * 
function wc_products_table_shortcode() {
    ob_start();
    ?>
    <table id="wc-products-table">
        <thead>
            <tr>
                <th><input type="checkbox" id="select-all"></th>
                <th>Product</th>
                <th>Price</th>
            </tr>
        </thead>
        <tbody>
        <?php
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => -1,
            'meta_key' => '_price',
            'orderby' => 'meta_value_num',
            'order' => 'ASC'
        );
        $products = new WP_Query( $args );
        if ( $products->have_posts() ) {
            while ( $products->have_posts() ) {
                $products->the_post();
                $product = wc_get_product( get_the_ID() );
                $price = $product->get_price();
                ?>
                <tr>
                    <td><input type="checkbox" name="product-ids[]" value="<?php the_ID(); ?>"></td>
                    <td><?php the_title(); ?></td>
                    <td><?php echo wc_price( $price ); ?></td>
                </tr>
                <?php
            }
        }
        wp_reset_postdata();
        ?>
        </tbody>
    </table>
    <button id="add-to-cart-button">Add to Cart</button>
    <?php
    return ob_get_clean();
}
add_shortcode( 'wc_products_table', 'wc_products_table_shortcode' );

function wc_add_to_cart_button_script() {
    ?>
    <script>
    jQuery(document).ready(function($) {
        $('#add-to-cart-button').click(function() {
            var productIds = [];
            $('input[name="product-ids[]"]:checked').each(function() {
                productIds.push($(this).val());
            });
            if (productIds.length > 0) {
                $.ajax({
                    type: 'POST',
                    url: wc_add_to_cart_params.ajax_url,
                    data: {
                        action: 'woocommerce_ajax_add_products',
                        product_ids: productIds,
                        security: wc_add_to_cart_params.add_to_cart_nonce
                    },
                    success: function(response) {
                        console.log(response);
                        alert('Products added to cart!');
                    }
                });
            }
        });
    });
    </script>
    <?php
}
// add_action( 'wp_enqueue_scripts', 'wc_add_to_cart_button_script' );
 * 
 * */