/*
Theme Name: Flatsome Child
Description: This is a child theme for Flatsome Theme
Author: UX Themes
Template: flatsome
Version: 3.0
*/
.woocommerce-terms-and-conditions-checkbox-text{
	font-size: 13px;
	line-height: 20px;
	font-weight: 400;
	margin-bottom: 30px;
	display: block;
}

/** main corlor button: #40b705 */
.button#place_order, .button.checkout, .checkout-button, .single_add_to_cart_button.button {
    display: block;
    width: 100%;
    line-height: 50px;
    border-radius: 3px;
    font-size: 1.1em;
    letter-spacing: 2px;
}

.callout.badge.badge-circle{
	display: none;
}

.product-info span.amount {
    white-space: nowrap;
    color: #f44336;
    font-weight: 700;
    font-size: 1.2em;
}

.sn_size_chart_wrp{
	display: block;
}
.faqs-tips .accordion-item {
    margin-bottom: 10px;
}
.faqs-tips .accordion-title {
    background-color: #fff;
    cursor: pointer;
    border: none;
    border-radius: 5px;
}
/*************** ADD CUSTOM CSS HERE.   ***************/
#review_form .review-form-inner.has-border{
	
}

@media only screen and (max-width: 48em) {
/*************** ADD MOBILE ONLY CSS HERE  ***************/


}

.sn-mobile-mini-cart-checkout .shipping__table{
	display: none;
}
@media only screen and (max-width: 999px) {
	.sn-mobile-mini-cart-checkout{
		display: block!important;
	}
}

/// xóa giá ở productpage
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );




// Thay đổi vị trí hiển thị Related Product
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 5 );
// // Shortcode hien thi anh attachment trong description
add_shortcode('thien_display_attachment_images', 'podcustome_display_attachment_images');
function podcustome_display_attachment_images() {
	global $product;

	if (isset($product)){
		$output = array();
		$title = $product->get_name();
		$image_id = $product->get_image_id();
		$image_src = wp_get_attachment_url($image_id);
		$html = '<img class="size-medium wp-image-287 aligncenter" src="'.$image_src.'" alt="'.$title.'" title="'.$title.'" width="650" height="650" />';
		$output[] = $html;

		$attachment_ids = $product->get_gallery_attachment_ids();

		foreach($attachment_ids as $attachment_id){
			$image_src = wp_get_attachment_url($attachment_id);

			$html = '<img class="size-medium wp-image-287 aligncenter" src="'.$image_src.'" alt="'.$title.'" title="'.$title.'" width="650" height="650" />';
			// $html = wp_get_attachment_image($attachment_id);
			$output[] = $html;
		}
		return implode(",",$output);
	}
	return;
}
function get_related_products_by_popularity() {
    // Lấy ID của bài viết hiện tại
    $post_id = get_the_ID();

    // Lấy danh sách thẻ của bài viết
    $tags = wp_get_post_terms($post_id, 'product_tag', ['fields' => 'ids']);

    // Truy vấn lấy 4 sản phẩm phổ biến thuộc tag đó
    $args = [
        'post_type' => 'product',
        'tax_query' => [
            [
                'taxonomy' => 'product_tag',
                'field'    => 'id',
                'terms'    => $tags,
                'operator' => 'IN'
            ],
        ],
        'post__not_in' => [$post_id], // Loại bỏ sản phẩm hiện tại
        'posts_per_page' => 4,
        'meta_key' => '_wc_review_count', // Key cho số lượng lượt mua
        'orderby' => 'meta_value_num', // Sắp xếp theo số lượng lượt mua
        'order' => 'DESC' // Lấy sản phẩm có số lượt mua cao nhất
    ];
    $query = new WP_Query($args);

    // Xây dựng nội dung để hiển thị
    $output = '<h3>Related Products:</h3>';
    $output .= '<ol>';
    while ($query->have_posts()) {
        $query->the_post();
        $output .= '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
    }
    $output .= '</ol>';

    // Reset lại post data sau khi truy vấn
    wp_reset_postdata();

    return $output;
}

// Tạo shortcode
function display_link_product_related_shortcode($atts, $content = null) {
    return get_related_products_by_popularity();
}
add_shortcode('display_link_product_related', 'display_link_product_related_shortcode');
// short code hieen thi tags trong description
add_shortcode('thien_display_tags', 'podcustome_display_tags');
function podcustome_display_tags(){
	global $product;
	$output = array();

	if (isset($product)) {
		$terms = wp_get_post_terms(get_the_id(), 'product_tag');
		foreach( $terms as $term) {
			$term_link = get_term_link( $term );
			$output[] = '<a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>';//$term_name;
		}
		return implode(", ", $output);
	}
	return;
}
#code báo tele thêm sản phẩm / thay id nhóm và token tele
add_action('woocommerce_add_to_cart', 'send_telegram_message', 10, 6);

function send_telegram_message($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    $bot_token = '6412524327:AAGX6ReGOfqtRsRNftdzM9OfFjHrgvzI9qA';
    $chat_id = '-4253913849';
    $product = wc_get_product($product_id);
    $domain = parse_url(get_site_url(), PHP_URL_HOST); 
    $product_link = get_permalink($product_id);

    $message = " Một sản phẩm mới được thêm vào tại $domain:\n";
    $message .= "Tên sản phẩm: " . $product->get_name() . "\n";
    $message .= "Link sản phẩm: $product_link";

    $url = "https://api.telegram.org/bot$bot_token/sendMessage?chat_id=$chat_id&text=" . urlencode($message);
    $args = array('timeout' => '5');
    $response = wp_remote_get($url, $args);
}