Exemplo n.º 1
0
/**
 Function and action for duplicating products,
*/
function wpsc_duplicate_product()
{
    global $wpdb;
    $product_id = absint($_GET['product']);
    check_admin_referer('duplicate_product_' . $product_id);
    if ($product_id > 0) {
        $sql = " INSERT INTO " . WPSC_TABLE_PRODUCT_LIST . "( `name` , `description` , `additional_description` , `price` , `weight` , `weight_unit` , `pnp` , `international_pnp` , `file` , `image`  , `quantity_limited` , `quantity` , `special` , `special_price` , `display_frontpage` , `notax` , `active` , `publish`, `donation` , `no_shipping` , `thumbnail_image` , `thumbnail_state` ) SELECT `name` , `description` , `additional_description` , `price` , `weight` , `weight_unit` , `pnp` , `international_pnp` , `file` , `image`  , `quantity_limited` , `quantity` , `special` , `special_price` , `display_frontpage` , `notax` , `active`  , `publish`, `donation` , `no_shipping` , `thumbnail_image` , `thumbnail_state` FROM " . WPSC_TABLE_PRODUCT_LIST . " WHERE id = '" . $product_id . "' ";
        //	exit($sql);
        $wpdb->query($sql);
        $new_id = $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` LIMIT 1");
        //Inserting duplicated variations record.
        $nzshpcrt_variations = new nzshpcrt_variations();
        $nzshpcrt_variations->duplicate_variation_values($new_id, $product_id);
        /*
        		$variation_assocs = $wpdb->get_results("SELECT * FROM ".WPSC_TABLE_VARIATION_ASSOC." WHERE associated_id = ".$product_id, ARRAY_A);
        		if(count($variation_assocs))foreach($variation_assocs as $variation_assoc){
        			$wpdb->query("INSERT INTO ".WPSC_TABLE_VARIATION_ASSOC." VALUES ('', '".$variation_assoc['type']."', '".$variation_assoc['name']."', '".$new_id."', '".$variation_assoc['variation_id']."');");
        		}
        		
        		$variation_combinations = $wpdb->get_results("SELECT * FROM ".WPSC_TABLE_VARIATION_COMBINATIONS." WHERE product_id = ".$product_id, ARRAY_A);
        		if(count($variation_combinations))foreach($variation_combinations as $variation_combination){
        			$variation_properties = $wpdb->get_results("SELECT * FROM ".WPSC_TABLE_VARIATION_PROPERTIES." WHERE id = ".$variation_combination['priceandstock_id'], ARRAY_A);
        			exit('<pre>'.print_r($variation_properties, true).'</pre>');
        			$wpdb->query("INSERT INTO ".WPSC_TABLE_VARIATION_PROPERTIES." VALUES ('', '".$new_id."', '".$variation_properties[0]['stock']."', '".$variation_properties[0]['price']."', '".$variation_properties[0]['weight']."', '".$variation_properties[0]['weight_unit']."', '".$variation_properties[0]['visibility']."', '".$variation_properties[0]['file']."');");
        			$new_prop_id= $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `".WPSC_TABLE_VARIATION_PROPERTIES."` LIMIT 1");
        			$wpdb->query("INSERT INTO ".WPSC_TABLE_VARIATION_COMBINATIONS." VALUES ('".$new_id."', '".$new_prop_id."', '".$variation_combination['value_id']."', '".$variation_combination['variation_id']."', '".$variation_combination['all_variation_ids']."');");			
        		}
        		
        		$variation_values_assocs = $wpdb->get_results("SELECT * FROM ".WPSC_TABLE_VARIATION_VALUES_ASSOC." WHERE product_id = ".$product_id, ARRAY_A);
        		if(count($variation_values_assocs))foreach($variation_values_assocs as $variation_values_assoc){
        			$wpdb->query("INSERT INTO ".WPSC_TABLE_VARIATION_VALUES_ASSOC." VALUES ('', '".$new_id."', '".$variation_values_assoc['value_id']."', '".$variation_values_assoc['visible']."', '".$variation_values_assoc['variation_id']."');");
        		}
        		//end of variations
        */
        //Inserting duplicated category record.
        $category_assoc = $wpdb->get_col("SELECT `category_id` FROM " . WPSC_TABLE_ITEM_CATEGORY_ASSOC . " WHERE product_id = '" . $product_id . "'");
        $new_product_category = array();
        if (count($category_assoc) > 0) {
            foreach ($category_assoc as $key => $category) {
                $new_product_category[] = "('" . $new_id . "','" . $category . "')";
                $check_existing = $wpdb->get_results("SELECT * FROM `" . WPSC_TABLE_PRODUCT_ORDER . "` WHERE `category_id` IN('{$category}') AND `order` IN('0') LIMIT 1;", ARRAY_A);
                if ($wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_PRODUCT_ORDER . "` WHERE `category_id` IN('{$category}') AND `product_id` IN('{$product_id}') LIMIT 1")) {
                    $wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_ORDER . "` SET `order` = '0' WHERE `category_id` IN('{$category}') AND `product_id` IN('{$product_id}') LIMIT 1;");
                } else {
                    $wpdb->query("INSERT INTO `" . WPSC_TABLE_PRODUCT_ORDER . "` (`category_id`, `product_id`, `order`) VALUES ('{$category}', '{$product_id}', 0)");
                }
                if ($check_existing != null) {
                    $wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_ORDER . "` SET `order` = (`order` + 1) WHERE `category_id` IN('{$category}') AND `product_id` NOT IN('{$product_id}') AND `order` < '0'");
                }
            }
            $wpdb->query("INSERT INTO " . WPSC_TABLE_ITEM_CATEGORY_ASSOC . " (product_id, category_id) VALUES " . implode(",", $new_product_category));
        }
        //Inserting duplicated meta info
        $meta_values = $wpdb->get_results("SELECT `meta_key`, `meta_value`, `custom` FROM `" . WPSC_TABLE_PRODUCTMETA . "` WHERE product_id='" . $product_id . "' AND  `meta_key` NOT IN ('url_name') ", ARRAY_A);
        $new_meta_value = array();
        if (count($meta_values) > 0) {
            foreach ($meta_values as $key => $meta) {
                $new_meta_value[] = "('" . $new_id . "','" . $meta['meta_key'] . "','" . $meta['meta_value'] . "','" . $meta['custom'] . "')";
            }
            $wpdb->query("INSERT INTO `" . WPSC_TABLE_PRODUCTMETA . "` (`product_id`, `meta_key`, `meta_value`, `custom`) VALUES " . implode(",", $new_meta_value));
        }
        $product_name = $wpdb->get_var("SELECT `name` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE `id` = '{$new_id}' LIMIT 1");
        if ($product_name != '') {
            $tidied_name = strtolower(trim($product_name));
            $url_name = sanitize_title($tidied_name);
            $similar_names = $wpdb->get_row("SELECT COUNT(*) AS `count`, MAX(REPLACE(`meta_value`, '{$url_name}', '')) AS `max_number` FROM `" . WPSC_TABLE_PRODUCTMETA . "` WHERE `meta_key` IN ('url_name') AND `meta_value` REGEXP '^({$url_name}){1}[[:digit:]]*\$' ", ARRAY_A);
            $extension_number = '';
            if ($similar_names['count'] > 0) {
                $extension_number = (int) $similar_names['max_number'] + 1;
            }
            $url_name .= $extension_number;
            add_product_meta($new_id, 'url_name', $url_name, true);
        }
        $tags = wp_get_object_terms($product_id, 'product_tag', array('fields' => 'names'));
        wp_set_object_terms($new_id, $tags, 'product_tag');
        //Inserting duplicated image info
        $image_values = $wpdb->get_results("SELECT `image`, `width`, `height`, `image_order`, `meta` FROM " . WPSC_TABLE_PRODUCT_IMAGES . " WHERE product_id='" . $product_id . "'", ARRAY_A);
        $new_image_value = array();
        if (count($image_values) > 0) {
            foreach ($image_values as $key => $image) {
                if ($image['image'] != '') {
                    if (is_numeric($image['width']) && is_numeric($image['height'])) {
                        $image['width'] = absint($image['width']);
                        $image['height'] = absint($image['height']);
                    } else {
                        $image['width'] = 'null';
                        $image['height'] = 'null';
                    }
                    $new_image_value[] = "('" . $new_id . "','" . $image['image'] . "'," . $image['width'] . "," . $image['height'] . ",'" . $image['image_order'] . "','" . $image['meta'] . "')";
                }
            }
            if (count($new_image_value) > 0) {
                $new_image_value = implode(",", $new_image_value);
                $sql = "INSERT INTO " . WPSC_TABLE_PRODUCT_IMAGES . " (`product_id`, `image`, `width`, `height`, `image_order`, `meta`) VALUES " . $new_image_value;
                $wpdb->query($sql);
            }
        }
        $duplicated = true;
    }
    $sendback = wp_get_referer();
    if (isset($duplicated)) {
        $sendback = add_query_arg('duplicated', (int) $duplicated, $sendback);
    }
    wp_redirect($sendback);
    exit;
}