function wpsc_insert_product($post_data, $wpsc_error = false)
{
    global $wpdb;
    $adding = false;
    $update = false;
    if ((int) $post_data['product_id'] > 0) {
        $product_id = absint($post_data['product_id']);
        $update = true;
    }
    $product_columns = array('name' => '', 'description' => '', 'additional_description' => '', 'price' => null, 'weight' => null, 'weight_unit' => '', 'pnp' => null, 'international_pnp' => null, 'file' => null, 'image' => '0', 'quantity_limited' => '', 'quantity' => null, 'special' => null, 'special_price' => null, 'display_frontpage' => null, 'notax' => null, 'publish' => null, 'active' => null, 'donation' => null, 'no_shipping' => null, 'thumbnail_image' => null, 'thumbnail_state' => null);
    foreach ($product_columns as $column => $default) {
        if (isset($post_data[$column]) || $post_data[$column] !== null) {
            $update_values[$column] = stripslashes($post_data[$column]);
        } else {
            if ($update != true && $default !== null) {
                $update_values[$column] = stripslashes($default);
            }
        }
    }
    if ($update === true) {
        $where = array('id' => $product_id);
        if (false === $wpdb->update(WPSC_TABLE_PRODUCT_LIST, $update_values, $where)) {
            if ($wpsc_error) {
                return new WP_Error('db_update_error', __('Could not update product in the database'), $wpdb->last_error);
            } else {
                return false;
            }
        }
    } else {
        if (false === $wpdb->insert(WPSC_TABLE_PRODUCT_LIST, $update_values)) {
            if ($wp_error) {
                return new WP_Error('db_insert_error', __('Could not insert product into the database'), $wpdb->last_error);
            } else {
                return 0;
            }
        }
        $adding = true;
        $product_id = (int) $wpdb->insert_id;
    }
    /* Add tidy url name */
    if ($post_data['name'] != '') {
        $existing_name = get_product_meta($product_id, 'url_name');
        // strip slashes, trim whitespace, convert to lowercase
        $tidied_name = strtolower(trim(stripslashes($post_data['name'])));
        // convert " - " to "-", all other spaces to dashes, and remove all foward slashes.
        //$url_name = preg_replace(array("/(\s-\s)+/","/(\s)+/", "/(\/)+/"), array("-","-", ""), $tidied_name);
        $url_name = sanitize_title($tidied_name);
        // Select all similar names, using an escaped version of the URL name
        $similar_names = (array) $wpdb->get_col("SELECT `meta_value` FROM `" . WPSC_TABLE_PRODUCTMETA . "` WHERE `product_id` NOT IN('{$product_id}}') AND `meta_key` IN ('url_name') AND `meta_value` REGEXP '^(" . $wpdb->escape(preg_quote($url_name)) . "){1}[[:digit:]]*\$' ");
        // Check desired name is not taken
        if (array_search($url_name, $similar_names) !== false) {
            // If it is, try to add a number to the end, if that is taken, try the next highest number...
            $i = 0;
            do {
                $i++;
            } while (array_search($url_name . $i, $similar_names) !== false);
            // Concatenate the first number found that wasn't taken
            $url_name .= $i;
        }
        // If our URL name is the same as the existing name, do othing more.
        if ($existing_name != $url_name) {
            update_product_meta($product_id, 'url_name', $url_name);
        }
    }
    // if we succeed, we can do further editing
    // update the categories
    wpsc_update_category_associations($product_id, $post_data['category']);
    // and the tags
    wpsc_update_product_tags($product_id, $post_data['product_tags'], $post_data['wpsc_existing_tags']);
    // and the meta
    wpsc_update_product_meta($product_id, $post_data['meta']);
    // and the custom meta
    wpsc_update_custom_meta($product_id, $post_data);
    // and the images
    wpsc_update_product_images($product_id, $post_data);
    //and the alt currency
    foreach ((array) $post_data['newCurrency'] as $key => $value) {
        wpsc_update_alt_product_currency($product_id, $value, $post_data['newCurrPrice'][$key]);
    }
    if ($post_data['files']['file']['tmp_name'] != '') {
        wpsc_item_process_file($product_id, $post_data['files']['file']);
    } else {
        wpsc_item_reassign_file($product_id, $post_data['select_product_file']);
    }
    //exit('<pre>'.print_r($post_data, true).'</pre>');
    if ($post_data['files']['preview_file']['tmp_name'] != '') {
        wpsc_item_add_preview_file($product_id, $post_data['files']['preview_file']);
    }
    $variations_processor = new nzshpcrt_variations();
    if ($adding === true && $_POST['variations'] != null) {
        foreach ((array) $_POST['variations'] as $variation_id => $state) {
            $variation_id = (int) $variation_id;
            if ($state == 1) {
                $variation_values = $variations_processor->falsepost_variation_values($variation_id);
                $variations_processor->add_to_existing_product($product_id, $variation_values);
            }
        }
    }
    if ($post_data['edit_variation_values'] != null) {
        $variations_processor->edit_product_values($product_id, $post_data['edit_variation_values']);
    }
    if ($post_data['edit_add_variation_values'] != null) {
        $variations_processor->edit_add_product_values($product_id, $post_data['edit_add_variation_values']);
    }
    if ($post_data['variation_priceandstock'] != null) {
        $variations_processor->update_variation_values($product_id, $post_data['variation_priceandstock']);
    }
    do_action('wpsc_edit_product', $product_id);
    wpsc_ping();
    return $product_id;
}
function wpsc_add_product($product_values)
{
    global $wpdb;
    // takes an array, inserts it into the database as a product
    $success = false;
    $insertsql = "INSERT INTO `" . WPSC_TABLE_PRODUCT_LIST . "` SET";
    $insertsql .= "`name` = '" . $wpdb->escape($product_values['name']) . "',";
    $insertsql .= "`description`  = '" . $wpdb->escape($product_values['description']) . "',";
    $insertsql .= "`additional_description`  = '" . $wpdb->escape($product_values['additional_description']) . "',";
    $insertsql .= "`price` = '" . $wpdb->escape($product_values['price']) . "',";
    $insertsql .= "`quantity_limited` = '" . $wpdb->escape($product_values['quantity_limited']) . "',";
    $insertsql .= "`quantity` = '" . $wpdb->escape($product_values['quantity']) . "',";
    $insertsql .= "`special` = '" . $wpdb->escape($product_values['special']) . "',";
    $insertsql .= "`special_price` = '" . $wpdb->escape($product_values['special_price']) . "',";
    $insertsql .= "`weight` = '" . $wpdb->escape($product_values['weight']) . "',";
    $insertsql .= "`weight_unit` = '" . $wpdb->escape($product_values['weight_unit']) . "',";
    $insertsql .= "`no_shipping` = '" . $wpdb->escape($product_values['no_shipping']) . "',";
    $insertsql .= "`pnp` = '" . $wpdb->escape($product_values['pnp']) . "',";
    $insertsql .= "`international_pnp` = '" . $wpdb->escape($product_values['international_pnp']) . "',";
    $insertsql .= "`donation` = '" . $wpdb->escape($product_values['donation']) . "',";
    $insertsql .= "`display_frontpage` = '" . $wpdb->escape($product_values['display_frontpage']) . "',";
    $insertsql .= "`notax` = '" . $wpdb->escape($product_values['notax']) . "',";
    $insertsql .= "`image` = '0',";
    $insertsql .= "`file` = '0',";
    $insertsql .= "`thumbnail_state` = '0' ;";
    //Insert the data
    if ($wpdb->query($insertsql)) {
        // if we succeeded, we have a product id, we wants it for the next stuff
        $product_id = $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` LIMIT 1");
        // add the tags
        if (function_exists('wp_insert_term')) {
            product_tag_init();
            $tags = $product_values['product_tag'];
            if ($tags != "") {
                $tags = explode(',', $tags);
                foreach ($tags as $tag) {
                    $tt = wp_insert_term((string) $tag, 'product_tag');
                }
                $return = wp_set_object_terms($product_id, $tags, 'product_tag');
            }
        }
        $image = wpsc_item_process_image($product_id, $product_values['image_path'], basename($product_values['image_path']), $product_values['width'], $product_values['height'], $product_values['image_resize']);
        if ($image != null) {
            $wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_LIST . "` SET `image` = '" . $wpdb->escape($image) . "' WHERE `id`='" . $product_id . "' LIMIT 1");
        }
        // add the product meta values
        if ($product_values['productmeta_values'] != null) {
            foreach ((array) $product_values['productmeta_values'] as $key => $value) {
                if (get_product_meta($product_id, $key) != false) {
                    update_product_meta($product_id, $key, $value);
                } else {
                    add_product_meta($product_id, $key, $value);
                }
            }
        }
        // and the custom meta values
        if ($product_values['new_custom_meta'] != null) {
            foreach ((array) $product_values['new_custom_meta']['name'] as $key => $name) {
                $value = $product_values['new_custom_meta']['value'][(int) $key];
                if ($name != '' && $value != '') {
                    add_product_meta($product_id, $name, $value, false, true);
                }
            }
        }
        // Add the tidy url name
        $tidied_name = trim($product_values['name']);
        $tidied_name = strtolower($tidied_name);
        $url_name = sanitize_title($tidied_name);
        $similar_names = $wpdb->get_row("SELECT COUNT(*) AS `count`, MAX(REPLACE(`meta_value`, '" . $wpdb->escape($url_name) . "', '')) AS `max_number` FROM `" . WPSC_TABLE_PRODUCTMETA . "` WHERE `meta_key` IN ('url_name') AND `meta_value` REGEXP '^(" . $wpdb->escape($url_name) . "){1}(\\d)*\$' ", ARRAY_A);
        $extension_number = '';
        if ($similar_names['count'] > 0) {
            $extension_number = (int) $similar_names['max_number'] + 1;
        }
        $url_name .= $extension_number;
        add_product_meta($product_id, 'url_name', $url_name, true);
        // Add the varations and associated values
        $variations_procesor = new nzshpcrt_variations();
        if ($product_values['variation_values'] != null) {
            $variations_procesor->add_to_existing_product($product_id, $product_values['variation_values']);
        }
        if ($product_values['variation_priceandstock'] != null) {
            $variations_procesor->update_variation_values($product_id, $product_values['variation_priceandstock']);
        }
        // Add the selelcted categories
        $item_list = '';
        if (count($product_values['category']) > 0) {
            foreach ($product_values['category'] as $category_id) {
                $category_id = (int) $category_id;
                $check_existing = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` WHERE `product_id` = " . $product_id . " AND `category_id` = '{$category_id}' LIMIT 1");
                if ($check_existing == null) {
                    $wpdb->query("INSERT INTO `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` ( `product_id` , `category_id` ) VALUES ( '" . $product_id . "', '" . $category_id . "');");
                }
            }
        }
        $success = true;
    }
    return $success;
}
예제 #3
0
        $wpdb->query("UPDATE `" . $wpdb->prefix . "product_list` SET `thumbnail_state` = '" . $image_resize . "' WHERE `id`='" . $_POST['prodid'] . "' LIMIT 1");
        if ($_POST['deleteimage'] == 1) {
            $wpdb->query("UPDATE `" . $wpdb->prefix . "product_list` SET `image` = ''  WHERE `id`='" . $_POST['prodid'] . "' LIMIT 1");
        }
        $variations_procesor = new nzshpcrt_variations();
        if ($_POST['variation_values'] != null) {
            //$variations_procesor->add_to_existing_product($_POST['prodid'],$_POST['variation_values']);
        }
        if ($_POST['edit_variation_values'] != null) {
            $variations_procesor->edit_product_values($_POST['prodid'], $_POST['edit_variation_values']);
        }
        if ($_POST['edit_add_variation_values'] != null) {
            $variations_procesor->edit_add_product_values($_POST['prodid'], $_POST['edit_add_variation_values']);
        }
        if ($_POST['variation_priceandstock'] != null) {
            $variations_procesor->update_variation_values($_POST['prodid'], $_POST['variation_priceandstock']);
        }
        // send the pings out.
        wpsc_ping();
        echo "<div class='updated'><p align='center'>" . TXT_WPSC_PRODUCTHASBEENEDITED . "</p></div>";
    }
}
if (is_numeric($_GET['deleteid'])) {
    $wpdb->query("DELETE FROM `" . $wpdb->prefix . "wpsc_productmeta` WHERE `product_id` = '" . $_GET['deleteid'] . "' AND `meta_key` IN ('url_name')");
    $wpdb->query("UPDATE `" . $wpdb->prefix . "product_list` SET  `active` = '0' WHERE `id`='" . $_GET['deleteid'] . "' LIMIT 1");
}
/*
 * Sort out the searching of the products
 */
if ($_GET['search_products']) {
    $search_string_title = "%" . $wpdb->escape(stripslashes($_GET['search_products'])) . "%";