コード例 #1
0
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;
}
コード例 #2
0
/**
	* wpsc_insert_product function
	*
	* @param unknown
	* @return unknown
*/
function wpsc_insert_product($post_data, $wpsc_error = false)
{
    global $wpdb, $user_ID;
    $adding = false;
    $update = false;
    $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] = '';
        }
        if ($post_data[$column] !== null) {
            $update_values[$column] = stripslashes($post_data[$column]);
        } else {
            if ($update != true && $default !== null) {
                $update_values[$column] = stripslashes($default);
            }
        }
    }
    $product_post_values = array('post_author' => $user_ID, 'post_content' => $post_data['description'], 'post_excerpt' => $post_data['additional_description'], 'post_title' => $post_data['name'], 'post_status' => $post_data['post_status'], 'post_type' => "wpsc-product", 'post_name' => sanitize_title($post_data['name']));
    $product_post_values["comment_status"] = "open";
    if (isset($sku) && $sku != '') {
        $product_post_array['guid'] = $sku;
    }
    $product_id = wp_insert_post($product_post_values);
    if (isset($post_data["sticky"])) {
        stick_post($product_id);
    } else {
        unstick_post($product_id);
    }
    if ($product_id == 0) {
        if ($wp_error) {
            return new WP_Error('db_insert_error', __('Could not insert product into the database', 'wpsc'), $wpdb->last_error);
        } else {
            return 0;
        }
    }
    $adding = true;
    // if we succeed, we can do further editing
    // and the meta
    wpsc_update_product_meta($product_id, $post_data['meta']);
    do_action('wpsc_edit_product', $product_id);
    return $product_id;
}
コード例 #3
0
ファイル: product-functions.php プロジェクト: hornet9/Morato
/**
	* wpsc_insert_product function 
	*
	* @param unknown 
	* @return unknown
*/
function wpsc_insert_product($post_data, $wpsc_error = false)
{
    global $wpdb, $user_ID;
    $adding = false;
    $update = false;
    if ((int) $post_data['post_ID'] > 0) {
        $product_id = absint($post_data['post_ID']);
        $update = true;
    } else {
        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] = '';
        }
        if ($post_data[$column] !== null) {
            $update_values[$column] = stripslashes($post_data[$column]);
        } else {
            if ($update != true && $default !== null) {
                $update_values[$column] = stripslashes($default);
            }
        }
    }
    $product_post_values = array('ID' => $product_id, 'post_author' => $user_ID, 'post_content' => $post_data['description'], 'post_excerpt' => $post_data['additional_description'], 'post_title' => $post_data['name'], 'post_status' => $post_data['post_status'], 'post_type' => "wpsc-product", 'post_name' => sanitize_title($post_data['name']));
    if ($post_data['meta']['_wpsc_product_metadata']['enable_comments'] == 0) {
        $product_post_values["comment_status"] = "closed";
    } else {
        $product_post_values["comment_status"] = "open";
    }
    if (isset($sku) && $sku != '') {
        $product_post_array['guid'] = $sku;
    }
    if ($update === true) {
        $where = array('id' => $product_id);
        $product_id = wp_update_post($product_post_values);
        if (isset($post_data["sticky"])) {
            stick_post($product_id);
        } else {
            unstick_post($product_id);
        }
        if ($product_id == 0) {
            if ($wpsc_error) {
                return new WP_Error('db_update_error', __('Could not update product in the database', 'wpsc'), $wpdb->last_error);
            } else {
                return false;
            }
        }
    } else {
        $product_post_values += array('post_date' => $product['date_added']);
        $product_id = wp_insert_post($product_post_values);
        if (isset($post_data["sticky"])) {
            stick_post($product_id);
        } else {
            unstick_post($product_id);
        }
        if ($product_id == 0) {
            if ($wp_error) {
                return new WP_Error('db_insert_error', __('Could not insert product into the database', 'wpsc'), $wpdb->last_error);
            } else {
                return 0;
            }
        }
        $adding = true;
    }
    // if we succeed, we can do further editing
    // 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 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 {
        if (!isset($post_data['select_product_file'])) {
            $post_data['select_product_file'] = null;
        }
        wpsc_item_reassign_file($product_id, $post_data['select_product_file']);
    }
    if (isset($post_data['files']['preview_file']['tmp_name']) && $post_data['files']['preview_file']['tmp_name'] != '') {
        wpsc_item_add_preview_file($product_id, $post_data['files']['preview_file']);
    }
    do_action('wpsc_edit_product', $product_id);
    wpsc_ping();
    return $product_id;
}