function rd_tf_sync_tour_product($tour_id)
{
    $tour = get_post($tour_id);

    if (!$tour || $tour->post_type != 'tf_tours') {
        return false;
    }

    // Đã map rồi?
    $product_id = (int) get_post_meta($tour_id, 'product_id', true);

    if ($product_id && get_post_type($product_id) == 'product') {

        update_post_meta($product_id, '_tour_id', $tour_id);

        $product = wc_get_product($product_id);

        if ($product) {

            $product->set_name($tour->post_title);

            $product->save();

            return $product_id;
        }
    }

    /*
     * Tìm Product đã liên kết bằng _tour_id
     */
    $posts = get_posts(array(
        'post_type' => 'product',
        'post_status' => array('publish','draft','private'),
        'meta_key' => '_tour_id',
        'meta_value' => $tour_id,
        'posts_per_page' => 1
    ));

    if ($posts) {

        $product_id = $posts[0]->ID;

        update_post_meta($tour_id,'product_id',$product_id);

        return $product_id;
    }

    /*
     * Tìm Product cùng tên
     */

    $posts = get_posts(array(
        'post_type'=>'product',
        'title'=>$tour->post_title,
        'post_status'=>array('publish','draft'),
        'posts_per_page'=>1
    ));

    if($posts){

        $product_id=$posts[0]->ID;

        update_post_meta($tour_id,'product_id',$product_id);

        update_post_meta($product_id,'_tour_id',$tour_id);

        return $product_id;

    }

    /*
     * Chưa có -> tạo mới
     */

    $product = new WC_Product_Simple();

    $product->set_name($tour->post_title);

    $product->set_status('publish');

    $product->set_catalog_visibility('hidden');

    $product->set_virtual(true);

    $product->set_regular_price('0');

    $product->save();

    $product_id=$product->get_id();

    update_post_meta($tour_id,'product_id',$product_id);

    update_post_meta($product_id,'_tour_id',$tour_id);

    return $product_id;
}





add_action('init', function () {

    if (!isset($_GET['sync_tour_products'])) {
        return;
    }

    if (!current_user_can('manage_options')) {
        wp_die('Permission denied');
    }

    @set_time_limit(0);

    $tours = get_posts(array(
        'post_type'      => 'tf_tours',
        'posts_per_page' => -1,
        'post_status'    => 'any'
    ));

    echo "<h2>Tourfic Product Sync</h2>";

    foreach ($tours as $tour) {

        $id = rd_tf_sync_tour_product($tour->ID);

        echo "Tour {$tour->ID} → Product {$id}<br>";
        flush();
    }

    exit('<br><br><strong>DONE</strong>');
});<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="//rockiesdreamtravel.ca/main-sitemap.xsl"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<sitemap>
		<loc>https://rockiesdreamtravel.ca/post-sitemap.xml</loc>
		<lastmod>2026-05-09T07:11:34+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://rockiesdreamtravel.ca/page-sitemap.xml</loc>
		<lastmod>2026-07-27T10:08:19+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://rockiesdreamtravel.ca/product-sitemap.xml</loc>
		<lastmod>2026-07-13T08:49:16+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://rockiesdreamtravel.ca/tf_tours-sitemap.xml</loc>
		<lastmod>2026-08-02T08:58:18+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://rockiesdreamtravel.ca/category-sitemap.xml</loc>
		<lastmod>2026-05-09T07:11:34+00:00</lastmod>
	</sitemap>
</sitemapindex>
<!-- XML Sitemap generated by Rank Math SEO Plugin (c) Rank Math - rankmath.com -->