/** * wpsc_admin_submit_product function * * @return nothing */ function wpsc_admin_submit_product() { check_admin_referer('edit-product', 'wpsc-edit-product'); $post_data = wpsc_sanitise_product_forms(); if (isset($post_data['title']) && $post_data['title'] != '' && isset($post_data['category'])) { $product_id = wpsc_insert_product($post_data, true); if ($product_id > 0) { $sendback = add_query_arg('product_id', $product_id); } $sendback = add_query_arg('message', 1, $sendback); //exit('<pre>'.print_r($sendback,true).'</pre>'); wp_redirect($sendback); } else { $_SESSION['product_error_messages'] = array(); if ($post_data['title'] == '') { $_SESSION['product_error_messages'][] = __('<strong>ERROR</strong>: Please enter a Product name.<br />'); } if (!isset($post_data['category'])) { $_SESSION['product_error_messages'][] = __('<strong>ERROR</strong>: Please enter a Product Category.<br />'); } $_SESSION['wpsc_failed_product_post_data'] = $post_data; // exit('<pre>'.print_r($_SESSION['product_error_messages'], true).'</pre>'); $sendback = add_query_arg('ErrMessage', 1); wp_redirect($sendback); } exit; }
private function import_data() { ini_set('auto_detect_line_endings', 1); $handle = @fopen($this->file, 'r'); if (!$handle) { $this->reset_state(); return; } $length = filesize($this->file); $column_map = array_flip($_POST['value_name']); extract($column_map, EXTR_SKIP); $record_count = 0; while ($row = @fgetcsv($handle, $length, ',')) { $product = array('post_title' => isset($row[$column_name]) ? $row[$column_name] : '', 'content' => isset($row[$column_description]) ? $row[$column_description] : '', 'price' => isset($row[$column_price]) ? str_replace('$', '', $row[$column_price]) : 0, 'weight' => isset($row[$column_weight]) ? $row[$column_weight] : '', 'weight_unit' => isset($row[$column_weight_unit]) ? $row[$column_weight_unit] : '', 'additional_description' => isset($row[$column_additional_description]) ? $row[$column_additional_description] : '', 'pnp' => null, 'international_pnp' => null, 'file' => null, 'image' => '0', 'quantity_limited' => isset($row[$column_quantity_limited]) ? $row[$column_quantity_limited] : '', 'quantity' => isset($row[$column_quantity]) ? $row[$column_quantity] : null, 'special' => null, 'special_price' => null, 'display_frontpage' => null, 'notax' => null, 'active' => null, 'donation' => null, 'no_shipping' => null, 'thumbnail_image' => null, 'thumbnail_state' => null, 'meta' => array('_wpsc_price' => isset($row[$column_price]) ? str_replace('$', '', $row[$column_price]) : 0, '_wpsc_special_price' => '', '_wpsc_sku' => isset($row[$column_sku]) ? $row[$column_sku] : '', '_wpsc_stock' => isset($row[$column_quantity]) ? $row[$column_quantity] : null, '_wpsc_limited_stock' => isset($row[$column_quantity_limited]) ? $row[$column_quantity_limited] : '', '_wpsc_product_metadata' => array('weight' => isset($row[$column_weight]) ? $row[$column_weight] : '', 'weight_unit' => isset($row[$column_weight_unit]) ? $row[$column_weight_unit] : ''))); $product = apply_filters('wpsc_product_import_row', $product, $row, $this); if (empty($product['post_title']) && apply_filters('wpsc_product_import_require_title', true, $product, $row, $this)) { continue; } $product = wpsc_sanitise_product_forms($product); // status needs to be set here because wpsc_sanitise_product_forms overwrites it :/ $product['post_status'] = $_POST['post_status']; $product_id = wpsc_insert_product($product); if ((int) $_POST['category'] > 0) { wp_set_object_terms($product_id, array((int) $_POST['category']), 'wpsc_product_category'); } $record_count += 1; } $this->reset_state(); $this->completed = true; add_settings_error('wpsc-settings', 'settings_updated', sprintf(__('CSV file successfully processed. %s record(s) imported.', 'wpsc'), $record_count), 'updated'); }
/** * This file handles the standard importing of products through a csv file upload. Access this page via WP-admin Settings>Import * @package WP e-Commerce */ function wpsc_options_import() { global $wpdb; ?> <form name='cart_options' enctype='multipart/form-data' id='cart_options' method='post' action='<?php echo 'admin.php?page=wpsc-settings&tab=import'; ?> ' class='wpsc_form_track'> <div class="wrap"> <?php _e('<p>You can import your products from a comma delimited text file.</p><p>An example of a csv import file would look like this: </p><p>Description, Additional Description, Product Name, Price, SKU, weight, weight unit, stock quantity, is limited quantity</p>', 'wpsc'); ?> <?php wp_nonce_field('update-options', 'wpsc-update-options'); ?> <input type='hidden' name='MAX_FILE_SIZE' value='5000000' /> <input type='file' name='csv_file' /> <input type='submit' value='Import' class='button-primary'> <?php if (isset($_FILES['csv_file']['name']) && $_FILES['csv_file']['name'] != '') { ini_set("auto_detect_line_endings", 1); $file = $_FILES['csv_file']; if (move_uploaded_file($file['tmp_name'], WPSC_FILE_DIR . $file['name'])) { $content = file_get_contents(WPSC_FILE_DIR . $file['name']); $handle = @fopen(WPSC_FILE_DIR . $file['name'], 'r'); while (($csv_data = @fgetcsv($handle, filesize($handle), ",")) !== false) { $fields = count($csv_data); for ($i = 0; $i < $fields; $i++) { if (!is_array($data1[$i])) { $data1[$i] = array(); } array_push($data1[$i], $csv_data[$i]); } } $_SESSION['cvs_data'] = $data1; $categories = get_terms('wpsc_product_category', 'hide_empty=0&parent=' . $category_id); ?> <p><?php _e('For each column, select the field it corresponds to in \'Belongs to\'. You can upload as many products as you like.', 'wpsc'); ?> </p> <div class='metabox-holder' style='width:90%'> <input type='hidden' name='csv_action' value='import'> <div style='width:100%;' class='postbox'> <h3 class='hndle'><?php _e('Product Status', 'wpsc'); ?> </h3> <div class='inside'> <table> <tr><td style='width:80%;'> <?php _e('Select if you would like to import your products in as Drafts or Publish them right away.', 'wpsc'); ?> <br /> </td><td> <select name='post_status'> <option value='publish'><?php _e('Publish', 'wpsc'); ?> </option> <option value='draft'><?php _e('Draft', 'wpsc'); ?> </option> </select> </td></tr> </table> </div> </div> <?php foreach ((array) $data1 as $key => $datum) { ?> <div style='width:100%;' class='postbox'> <h3 class='hndle'><?php printf(__('Column (%s)', 'wpsc'), $key + 1); ?> </h3> <div class='inside'> <table> <tr><td style='width:80%;'> <input type='hidden' name='column[]' value='<?php echo $key + 1; ?> '> <?php foreach ($datum as $column) { echo $column; break; } ?> <br /> </td><td> <select name='value_name[]'> <!-- /* These are the current fields that can be imported with products, to add additional fields add more <option> to this dorpdown list */ --> <option value='name'><?php _e('Product Name', 'wpsc'); ?> </option> <option value='description'><?php _e('Description', 'wpsc'); ?> </option> <option value='additional_description'><?php _e('Additional Description', 'wpsc'); ?> </option> <option value='price'><?php _e('Price', 'wpsc'); ?> </option> <option value='sku'><?php _e('SKU', 'wpsc'); ?> </option> <option value='weight'><?php _e('Weight', 'wpsc'); ?> </option> <option value='weight_unit'><?php _e('Weight Unit', 'wpsc'); ?> </option> <option value='quantity'><?php _e('Stock Quantity', 'wpsc'); ?> </option> <option value='quantity_limited'><?php _e('Stock Quantity Limit', 'wpsc'); ?> </option> </select> </td></tr> </table> </div> </div> <?php } ?> <label for='category'><?php _e('Please select a category you would like to place all products from this CSV into', 'wpsc'); ?> :</label> <select id='category' name='category'> <?php foreach ($categories as $category) { echo '<option value="' . $category->term_id . '">' . $category->name . '</option>'; } ?> </select> <input type='submit' value='Import' class='button-primary'> </div> <?php } else { echo "<br /><br />" . __('There was an error while uploading your csv file.', 'wpsc'); } } if (isset($_POST['csv_action']) && 'import' == $_POST['csv_action']) { global $wpdb; $cvs_data = $_SESSION['cvs_data']; $column_data = $_POST['column']; $value_data = $_POST['value_name']; $status = esc_attr($_POST['post_status']); $name = array(); foreach ($value_data as $key => $value) { $cvs_data2[$value] = $cvs_data[$key]; } $num = count($cvs_data2['name']); for ($i = 0; $i < $num; $i++) { $product_columns = array('post_title' => esc_attr($cvs_data2['name'][$i]), 'content' => esc_attr($cvs_data2['description'][$i]), 'additional_description' => esc_attr($cvs_data2['additional_description'][$i]), 'price' => esc_attr(str_replace('$', '', $cvs_data2['price'][$i])), 'weight' => esc_attr($cvs_data2['weight'][$i]), 'weight_unit' => esc_attr($cvs_data2['weight_unit'][$i]), 'pnp' => null, 'international_pnp' => null, 'file' => null, 'image' => '0', 'quantity_limited' => esc_attr($cvs_data2['quantity_limited'][$i]), 'quantity' => esc_attr($cvs_data2['quantity'][$i]), 'special' => null, 'special_price' => null, 'display_frontpage' => null, 'notax' => null, 'active' => null, 'donation' => null, 'no_shipping' => null, 'thumbnail_image' => null, 'thumbnail_state' => null, 'meta' => array('_wpsc_price' => esc_attr(str_replace('$', '', $cvs_data2['price'][$i])), '_wpsc_sku' => esc_attr($cvs_data2['sku'][$i]), '_wpsc_stock' => esc_attr($cvs_data2['quantity'][$i]), '_wpsc_limited_stock' => esc_attr($cvs_data2['quantity_limited'][$i]), '_wpsc_product_metadata' => array('weight' => esc_attr($cvs_data2['weight'][$i]), 'weight_unit' => esc_attr($cvs_data2['weight_unit'][$i])))); $product_columns = wpsc_sanitise_product_forms($product_columns); // status needs to be set here because wpsc_sanitise_product_forms overwrites it :/ $product_columns['post_status'] = $status; $product_id = wpsc_insert_product($product_columns); wp_set_object_terms($product_id, array((int) $_POST['category']), 'wpsc_product_category'); } echo "<br /><br />" . sprintf(__("Success, your <a href='%s'>products</a> have been upload.", "wpsc"), admin_url('edit.php?post_type=wpsc-product')); } ?> </div> </form> <?php }
function data_for_insert_update($post) { global $result, $wpdb; $_POST = $post; // Fix: PHP 5.4 $selected_object = json_decode(stripslashes($_POST['selected'])); $edited_object = json_decode(stripslashes($_POST['edited'])); $objectArray = array(); if (is_array($edited_object)) { foreach ($edited_object as $obj) { array_push($objectArray, $obj); } } if (is_array($selected_object)) { foreach ($selected_object as $obj) { if (!in_array($obj, $objectArray)) { array_push($objectArray, $obj); } } } $insertCnt = 1; $updateCnt = 1; $result['updated'] = 0; $result['inserted'] = 0; $result['productId'] = array(); $result['8B_Email'] = array(); $result['log_id'] = array(); if (is_array($objectArray)) { foreach ($objectArray as $obj) { foreach ($obj as $obj_key => $obj_value) { $obj->{$obj_key} = $wpdb->_real_escape($obj->{$obj_key}); } //escaping the input values if ($obj->id == '') { $result['inserted'] = 1; if ($obj->name == 'Enter Product Name') { $obj->name = 'New Product Added'; } $new_product_details['product_id'] = 0; $new_product_details['title'] = $obj->name; $new_product_details['productmeta_values']['sku'] = $obj->sku; $new_product_details['productmeta_values']['table_rate_price']['quantity'] = array(0 => ''); $new_product_details['productmeta_values']['table_rate_price']['table_price'] = array(0 => ''); $new_product_details['productmeta_values']['custom_tax'] = ''; $new_product_details['productmeta_values']['dimensions']['height'] = $obj->height; $new_product_details['productmeta_values']['dimensions']['height_unit'] = $obj->height_unit; $new_product_details['productmeta_values']['dimensions']['width'] = $obj->width; $new_product_details['productmeta_values']['dimensions']['width_unit'] = $obj->width_unit; $new_product_details['productmeta_values']['dimensions']['length'] = $obj->length; $new_product_details['productmeta_values']['dimensions']['length_unit'] = $obj->length_unit; $new_product_details['productmeta_values']['merchant_notes'] = ''; $new_product_details['productmeta_values']['engraved'] = 0; $new_product_details['productmeta_values']['can_have_uploaded_image'] = 0; $new_product_details['productmeta_values']['external_link'] = ''; $new_product_details['price'] = $obj->price; $new_product_details['special_price'] = $obj->sale_price; $new_product_details['quantity'] = $obj->quantity; $new_product_details['category'] = array(0 => $obj->category); $new_product_details['weight'] = $obj->weight; $new_product_details['weight_unit'] = $obj->weight_unit; $new_product_details['publish'] = $obj->publish; $new_product_details['no_shipping'] = $obj->disregard_shipping; $new_product_details['content'] = $obj->description; $new_product_details['pnp'] = $obj->pnp; $new_product_details['additional_description'] = $obj->additional_description; $new_product_details['international_pnp'] = $obj->international_pnp; $post_data = wpsc_sanitise_product_forms($new_product_details); if (isset($post_data['title']) && $post_data['title'] != '' && isset($post_data['category'])) { $product_id = wpsc_insert_product($post_data, true); } if ($product_id) { $result['result'] = true; } array_push($result['productId'], $product_id); if ($result['result']) { $result['insertCnt'] = $insertCnt++; } } else { if (in_array($obj, $edited_object)) { $result['updated'] = 1; $result = update($obj); if ($result['result']) { $result['updateCnt'] = $updateCnt++; } } } //to save customer details. //since $obj is an object array we cannot access email key by doing $obj->8B_Email $email = (array) $obj; if ($email['Old_Email_Id']) { $log_id = get_log_id($email['Old_Email_Id']); array_push($result['log_id'], $log_id); } } } return $result; }
function updateProduct(&$products) { wp_reset_query(); global $wpdb, $productUpdates, $variantUpdates; $productUpdates = 0; $variantUpdates = 0; set_time_limit(0); $r = 0; foreach ($products as $row) { $r++; if ($this->isGood($row)) { foreach ($row as $key => $val) { $key = mb_convert_encoding($key, 'UTF-8'); $row[$key] = mb_convert_encoding($val, 'UTF-8'); } //search products by style, don't waste time with name as it can be to hard to keep exact match $this->logger->info("Row #{$r}"); if ($this->isGood($row['style'])) { $product = query_posts(array('post_type' => 'wpsc-product', 'meta_key' => 'style', 'meta_value' => $row['style'])); } elseif ($this->isGood($row['name'])) { $product = query_posts(array('post_type' => 'wpsc-product', 'post_title' => $row['name'])); } else { $product = false; } //Meta the meta info ready $row['meta']['_wpsc_price'] = abs((double) str_replace(',', '', $this->isGood($row['price']) ? $row['price'] : '')); $row['meta']['_wpsc_product_metadata']['display_weight_as'] = 'pound'; $row['meta']['_wpsc_product_metadata']['weight_unit'] = 'pound'; //This sets product dimensions for shipping calculators if ($this->isGood($row['width']) && is_numeric($row['width'])) { $row['meta']['_wpsc_product_metadata']['dimensions']['width'] = $row['width']; unset($row['width']); } if ($this->isGood($row['length']) && is_numeric($row['length'])) { $row['meta']['_wpsc_product_metadata']['dimensions']['length'] = $row['length']; unset($row['length']); } if ($this->isGood($row['height']) && is_numeric($row['height'])) { $row['meta']['_wpsc_product_metadata']['dimensions']['height'] = $row['height']; unset($row['height']); } //This is for setting the weight if ($this->isGood($row['weight']) && is_numeric($row['weight'])) { $row['meta']['_wpsc_product_metadata']['weight'] = $row['weight']; unset($row['weight']); } elseif ($this->isGood($row['weight']) && ($weight = $this->strstr($row['weight'], 'kilogram', true))) { $row['meta']['_wpsc_product_metadata']['display_weight_as'] = 'kilogram'; $row['meta']['_wpsc_product_metadata']['weight_unit'] = 'kilogram'; $row['meta']['_wpsc_product_metadata']['weight'] = $weight; unset($row['weight']); } elseif ($this->isGood($row['weight']) && ($weight = $this->strstr($row['weight'], 'gram', true))) { $row['meta']['_wpsc_product_metadata']['display_weight_as'] = 'gram'; $row['meta']['_wpsc_product_metadata']['weight_unit'] = 'gram'; $row['meta']['_wpsc_product_metadata']['weight'] = $weight; unset($row['weight']); } elseif ($this->isGood($row['weight']) && ($weight = $this->strstr($row['weight'], 'once', true))) { $row['meta']['_wpsc_product_metadata']['display_weight_as'] = 'once'; $row['meta']['_wpsc_product_metadata']['weight_unit'] = 'once'; $row['meta']['_wpsc_product_metadata']['weight'] = $weight; unset($row['weight']); } elseif ($this->isGood($row['weight']) && ($weight = $this->strstr($row['weight'], 'pound', true))) { $row['meta']['_wpsc_product_metadata']['display_weight_as'] = 'pound'; $row['meta']['_wpsc_product_metadata']['weight_unit'] = 'pond'; $row['meta']['_wpsc_product_metadata']['weight'] = $weight; unset($row['weight']); } else { $row['meta']['_wpsc_product_metadata']['weight'] = 1; } if ($this->isGood($row['quantity']) && is_numeric($row['quantity'])) { $row['meta']['_wpsc_stock'] = $row['quantity']; $row['meta']['quantity_limited'] = 1; $row['meta']['_wpsc_limited_stock'] = 1; $row['quantity_limited'] = 1; $row['_wpsc_limited_stock'] = 1; unset($row['quantity']); } else { $row['meta']['_wpsc_stock'] = ''; } //Include a product sku if ($this->isGood($row['sku'])) { $row['meta']['_wpsc_sku'] = $row['sku']; unset($row['sku']); } //Setup The custom meta info foreach ($row as $k => $v) { if (is_string($k) && $this->isGood($v) && preg_match('/^meta_/', $k) > 0) { $row['new_custom_meta']['name'][] = substr($k, 5); $row['new_custom_meta']['value'][] = $v; } } if ($this->isGood($row['active']) && $row['active'] == 1) { $row['post_status'] = 'publish'; } if ($this->isGood($product)) { $product_id = $product[0]->ID; //looks like either an update or variant adjustment $row['product_id'] = $product[0]->ID; $row['ID'] = $product[0]->ID; //if the product is no longer active, unpublish it. if ($this->isGood($row['active']) && $row['active'] == 0) { $row['post_status'] = 'draft'; } if ($this->isGood($row['image'])) { $images = explode(' | ', $row['image']); foreach ($images as $image) { $image_id = $this->image_handle_upload($image, $row['product_id']); } } if (isset($_SESSION['status_msg'])) { $_SESSION['status_msg'] .= __("Updating Product:", 'ecommerce_feeder') . $row['name'] . '. '; } else { $_SESSION['status_msg'] = __("Updating Product:", 'ecommerce_feeder') . $row['name'] . '. '; } $result = $this->updateVariant($row['product_id'], $row); $variantUpdates++; } else { //Add inital product $row['new_custom_meta']['name'][] = 'style'; $row['new_custom_meta']['value'][] = $row['style']; if (!isset($row['description']) || empty($row['description'])) { $row['description'] = ' '; } $product_id = wpsc_insert_product($row); $productUpdates++; if ($this->isGood($row['image'])) { $images = explode(' | ', $row['image']); foreach ($images as $image) { $image_id = $this->image_handle_upload($image, $product_id); } } if (isset($_SESSION['status_msg'])) { $_SESSION['status_msg'] .= __("Adding Product:", 'ecommerce_feeder') . $row['name'] . '. '; } else { $_SESSION['status_msg'] = __("Adding Product:", 'ecommerce_feeder') . $row['name'] . '. '; } $result = $this->updateVariant($product_id, $row); } //Products tags if ($this->isGood($row['tags'])) { //Makes the tags a santizied array product_tag $row['tags'] = explode('|', preg_replace('/\\s+\\|\\s+/', '|', $row['tags'])); wp_set_object_terms($product_id, $row['tags'], 'product_tag'); } //This sets the category, if category doesn't exists it makes it. if ($this->isGood($row['category'])) { $categories = explode('|', preg_replace('/\\s+\\|\\s+/', '|', $row['category'])); foreach ($categories as $cat) { $categoryPath = explode('->', $cat); if (count($categoryPath) > 1) { $pid = $this->getVariant($categoryPath[0], 'wpsc_product_category'); for ($i = 1; count($categoryPath) > $i; $i++) { $pid = $this->getVariant($categoryPath[$i], 'wpsc_product_category', $pid); } $row['category'] = (int) $pid; } else { $row['category'] = (int) $this->getVariant($categoryPath[0], 'wpsc_product_category'); } $this->logger->info("Adding {$row['category']} to productid {$product_id}"); wp_set_object_terms($product_id, $row['category'], 'wpsc_product_category', true); } } //Update the Meta foreach ($row['meta'] as $meta_key => $meta_value) { if ($meta_key == '_wpsc_sku') { $this->logger->debug("Trying to add a sku to {$product_id} of {$meta_value}"); } if ($meta_key == "_wpsc_product_metadata") { update_post_meta($product_id, $meta_key, $meta_value); } else { update_post_meta($product_id, $meta_key, $meta_value); } } } } }