Guided Rec - Wordpress/WooCommerce

Implementation on Wordpress/WooCommerce Platform

The following code can be placed in your theme's functions.php file.
If your theme is updatable, you will need to add the code block again to your theme’s functions.php file. Once implemented, save the code block to a text file in order to copy and paste.

add_action('wp_enqueue_scripts', 'guidedRecAssets', 10); 
add_action('wp_ajax_nopriv_get_woocommerce_card', 'getWoocommerceCard'); // Enables Ajax Fn to get Woocommerce Item Grid
add_action('wp_ajax_get_woocommerce_card', 'getWoocommerceCard');
add_action('wp_footer', 'addPreferabliGRJs', 9999); // Adds JS to footer of website

if (!function_exists('guidedRecAssets')) {
    function guidedRecAssets()
    {
        wp_register_script('guidedrec_js', 'https://guidedrec.preferabli.com/preferabli.guidedrec.js', array('jquery'), '', true);
        wp_enqueue_script('guidedrec_js');

        wp_register_style('guidedrec_css', 'https://guidedrec.preferabli.com/preferabli.guidedrec.style.css', array(), '', 'all');
        wp_enqueue_style('guidedrec_css');
    }
}

if (!function_exists('addPreferabliGRJs')) {
    function addPreferabliGRJs(){
?>

        <script type="text/javascript">
        (function() {
        

        var _guidedrec = $('body').preferabliGR({
        integration_id: %INTEGRATION_ID%,
        questionnaire_id: %QUESTIONNAIRE_ID%,
        openNewTab: true,
        resultsRedirect: `%URL_REDIRECT%`,
        renderProductCards: (lookups) => {

            const fetchCards = new Promise((resolve, reject) => {
                const post__in = [...lookups].map((lookup) => {
                    const { value } = lookup;
                    return Number(value);
                });

                const response = jQuery.parseJSON(jQuery.ajax({
                type : "POST",
                async: false,
                dataType : "json",
                url : "<?php echo admin_url('admin-ajax.php'); ?>",
                data : {
                    action: "get_woocommerce_card",
                    post__in: post__in,
                },
                }).responseText);

                if(response) {
                    resolve(response.data);
                }   
            });

            return fetchCards.then((htmlString) => (htmlString)).catch((error) => (error));
        }
        });

         <?php  if(!is_page_template('template-results.php')): ?> // Call to Action to display on all other pages

         $('.trigger-modal-q1, .special-class').on('click', function () {
             _guidedrec.preferabliGR('show');
         });

         <?php endif; ?>
 
         <?php  if(is_page_template('template-results.php')): ?> // set to results page template

        _guidedrec.preferabliGR('show', {
            type: 'results',
            element: $(%DIV_ELEMENT%),
        });

         <?php endif; ?>
      

        })(jQuery);
        </script>

        <?php
    }
}

if(!function_exists('getWoocommerceCard')){
    function getWoocommerceCard() {
        $cards = array();
        
        $post__in = array_map('intval', $_REQUEST['post__in']);
        
        $args = array(
            'post_type' => 'product',
            'post__in' => $post__in,
            );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : 
                ob_start();
                $loop->the_post();
                wc_get_template_part( 'content', 'product' );
                $card_html = ob_get_contents();
                ob_end_clean();
                $cards[] = $card_html;
            endwhile;
        }

        wp_send_json_success( $cards );
    }
}