<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;

// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:

if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
    function chld_thm_cfg_locale_css( $uri ){
        if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
            $uri = get_template_directory_uri() . '/rtl.css';
        return $uri;
    }
endif;
add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
         
if ( !function_exists( 'child_theme_configurator_css' ) ):
    function child_theme_configurator_css() {
        wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'kadence-global','kadence-header','kadence-content','kadence-woocommerce','kadence-footer' ) );
    }
endif;
add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 10 );

// END ENQUEUE PARENT ACTION

/**
 * SUUNNITTELIJATIEDON LISÄYS TUOTELISTAUKSEEN
 */
function sly_switch_loop_title(){
     remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
    add_action( 'woocommerce_before_shop_loop_item_title', 'sly_template_loop_product_title', 15 );
}
add_action( 'woocommerce_before_shop_loop_item_title', 'sly_switch_loop_title' );

function sly_template_loop_product_title() {
	 echo '<h5 class="suunnittelija-archive">' . get_field( 'suunnittelija' ) . '</h5>';
}

add_action( 'woocommerce_after_shop_loop_item_title', 'read_more' );

function read_more() {
    global $product;
    if ( $product ) {
        $url = esc_url( $product->get_permalink() );
        echo '<a rel="nofollow" href="' . $url . '" class="read_more">tutustu</a>';
    }
}

/*camweara code starts*/

function add_theme_scripts() {
  wp_enqueue_style( 'style', get_stylesheet_uri() );
   wp_enqueue_script( 'script', 'https://camweara.com/integrations/camweara_api.js', array ( 'jquery' ), 1.12, true);
	
    if (is_singular( 'product' ) ) { 
  		
        global $post;
        $product = wc_get_product( $post->ID );
        $product_sku_list = $product->get_sku();

        $product_sku = array(
            'product_sku' => __($product_sku_list, 'camweara'),
        );
        
		echo "<script type='text/javascript'>
			function onTryOnBuynowClick(arg){
				console.log('onTryOnBuynowClick', arg);
				 document.querySelector('.single_add_to_cart_button').click();
			}
		</script>";
		
        echo "<script type='text/javascript'>
			window.onload = function() {
			console.log('window onloaded');
			loadTryOnButton({
			psku: '{$product_sku_list}',
			page:'product',
			company:'company_name',
			buynowCallback:'onTryOnBuynowClick',
			appendButton:{class:'elementor-widget-woocommerce-product-price'},
			styles:{
			tryonbutton:{backgroundColor:'transparent',color:'white',height:'auto', width:'140px', marginLeft:'40px', marginBottom:'20px'},
			tryonbuttonHover:{transform:'scale(1.1)'}
			},
			tryonButtonData:{text:'Virtual Try On'},
			})
			};
			</script>";

    }
 
    /*if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
      wp_enqueue_script( 'comment-reply' );
    }*/
}
add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );

/*camweara code ends*/