<?php
defined('ABSPATH') || exit;

if (! function_exists('reeid_copy_wc_data_to_translation')) {
    function reeid_copy_wc_data_to_translation($src_id, $new_id)
    {
        if (get_post_type($src_id) !== 'product' || get_post_type($new_id) !== 'product') {
            return;
        }

        $wc_keys = [
            '_regular_price',
            '_sale_price',
            '_price',
            '_stock',
            '_stock_status',
            '_manage_stock',
            '_sku',
            '_downloadable',
            '_virtual',
            '_weight',
            '_length',
            '_width',
            '_height',
            '_tax_class',
            '_tax_status',
            '_product_attributes',
            '_product_version',
            'total_sales',
            '_sold_individually',
            '_backorders'
        ];

        foreach ($wc_keys as $key) {
            $val = get_post_meta($src_id, $key, true);
            if ($val !== '') {
                update_post_meta($new_id, $key, $val);
            }
        }

        $thumb = get_post_thumbnail_id($src_id);
        if ($thumb) {
            set_post_thumbnail($new_id, $thumb);
        }

        $gallery = get_post_meta($src_id, '_product_image_gallery', true);
        if ($gallery) {
            update_post_meta($new_id, '_product_image_gallery', $gallery);
        }

        $taxes = ['product_cat', 'product_tag', 'product_type', 'product_visibility'];
        if (function_exists('wc_get_attribute_taxonomies')) {
            foreach ((array) wc_get_attribute_taxonomies() as $att) {
                $taxes[] = 'pa_' . $att->attribute_name;
            }
        }

        foreach ($taxes as $tax) {
            $terms = wp_get_object_terms($src_id, $tax, ['fields' => 'ids']);
            if (! is_wp_error($terms) && $terms) {
                wp_set_object_terms($new_id, $terms, $tax, false);
            }
        }
    }
}