本文整理汇总了PHP中wc_update_product_stock_status函数的典型用法代码示例。如果您正苦于以下问题:PHP wc_update_product_stock_status函数的具体用法?PHP wc_update_product_stock_status怎么用?PHP wc_update_product_stock_status使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wc_update_product_stock_status函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: wc_bulk_stock_after_process_qty_action
function wc_bulk_stock_after_process_qty_action($id)
{
global $sitepress;
$new_quantity = get_post_meta($id, '_stock', true);
if (is_numeric($new_quantity)) {
$new_stock_status = $new_quantity > 0 ? "instock" : "outofstock";
wc_update_product_stock_status($id, $new_stock_status);
$trid = $sitepress->get_element_trid($id, 'post_product');
if (is_numeric($trid)) {
$translations = $sitepress->get_element_translations($trid, 'post_product');
if (is_array($translations)) {
foreach ($translations as $translation) {
if (!isset($translation->element_id) || $translation->element_id == $id) {
continue;
}
update_post_meta($translation->element_id, '_stock', $new_quantity);
wc_update_product_stock_status($translation->element_id, $new_stock_status);
}
}
}
}
}
示例2: sync_stock_status
/**
* Sync variable product stock status with children.
* @param int $product_id
*/
public static function sync_stock_status($product_id)
{
$children = get_posts(array('post_parent' => $product_id, 'posts_per_page' => -1, 'post_type' => 'product_variation', 'fields' => 'ids', 'post_status' => 'publish'));
$stock_status = 'outofstock';
foreach ($children as $child_id) {
$child_stock_status = get_post_meta($child_id, '_stock_status', true);
$child_stock_status = $child_stock_status ? $child_stock_status : 'instock';
if ('instock' === $child_stock_status) {
$stock_status = 'instock';
break;
}
}
wc_update_product_stock_status($product_id, $stock_status);
}
示例3: bulk_edit_save
/**
* Bulk edit
* @param integer $post_id
* @param WC_Product $product
*/
public function bulk_edit_save($post_id, $product)
{
$old_regular_price = $product->regular_price;
$old_sale_price = $product->sale_price;
// Save fields
if (!empty($_REQUEST['change_weight']) && isset($_REQUEST['_weight'])) {
update_post_meta($post_id, '_weight', wc_clean(stripslashes($_REQUEST['_weight'])));
}
if (!empty($_REQUEST['change_dimensions'])) {
if (isset($_REQUEST['_length'])) {
update_post_meta($post_id, '_length', wc_clean(stripslashes($_REQUEST['_length'])));
}
if (isset($_REQUEST['_width'])) {
update_post_meta($post_id, '_width', wc_clean(stripslashes($_REQUEST['_width'])));
}
if (isset($_REQUEST['_height'])) {
update_post_meta($post_id, '_height', wc_clean(stripslashes($_REQUEST['_height'])));
}
}
if (!empty($_REQUEST['_tax_status'])) {
update_post_meta($post_id, '_tax_status', wc_clean($_REQUEST['_tax_status']));
}
if (!empty($_REQUEST['_tax_class'])) {
$tax_class = wc_clean($_REQUEST['_tax_class']);
if ('standard' == $tax_class) {
$tax_class = '';
}
update_post_meta($post_id, '_tax_class', $tax_class);
}
if (!empty($_REQUEST['_stock_status'])) {
$stock_status = wc_clean($_REQUEST['_stock_status']);
if ($product->is_type('variable')) {
foreach ($product->get_children() as $child_id) {
if ('yes' !== get_post_meta($child_id, '_manage_stock', true)) {
wc_update_product_stock_status($child_id, $stock_status);
}
}
WC_Product_Variable::sync_stock_status($post_id);
} else {
wc_update_product_stock_status($post_id, $stock_status);
}
}
if (!empty($_REQUEST['_shipping_class'])) {
$shipping_class = '_no_shipping_class' == $_REQUEST['_shipping_class'] ? '' : wc_clean($_REQUEST['_shipping_class']);
wp_set_object_terms($post_id, $shipping_class, 'product_shipping_class');
}
if (!empty($_REQUEST['_visibility'])) {
if (update_post_meta($post_id, '_visibility', wc_clean($_REQUEST['_visibility']))) {
do_action('woocommerce_product_set_visibility', $post_id, wc_clean($_REQUEST['_visibility']));
}
}
if (!empty($_REQUEST['_featured'])) {
if (update_post_meta($post_id, '_featured', stripslashes($_REQUEST['_featured']))) {
delete_transient('wc_featured_products');
}
}
// Sold Individually
if (!empty($_REQUEST['_sold_individually'])) {
if ($_REQUEST['_sold_individually'] == 'yes') {
update_post_meta($post_id, '_sold_individually', 'yes');
} else {
update_post_meta($post_id, '_sold_individually', '');
}
}
// Handle price - remove dates and set to lowest
if ($product->is_type('simple') || $product->is_type('external')) {
$price_changed = false;
if (!empty($_REQUEST['change_regular_price'])) {
$change_regular_price = absint($_REQUEST['change_regular_price']);
$regular_price = esc_attr(stripslashes($_REQUEST['_regular_price']));
switch ($change_regular_price) {
case 1:
$new_price = $regular_price;
break;
case 2:
if (strstr($regular_price, '%')) {
$percent = str_replace('%', '', $regular_price) / 100;
$new_price = $old_regular_price + round($old_regular_price * $percent, wc_get_price_decimals());
} else {
$new_price = $old_regular_price + $regular_price;
}
break;
case 3:
if (strstr($regular_price, '%')) {
$percent = str_replace('%', '', $regular_price) / 100;
$new_price = max(0, $old_regular_price - round($old_regular_price * $percent, wc_get_price_decimals()));
} else {
$new_price = max(0, $old_regular_price - $regular_price);
}
break;
default:
break;
}
if (isset($new_price) && $new_price != $old_regular_price) {
$price_changed = true;
//.........这里部分代码省略.........
示例4: pre_post_update
/**
* Some functions, like the term recount, require the visibility to be set prior. Lets save that here.
*
* @param int $post_id
*/
public static function pre_post_update($post_id)
{
if (isset($_POST['_visibility'])) {
if (update_post_meta($post_id, '_visibility', wc_clean($_POST['_visibility']))) {
do_action('woocommerce_product_set_visibility', $post_id, wc_clean($_POST['_visibility']));
}
}
if (isset($_POST['_stock_status'])) {
wc_update_product_stock_status($post_id, wc_clean($_POST['_stock_status']));
}
}
示例5: pre_post_update
/**
* Some functions, like the term recount, require the visibility to be set prior. Lets save that here.
*
* @param int $post_id
*/
public static function pre_post_update($post_id)
{
if ('product' === get_post_type($post_id)) {
$product_type = empty($_POST['product-type']) ? 'simple' : sanitize_title(stripslashes($_POST['product-type']));
if (isset($_POST['_visibility'])) {
if (update_post_meta($post_id, '_visibility', wc_clean($_POST['_visibility']))) {
do_action('woocommerce_product_set_visibility', $post_id, wc_clean($_POST['_visibility']));
}
}
if (isset($_POST['_stock_status']) && 'variable' !== $product_type) {
wc_update_product_stock_status($post_id, wc_clean($_POST['_stock_status']));
}
}
}
示例6: save_variations_data
//.........这里部分代码省略.........
}
update_post_meta($variation_id, '_thumbnail_id', $attachment_id);
}
} else {
delete_post_meta($variation_id, '_thumbnail_id');
}
}
// Virtual variation.
if (isset($variation['virtual'])) {
$is_virtual = true === $variation['virtual'] ? 'yes' : 'no';
update_post_meta($variation_id, '_virtual', $is_virtual);
}
// Downloadable variation.
if (isset($variation['downloadable'])) {
$is_downloadable = true === $variation['downloadable'] ? 'yes' : 'no';
update_post_meta($variation_id, '_downloadable', $is_downloadable);
} else {
$is_downloadable = get_post_meta($variation_id, '_downloadable', true);
}
// Shipping data.
$this->save_product_shipping_data($variation_id, $variation);
// Stock handling.
if (isset($variation['manage_stock'])) {
$manage_stock = true === $variation['manage_stock'] ? 'yes' : 'no';
} else {
$manage_stock = get_post_meta($variation_id, '_manage_stock', true);
}
update_post_meta($variation_id, '_manage_stock', '' === $manage_stock ? 'no' : $manage_stock);
if (isset($variation['in_stock'])) {
$stock_status = true === $variation['in_stock'] ? 'instock' : 'outofstock';
} else {
$stock_status = get_post_meta($variation_id, '_stock_status', true);
}
wc_update_product_stock_status($variation_id, '' === $stock_status ? 'instock' : $stock_status);
if ('yes' === $manage_stock) {
$backorders = get_post_meta($variation_id, '_backorders', true);
if (isset($variation['backorders'])) {
$backorders = $variation['backorders'];
}
update_post_meta($variation_id, '_backorders', '' === $backorders ? 'no' : $backorders);
if (isset($variation['stock_quantity'])) {
wc_update_product_stock($variation_id, wc_stock_amount($variation['stock_quantity']));
} elseif (isset($request['inventory_delta'])) {
$stock_quantity = wc_stock_amount(get_post_meta($variation_id, '_stock', true));
$stock_quantity += wc_stock_amount($request['inventory_delta']);
wc_update_product_stock($variation_id, wc_stock_amount($stock_quantity));
}
} else {
delete_post_meta($variation_id, '_backorders');
delete_post_meta($variation_id, '_stock');
}
// Regular Price.
if (isset($variation['regular_price'])) {
$regular_price = '' === $variation['regular_price'] ? '' : $variation['regular_price'];
} else {
$regular_price = get_post_meta($variation_id, '_regular_price', true);
}
// Sale Price.
if (isset($variation['sale_price'])) {
$sale_price = '' === $variation['sale_price'] ? '' : $variation['sale_price'];
} else {
$sale_price = get_post_meta($variation_id, '_sale_price', true);
}
if (isset($variation['date_on_sale_from'])) {
$date_from = $variation['date_on_sale_from'];
} else {
示例7: wc1c_replace_offer_post_meta
function wc1c_replace_offer_post_meta($is_full, $post_id, $offer, $attributes = array())
{
$price = isset($offer['Цена']['ЦенаЗаЕдиницу']) ? wc1c_parse_decimal($offer['Цена']['ЦенаЗаЕдиницу']) : null;
if (!is_null($price)) {
$coefficient = isset($offer['Цена']['Коэффициент']) ? wc1c_parse_decimal($offer['Цена']['Коэффициент']) : null;
if (!is_null($coefficient)) {
$price *= $coefficient;
}
}
$post_meta = array();
if (!is_null($price)) {
$post_meta['_regular_price'] = $price;
$post_meta['_manage_stock'] = 'yes';
}
if ($attributes) {
foreach ($attributes as $attribute_name => $attribute_value) {
$meta_key = 'attribute_' . sanitize_title($attribute_name);
$post_meta[$meta_key] = $attribute_value;
}
$current_post_meta = get_post_meta($post_id);
foreach ($current_post_meta as $meta_key => $meta_value) {
$current_post_meta[$meta_key] = $meta_value[0];
}
foreach ($current_post_meta as $meta_key => $meta_value) {
if (strpos($meta_key, 'attribute_') !== 0 || array_key_exists($meta_key, $post_meta)) {
continue;
}
delete_post_meta($post_id, $meta_key);
}
}
if (!is_null($price)) {
$sale_price = @$current_post_meta['_sale_price'];
$sale_price_from = @$current_post_meta['_sale_price_dates_from'];
$sale_price_to = @$current_post_meta['_sale_price_dates_to'];
if (empty($current_post_meta['_sale_price'])) {
$post_meta['_price'] = $price;
} else {
if (empty($sale_price_from) && empty($sale_price_to)) {
$post_meta['_price'] = $current_post_meta['_sale_price'];
} else {
$now = strtotime('now', current_time('timestamp'));
if (!empty($sale_price_from) && strtotime($sale_price_from) < $now) {
$post_meta['_price'] = $current_post_meta['_sale_price'];
}
if (!empty($sale_price_to) && strtotime($sale_price_to) < $now) {
$post_meta['_price'] = $price;
$post_meta['_sale_price_dates_from'] = '';
$post_meta['_sale_price_dates_to'] = '';
}
}
}
}
foreach ($post_meta as $meta_key => $meta_value) {
$current_meta_value = @$current_post_meta[$meta_key];
if ($meta_value !== '' && $current_meta_value == $meta_value) {
continue;
}
if ($meta_value === '' && $current_meta_value === $meta_value) {
continue;
}
update_post_meta($post_id, $meta_key, $meta_value);
}
$quantity = isset($offer['Количество']) ? $offer['Количество'] : @$offer['КоличествоНаСкладе'];
if (!is_null($quantity)) {
$quantity = wc1c_parse_decimal($quantity);
wc_update_product_stock($post_id, $quantity);
$stock_status = $quantity > 0 ? 'instock' : 'outofstock';
@wc_update_product_stock_status($post_id, $stock_status);
}
do_action('wc1c_post_offer_meta', $post_id, $offer, $is_full);
}
示例8: column_save
//.........这里部分代码省略.........
if (!$product->is_virtual()) {
update_post_meta($post->ID, '_weight', $value === '' ? '' : wc_format_decimal($value));
}
break;
case 'column-wc-dimensions':
if (is_array($value) && isset($value['length']) && isset($value['width']) && isset($value['height'])) {
$product = get_product($post->ID);
if (!$product->is_virtual()) {
update_post_meta($post->ID, '_length', $value === '' ? '' : wc_format_decimal($value['length']));
update_post_meta($post->ID, '_width', $value === '' ? '' : wc_format_decimal($value['width']));
update_post_meta($post->ID, '_height', $value === '' ? '' : wc_format_decimal($value['height']));
}
}
break;
case 'sku':
$product = get_product($post->ID);
$current_sku = get_post_meta($post->ID, '_sku', true);
$new_sku = wc_clean($value);
if (empty($new_sku)) {
$new_sku = '';
}
if ($new_sku != $current_sku) {
$existing_id = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\t\tSELECT {$wpdb->posts}.ID\n\t\t\t\t\t FROM {$wpdb->posts}\n\t\t\t\t\t LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)\n\t\t\t\t\t WHERE {$wpdb->posts}.post_type = 'product'\n\t\t\t\t\t AND {$wpdb->posts}.post_status = 'publish'\n\t\t\t\t\t AND {$wpdb->postmeta}.meta_key = '_sku' AND {$wpdb->postmeta}.meta_value = %s\n\t\t\t\t\t", $new_sku));
if ($existing_id) {
return new WP_Error('cacie_error_sku_exists', __('The SKU must be unique.', 'cpac'));
}
update_post_meta($post->ID, '_sku', $new_sku);
}
break;
case 'is_in_stock':
if (get_option('woocommerce_manage_stock') == 'yes') {
if ($value['manage_stock'] == 'yes') {
update_post_meta($post->ID, '_manage_stock', 'yes');
wc_update_product_stock_status($post->ID, wc_clean($value['stock_status']));
wc_update_product_stock($post->ID, intval($value['stock']));
} else {
// Don't manage stock
update_post_meta($post->ID, '_manage_stock', 'no');
update_post_meta($post->ID, '_stock', '');
wc_update_product_stock_status($post->ID, wc_clean($value['stock_status']));
}
} else {
wc_update_product_stock_status($post->ID, wc_clean($value['stock_status']));
}
break;
case 'column-wc-stock-status':
wc_update_product_stock_status($post->ID, wc_clean($value));
break;
case 'column-wc-free_shipping':
update_post_meta($id, 'free_shipping', $value == 'yes' ? 'yes' : 'no');
break;
case 'column-wc-shipping_class':
$this->set_post_terms($id, $value, 'product_shipping_class');
break;
case 'column-wc-apply_before_tax':
update_post_meta($id, 'apply_before_tax', $value == 'yes' ? 'yes' : 'no');
break;
case 'column-wc-backorders_allowed':
if (in_array($value, array('no', 'yes', 'notify'))) {
update_post_meta($post->ID, '_backorders', $value);
}
break;
case 'column-wc-upsells':
$upsell_ids = array();
if (is_array($value)) {
foreach ($value as $upsell_id) {
示例9: save_product_meta
//.........这里部分代码省略.........
// Stock status
if (isset($data['in_stock'])) {
$stock_status = true === $data['in_stock'] ? 'instock' : 'outofstock';
} else {
$stock_status = get_post_meta($id, '_stock_status', true);
if ('' === $stock_status) {
$stock_status = 'instock';
}
}
// Stock Data
if ('yes' == get_option('woocommerce_manage_stock')) {
// Manage stock
if (isset($data['managing_stock'])) {
$managing_stock = true === $data['managing_stock'] ? 'yes' : 'no';
update_post_meta($id, '_manage_stock', $managing_stock);
} else {
$managing_stock = get_post_meta($id, '_manage_stock', true);
}
// Backorders
if (isset($data['backorders'])) {
if ('notify' == $data['backorders']) {
$backorders = 'notify';
} else {
$backorders = true === $data['backorders'] ? 'yes' : 'no';
}
update_post_meta($id, '_backorders', $backorders);
} else {
$backorders = get_post_meta($id, '_backorders', true);
}
if ('grouped' == $product_type) {
update_post_meta($id, '_manage_stock', 'no');
update_post_meta($id, '_backorders', 'no');
update_post_meta($id, '_stock', '');
wc_update_product_stock_status($id, $stock_status);
} elseif ('external' == $product_type) {
update_post_meta($id, '_manage_stock', 'no');
update_post_meta($id, '_backorders', 'no');
update_post_meta($id, '_stock', '');
wc_update_product_stock_status($id, 'instock');
} elseif ('yes' == $managing_stock) {
update_post_meta($id, '_backorders', $backorders);
wc_update_product_stock_status($id, $stock_status);
// Stock quantity
if (isset($data['stock_quantity'])) {
wc_update_product_stock($id, intval($data['stock_quantity']));
}
} else {
// Don't manage stock
update_post_meta($id, '_manage_stock', 'no');
update_post_meta($id, '_backorders', $backorders);
update_post_meta($id, '_stock', '');
wc_update_product_stock_status($id, $stock_status);
}
} else {
wc_update_product_stock_status($id, $stock_status);
}
// Upsells
if (isset($data['upsell_ids'])) {
$upsells = array();
$ids = $data['upsell_ids'];
if (!empty($ids)) {
foreach ($ids as $id) {
if ($id && $id > 0) {
$upsells[] = $id;
}
}
示例10: save
//.........这里部分代码省略.........
}
if ($product_type == 'grouped') {
$clear_parent_ids[] = $post_id;
}
if ($_POST['previous_parent_id'] > 0) {
$clear_parent_ids[] = absint($_POST['previous_parent_id']);
}
if ($clear_parent_ids) {
foreach ($clear_parent_ids as $clear_id) {
$children_by_price = get_posts(array('post_parent' => $clear_id, 'orderby' => 'meta_value_num', 'order' => 'asc', 'meta_key' => '_price', 'posts_per_page' => 1, 'post_type' => 'product', 'fields' => 'ids'));
if ($children_by_price) {
foreach ($children_by_price as $child) {
$child_price = get_post_meta($child, '_price', true);
update_post_meta($clear_id, '_price', $child_price);
}
}
// Clear cache/transients
wc_delete_product_transients($clear_id);
}
}
}
// Sold Individually
if (!empty($_POST['_sold_individually'])) {
update_post_meta($post_id, '_sold_individually', 'yes');
} else {
update_post_meta($post_id, '_sold_individually', '');
}
// Stock Data
if (get_option('woocommerce_manage_stock') == 'yes') {
if ($product_type == 'grouped') {
update_post_meta($post_id, '_manage_stock', 'no');
update_post_meta($post_id, '_backorders', 'no');
update_post_meta($post_id, '_stock', '');
wc_update_product_stock_status($post_id, wc_clean($_POST['_stock_status']));
} elseif ($product_type == 'external') {
update_post_meta($post_id, '_manage_stock', 'no');
update_post_meta($post_id, '_backorders', 'no');
update_post_meta($post_id, '_stock', '');
wc_update_product_stock_status($post_id, 'instock');
} elseif (!empty($_POST['_manage_stock'])) {
update_post_meta($post_id, '_manage_stock', 'yes');
update_post_meta($post_id, '_backorders', wc_clean($_POST['_backorders']));
wc_update_product_stock_status($post_id, wc_clean($_POST['_stock_status']));
wc_update_product_stock($post_id, intval($_POST['_stock']));
} else {
// Don't manage stock
update_post_meta($post_id, '_manage_stock', 'no');
update_post_meta($post_id, '_backorders', wc_clean($_POST['_backorders']));
update_post_meta($post_id, '_stock', '');
wc_update_product_stock_status($post_id, wc_clean($_POST['_stock_status']));
}
} else {
wc_update_product_stock_status($post_id, wc_clean($_POST['_stock_status']));
}
// Upsells
if (isset($_POST['upsell_ids'])) {
$upsells = array();
$ids = $_POST['upsell_ids'];
foreach ($ids as $id) {
if ($id && $id > 0) {
$upsells[] = $id;
}
}
update_post_meta($post_id, '_upsell_ids', $upsells);
} else {
delete_post_meta($post_id, '_upsell_ids');
示例11: insert_product_meta
/**
*
* Insert post to database
*
* @param $post_id
* @param $data
*/
function insert_product_meta($post_id, $data)
{
add_post_meta($post_id, 'total_sales', '0', true);
wp_set_object_terms($post_id, 'simple', 'product_type', false);
wc_update_product_stock_status($post_id, 'instock');
update_post_meta($post_id, '_sale_price_dates_from', '');
update_post_meta($post_id, '_sale_price_dates_to', '');
update_post_meta($post_id, '_downloadable', 'no');
update_post_meta($post_id, '_virtual', 'no');
update_post_meta($post_id, '_sale_price', '');
if (isset($data['_regular_price']) && $data['_regular_price'] > 0) {
update_post_meta($post_id, '_price', $data['_regular_price']);
}
update_post_meta($post_id, '_tax_status', '');
update_post_meta($post_id, '_tax_class', '');
update_post_meta($post_id, '_purchase_note', '');
wp_set_object_terms($post_id, '', 'product_shipping_class');
update_post_meta($post_id, '_sku', '');
update_post_meta($post_id, '_sold_individually', '');
do_action('woocommerce_process_product_meta_' . 'simple', $post_id);
wc_delete_product_transients($post_id);
}
示例12: dokan_save_variations
function dokan_save_variations($post_id)
{
global $woocommerce, $wpdb;
$attributes = (array) maybe_unserialize(get_post_meta($post_id, '_product_attributes', true));
if (isset($_POST['variable_sku'])) {
$variable_post_id = $_POST['variable_post_id'];
$variable_sku = $_POST['variable_sku'];
$variable_regular_price = $_POST['variable_regular_price'];
$variable_sale_price = $_POST['variable_sale_price'];
$upload_image_id = $_POST['upload_image_id'];
$variable_download_limit = $_POST['variable_download_limit'];
$variable_download_expiry = $_POST['variable_download_expiry'];
$variable_shipping_class = $_POST['variable_shipping_class'];
$variable_tax_class = isset($_POST['variable_tax_class']) ? $_POST['variable_tax_class'] : array();
$variable_menu_order = $_POST['variation_menu_order'];
$variable_sale_price_dates_from = $_POST['variable_sale_price_dates_from'];
$variable_sale_price_dates_to = $_POST['variable_sale_price_dates_to'];
$variable_weight = isset($_POST['variable_weight']) ? $_POST['variable_weight'] : array();
$variable_length = isset($_POST['variable_length']) ? $_POST['variable_length'] : array();
$variable_width = isset($_POST['variable_width']) ? $_POST['variable_width'] : array();
$variable_height = isset($_POST['variable_height']) ? $_POST['variable_height'] : array();
$variable_stock = isset($_POST['variable_stock']) ? $_POST['variable_stock'] : array();
$variable_manage_stock = isset($_POST['variable_manage_stock']) ? $_POST['variable_manage_stock'] : array();
$variable_stock_status = isset($_POST['variable_stock_status']) ? $_POST['variable_stock_status'] : array();
$variable_backorders = isset($_POST['variable_backorders']) ? $_POST['variable_backorders'] : array();
$variable_enabled = isset($_POST['variable_enabled']) ? $_POST['variable_enabled'] : array();
$variable_is_virtual = isset($_POST['variable_is_virtual']) ? $_POST['variable_is_virtual'] : array();
$variable_is_downloadable = isset($_POST['variable_is_downloadable']) ? $_POST['variable_is_downloadable'] : array();
$max_loop = max(array_keys($_POST['variable_post_id']));
for ($i = 0; $i <= $max_loop; $i++) {
if (!isset($variable_post_id[$i])) {
continue;
}
$variation_id = absint($variable_post_id[$i]);
// Virtal/Downloadable
$is_downloadable = isset($variable_is_downloadable[$i]) ? 'yes' : 'no';
if (isset($variable_is_virtual[$i])) {
$is_virtual = 'yes';
} else {
if ($is_downloadable == 'yes') {
$is_virtual = 'yes';
} else {
$is_virtual = 'no';
}
}
// $is_virtual = isset( ) ? 'yes' : 'no';
// Enabled or disabled
$post_status = isset($variable_enabled[$i]) ? 'publish' : 'private';
$parent_manage_stock = isset($_POST['_manage_stock']) ? 'yes' : 'no';
$manage_stock = isset($variable_manage_stock[$i]) ? 'yes' : 'no';
// Generate a useful post title
$variation_post_title = sprintf(__('Variation #%s of %s', 'woocommerce'), absint($variation_id), esc_html(get_the_title($post_id)));
// Update or Add post
if (!$variation_id) {
$variation = array('post_title' => $variation_post_title, 'post_content' => '', 'post_status' => $post_status, 'post_author' => get_current_user_id(), 'post_parent' => $post_id, 'post_type' => 'product_variation', 'menu_order' => $variable_menu_order[$i]);
$variation_id = wp_insert_post($variation);
do_action('woocommerce_create_product_variation', $variation_id);
} else {
$wpdb->update($wpdb->posts, array('post_status' => $post_status, 'post_title' => $variation_post_title, 'menu_order' => $variable_menu_order[$i]), array('ID' => $variation_id));
do_action('woocommerce_update_product_variation', $variation_id);
}
// Only continue if we have a variation ID
if (!$variation_id) {
continue;
}
// Update post meta
update_post_meta($variation_id, '_sku', wc_clean($variable_sku[$i]));
//update_post_meta( $variation_id, '_stock', wc_clean( $variable_stock[ $i ] ) );
update_post_meta($variation_id, '_thumbnail_id', absint($upload_image_id[$i]));
update_post_meta($variation_id, '_virtual', wc_clean($is_virtual));
update_post_meta($variation_id, '_downloadable', wc_clean($is_downloadable));
update_post_meta($variation_id, '_manage_stock', $manage_stock);
// Only update stock status to user setting if changed by the user, but do so before looking at stock levels at variation level
if (!empty($variable_stock_status[$i])) {
wc_update_product_stock_status($variation_id, $variable_stock_status[$i]);
}
if ('yes' === $manage_stock) {
update_post_meta($variation_id, '_backorders', wc_clean($variable_backorders[$i]));
wc_update_product_stock($variation_id, wc_stock_amount($variable_stock[$i]));
} else {
delete_post_meta($variation_id, '_backorders');
delete_post_meta($variation_id, '_stock');
}
if (isset($variable_weight[$i])) {
update_post_meta($variation_id, '_weight', $variable_weight[$i] === '' ? '' : wc_format_decimal($variable_weight[$i]));
}
if (isset($variable_length[$i])) {
update_post_meta($variation_id, '_length', $variable_length[$i] === '' ? '' : wc_format_decimal($variable_length[$i]));
}
if (isset($variable_width[$i])) {
update_post_meta($variation_id, '_width', $variable_width[$i] === '' ? '' : wc_format_decimal($variable_width[$i]));
}
if (isset($variable_height[$i])) {
update_post_meta($variation_id, '_height', $variable_height[$i] === '' ? '' : wc_format_decimal($variable_height[$i]));
}
// Price handling
$regular_price = wc_format_decimal($variable_regular_price[$i]);
$sale_price = $variable_sale_price[$i] === '' ? '' : wc_format_decimal($variable_sale_price[$i]);
$date_from = wc_clean($variable_sale_price_dates_from[$i]);
$date_to = wc_clean($variable_sale_price_dates_to[$i]);
//.........这里部分代码省略.........
示例13: bulk_edit_save
//.........这里部分代码省略.........
}
break;
default:
break;
}
if (isset($new_price) && $new_price != $old_regular_price) {
$price_changed = true;
$new_price = round($new_price, wc_get_price_decimals());
$product->set_regular_price($new_price);
}
}
if (!empty($_REQUEST['change_sale_price'])) {
$change_sale_price = absint($_REQUEST['change_sale_price']);
$sale_price = esc_attr(stripslashes($_REQUEST['_sale_price']));
switch ($change_sale_price) {
case 1:
$new_price = $sale_price;
break;
case 2:
if (strstr($sale_price, '%')) {
$percent = str_replace('%', '', $sale_price) / 100;
$new_price = $old_sale_price + $old_sale_price * $percent;
} else {
$new_price = $old_sale_price + $sale_price;
}
break;
case 3:
if (strstr($sale_price, '%')) {
$percent = str_replace('%', '', $sale_price) / 100;
$new_price = max(0, $old_sale_price - $old_sale_price * $percent);
} else {
$new_price = max(0, $old_sale_price - $sale_price);
}
break;
case 4:
if (strstr($sale_price, '%')) {
$percent = str_replace('%', '', $sale_price) / 100;
$new_price = max(0, $product->regular_price - $product->regular_price * $percent);
} else {
$new_price = max(0, $product->regular_price - $sale_price);
}
break;
default:
break;
}
if (isset($new_price) && $new_price != $old_sale_price) {
$price_changed = true;
$new_price = !empty($new_price) || '0' === $new_price ? round($new_price, wc_get_price_decimals()) : '';
$product->set_sale_price($new_price);
}
}
if ($price_changed) {
$product->set_date_on_sale_to('');
$product->set_date_on_sale_from('');
if ($product->get_regular_price() < $product->get_sale_price()) {
$product->set_sale_price('');
}
}
}
// Handle Stock Data
$was_managing_stock = $product->get_manage_stock() ? 'yes' : 'no';
$stock_status = $product->get_stock_status();
$backorders = $product->get_backorders();
$backorders = !empty($_REQUEST['_backorders']) ? wc_clean($_REQUEST['_backorders']) : $backorders;
$stock_status = !empty($_REQUEST['_stock_status']) ? wc_clean($_REQUEST['_stock_status']) : $stock_status;
if (!empty($_REQUEST['_manage_stock'])) {
$manage_stock = 'yes' === wc_clean($_REQUEST['_manage_stock']) && 'grouped' !== $product->product_type ? 'yes' : 'no';
} else {
$manage_stock = $was_managing_stock;
}
$stock_amount = 'yes' === $manage_stock && isset($_REQUEST['_change_stock']) ? wc_stock_amount($_REQUEST['_change_stock']) : '';
if ('yes' === get_option('woocommerce_manage_stock')) {
// Apply product type constraints to stock status
if ($product->is_type('external')) {
// External always in stock
$stock_status = 'instock';
} elseif ($product->is_type('variable')) {
// Stock status is always determined by children
foreach ($product->get_children() as $child_id) {
$child = wc_get_product($child_id);
if (!$product->get_manage_stock()) {
$child->set_stock_status($stock_status);
$child->save();
}
}
$product = WC_Product_Variable::sync($product, false);
}
$product->set_manage_stock($manage_stock);
$product->set_backorders($backorders);
$product->save();
if (!$product->is_type('variable')) {
wc_update_product_stock_status($post_id, $stock_status);
}
wc_update_product_stock($post_id, $stock_amount);
} else {
$product->save();
wc_update_product_stock_status($post_id, $stock_status);
}
do_action('woocommerce_product_bulk_edit_save', $product);
}
示例14: mp_save_variation
public function mp_save_variation($id, $data)
{
global $wpdb;
/*================= my method*/
$menu_order = 0;
$attributes = (array) maybe_unserialize(get_post_meta($id, '_product_attributes', true));
/*
echo '<pre>';
print_r($attributes);
echo '</pre>';*/
// $data=array_reverse($data);
$cinv = 0;
foreach ($data as $variation_id) {
/*start of loop*/
$variation_id = isset($variation_id) ? absint($variation_id) : 0;
/*if($cinv==0 && isset($_POST['new_added_variation']) && $_POST['new_added_variation']>=1){
$post_title='Variation #'.$id.' of Product';
$product_data=array(
'post_author'=>get_current_user_id(),
'post_content'=>'',
'post_title'=>$post_title,
'post_status'=>'publish',
'post_type'=>'product_variation',
'post_parent'=>$id,
'menu_order'=>''
);
$var_id = wp_insert_post($product_data);
if($var_id!=''){
wp_delete_post($var_id);
}
}
$cinv++;*/
// SKU
if (isset($_POST['wkmp_variable_sku'][$variation_id])) {
$sku = get_post_meta($variation_id, '_sku', true);
$new_sku = wc_clean($_POST['wkmp_variable_sku'][$variation_id]);
$is_sku_unique = wc_product_has_unique_sku($variation_id, $new_sku);
if ('' == $new_sku) {
update_post_meta($variation_id, '_sku', '');
} elseif ($new_sku != $sku && $is_sku_unique) {
if (!empty($new_sku)) {
update_post_meta($variation_id, '_sku', $new_sku);
} else {
update_post_meta($variation_id, '_sku', '');
}
}
}
// Thumbnail
if (isset($_POST['upload_var_img'][$variation_id])) {
$attachment_id = $_POST['upload_var_img'][$variation_id];
if ($attachment_id) {
update_post_meta($variation_id, '_thumbnail_id', $attachment_id);
} else {
// delete_post_meta( $variation_id, '_thumbnail_id' );
update_post_meta($variation_id, '_thumbnail_id', 0);
}
}
// Virtual variation
if (isset($_POST['wkmp_variable_is_virtual'][$variation_id])) {
$is_virtual = $_POST['wkmp_variable_is_virtual'][$variation_id] == 'yes' ? 'yes' : 'no';
update_post_meta($variation_id, '_virtual', $is_virtual);
} else {
update_post_meta($variation_id, '_virtual', 'no');
}
// Downloadable variation
if (isset($_POST['wkmp_variable_is_downloadable'][$variation_id])) {
$is_downloadable = 'yes' == $_POST['wkmp_variable_is_downloadable'][$variation_id] ? 'yes' : 'no';
update_post_meta($variation_id, '_downloadable', $is_downloadable);
} else {
update_post_meta($variation_id, '_downloadable', 'no');
// $is_downloadable = get_post_meta( $variation_id, '_downloadable', true );
$is_downloadable = 'no';
}
/*// Shipping data
$this->mp_save_product_shipping_data( $variation_id, $_POST );*/
// Stock handling
if (isset($_POST['wkmp_variable_manage_stock'][$variation_id])) {
$managing_stock = 'yes' == $_POST['wkmp_variable_manage_stock'][$variation_id] ? 'yes' : 'no';
update_post_meta($variation_id, '_manage_stock', $managing_stock);
} else {
update_post_meta($variation_id, '_manage_stock', 'no');
// $managing_stock = get_post_meta( $variation_id, '_manage_stock', true );
$managing_stock = 'no';
}
// Only update stock status to user setting if changed by the user, but do so before looking at stock levels at variation level
if (isset($_POST['wkmp_variable_stock_status'][$variation_id])) {
$stock_status = 'instock' == $_POST['wkmp_variable_stock_status'][$variation_id] ? 'instock' : 'outofstock';
wc_update_product_stock_status($variation_id, $stock_status);
/*update_post_meta( $variation_id, '_stock_status', $managing_stock );*/
}
if ('yes' === $managing_stock) {
if (isset($_POST['wkmp_variable_backorders'][$variation_id])) {
if ('notify' == $_POST['wkmp_variable_backorders'][$variation_id]) {
$backorders = 'notify';
} else {
$backorders = 'yes' == $_POST['wkmp_variable_backorders'][$variation_id] ? 'yes' : 'no';
}
} else {
$backorders = 'no';
}
//.........这里部分代码省略.........
示例15: save_variations
/**
* Save meta box data
*
* @deprecated 2.4.0 Deprecated in favor to WC_AJAX::save_variations()
*/
public static function save_variations($post_id, $post)
{
global $wpdb;
$attributes = (array) maybe_unserialize(get_post_meta($post_id, '_product_attributes', true));
if (isset($_POST['variable_sku'])) {
$variable_post_id = $_POST['variable_post_id'];
$variable_sku = $_POST['variable_sku'];
$variable_regular_price = $_POST['variable_regular_price'];
$variable_sale_price = $_POST['variable_sale_price'];
$upload_image_id = $_POST['upload_image_id'];
$variable_download_limit = $_POST['variable_download_limit'];
$variable_download_expiry = $_POST['variable_download_expiry'];
$variable_shipping_class = $_POST['variable_shipping_class'];
$variable_tax_class = isset($_POST['variable_tax_class']) ? $_POST['variable_tax_class'] : array();
$variable_menu_order = $_POST['variation_menu_order'];
$variable_sale_price_dates_from = $_POST['variable_sale_price_dates_from'];
$variable_sale_price_dates_to = $_POST['variable_sale_price_dates_to'];
$variable_weight = isset($_POST['variable_weight']) ? $_POST['variable_weight'] : array();
$variable_length = isset($_POST['variable_length']) ? $_POST['variable_length'] : array();
$variable_width = isset($_POST['variable_width']) ? $_POST['variable_width'] : array();
$variable_height = isset($_POST['variable_height']) ? $_POST['variable_height'] : array();
$variable_enabled = isset($_POST['variable_enabled']) ? $_POST['variable_enabled'] : array();
$variable_is_virtual = isset($_POST['variable_is_virtual']) ? $_POST['variable_is_virtual'] : array();
$variable_is_downloadable = isset($_POST['variable_is_downloadable']) ? $_POST['variable_is_downloadable'] : array();
$variable_manage_stock = isset($_POST['variable_manage_stock']) ? $_POST['variable_manage_stock'] : array();
$variable_stock = isset($_POST['variable_stock']) ? $_POST['variable_stock'] : array();
$variable_backorders = isset($_POST['variable_backorders']) ? $_POST['variable_backorders'] : array();
$variable_stock_status = isset($_POST['variable_stock_status']) ? $_POST['variable_stock_status'] : array();
$variable_description = isset($_POST['variable_description']) ? $_POST['variable_description'] : array();
$max_loop = max(array_keys($_POST['variable_post_id']));
for ($i = 0; $i <= $max_loop; $i++) {
if (!isset($variable_post_id[$i])) {
continue;
}
$variation_id = absint($variable_post_id[$i]);
// Checkboxes
$is_virtual = isset($variable_is_virtual[$i]) ? 'yes' : 'no';
$is_downloadable = isset($variable_is_downloadable[$i]) ? 'yes' : 'no';
$post_status = isset($variable_enabled[$i]) ? 'publish' : 'private';
$manage_stock = isset($variable_manage_stock[$i]) ? 'yes' : 'no';
// Generate a useful post title
$variation_post_title = sprintf(__('Variation #%s of %s', 'woocommerce'), absint($variation_id), esc_html(get_the_title($post_id)));
// Update or Add post
if (!$variation_id) {
$variation = array('post_title' => $variation_post_title, 'post_content' => '', 'post_status' => $post_status, 'post_author' => get_current_user_id(), 'post_parent' => $post_id, 'post_type' => 'product_variation', 'menu_order' => $variable_menu_order[$i]);
$variation_id = wp_insert_post($variation);
do_action('woocommerce_create_product_variation', $variation_id);
} else {
$wpdb->update($wpdb->posts, array('post_status' => $post_status, 'post_title' => $variation_post_title, 'menu_order' => $variable_menu_order[$i]), array('ID' => $variation_id));
do_action('woocommerce_update_product_variation', $variation_id);
}
// Only continue if we have a variation ID
if (!$variation_id) {
continue;
}
// Unique SKU
$sku = get_post_meta($variation_id, '_sku', true);
$new_sku = wc_clean(stripslashes($variable_sku[$i]));
if ('' == $new_sku) {
update_post_meta($variation_id, '_sku', '');
} elseif ($new_sku !== $sku) {
if (!empty($new_sku)) {
$unique_sku = wc_product_has_unique_sku($variation_id, $new_sku);
if (!$unique_sku) {
WC_Admin_Meta_Boxes::add_error(__('Variation SKU must be unique.', 'woocommerce'));
} else {
update_post_meta($variation_id, '_sku', $new_sku);
}
} else {
update_post_meta($variation_id, '_sku', '');
}
}
// Update post meta
update_post_meta($variation_id, '_thumbnail_id', absint($upload_image_id[$i]));
update_post_meta($variation_id, '_virtual', wc_clean($is_virtual));
update_post_meta($variation_id, '_downloadable', wc_clean($is_downloadable));
if (isset($variable_weight[$i])) {
update_post_meta($variation_id, '_weight', '' === $variable_weight[$i] ? '' : wc_format_decimal($variable_weight[$i]));
}
if (isset($variable_length[$i])) {
update_post_meta($variation_id, '_length', '' === $variable_length[$i] ? '' : wc_format_decimal($variable_length[$i]));
}
if (isset($variable_width[$i])) {
update_post_meta($variation_id, '_width', '' === $variable_width[$i] ? '' : wc_format_decimal($variable_width[$i]));
}
if (isset($variable_height[$i])) {
update_post_meta($variation_id, '_height', '' === $variable_height[$i] ? '' : wc_format_decimal($variable_height[$i]));
}
// Stock handling
update_post_meta($variation_id, '_manage_stock', $manage_stock);
// Only update stock status to user setting if changed by the user, but do so before looking at stock levels at variation level
if (!empty($variable_stock_status[$i])) {
wc_update_product_stock_status($variation_id, $variable_stock_status[$i]);
}
if ('yes' === $manage_stock) {
//.........这里部分代码省略.........