本文整理汇总了PHP中WC_Product_Variable::sync方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product_Variable::sync方法的具体用法?PHP WC_Product_Variable::sync怎么用?PHP WC_Product_Variable::sync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Product_Variable
的用法示例。
在下文中一共展示了WC_Product_Variable::sync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: save_variations
//.........这里部分代码省略.........
} else {
delete_post_meta($variation_id, '_tax_class');
}
}
// Downloads
if ('yes' == $is_downloadable) {
// Downloadable files
if (isset($variation['downloads']) && is_array($variation['downloads'])) {
$this->save_downloadable_files($id, $variation['downloads'], $variation_id);
}
// Download limit
if (isset($variation['download_limit'])) {
$download_limit = absint($variation['download_limit']);
update_post_meta($variation_id, '_download_limit', !$download_limit ? '' : $download_limit);
}
// Download expiry
if (isset($variation['download_expiry'])) {
$download_expiry = absint($variation['download_expiry']);
update_post_meta($variation_id, '_download_expiry', !$download_expiry ? '' : $download_expiry);
}
} else {
update_post_meta($variation_id, '_download_limit', '');
update_post_meta($variation_id, '_download_expiry', '');
update_post_meta($variation_id, '_downloadable_files', '');
}
// Update taxonomies
if (isset($variation['attributes'])) {
$updated_attribute_keys = array();
foreach ($variation['attributes'] as $attribute_key => $attribute) {
if (!isset($attribute['name'])) {
continue;
}
$_attribute = array();
if (isset($attribute['slug'])) {
$taxonomy = $this->get_attribute_taxonomy_by_slug($attribute['slug']);
}
if (!$taxonomy) {
$taxonomy = sanitize_title($attribute['name']);
}
if (isset($attributes[$taxonomy])) {
$_attribute = $attributes[$taxonomy];
}
if (isset($_attribute['is_variation']) && $_attribute['is_variation']) {
$_attribute_key = 'attribute_' . sanitize_title($_attribute['name']);
$updated_attribute_keys[] = $_attribute_key;
if (isset($_attribute['is_taxonomy']) && $_attribute['is_taxonomy']) {
// Don't use wc_clean as it destroys sanitized characters
$_attribute_value = isset($attribute['option']) ? sanitize_title(stripslashes($attribute['option'])) : '';
} else {
$_attribute_value = isset($attribute['option']) ? wc_clean(stripslashes($attribute['option'])) : '';
}
update_post_meta($variation_id, $_attribute_key, $_attribute_value);
}
}
// Remove old taxonomies attributes so data is kept up to date - first get attribute key names
$delete_attribute_keys = $wpdb->get_col($wpdb->prepare("SELECT meta_key FROM {$wpdb->postmeta} WHERE meta_key LIKE 'attribute_%%' AND meta_key NOT IN ( '" . implode("','", $updated_attribute_keys) . "' ) AND post_id = %d;", $variation_id));
foreach ($delete_attribute_keys as $key) {
delete_post_meta($variation_id, $key);
}
}
do_action('woocommerce_api_save_product_variation', $variation_id, $menu_order, $variation);
}
// Update parent if variable so price sorting works and stays in sync with the cheapest child
WC_Product_Variable::sync($id);
// Update default attributes options setting
if (isset($data['default_attribute'])) {
$data['default_attributes'] = $data['default_attribute'];
}
if (isset($data['default_attributes']) && is_array($data['default_attributes'])) {
$default_attributes = array();
foreach ($data['default_attributes'] as $default_attr_key => $default_attr) {
if (!isset($default_attr['name'])) {
continue;
}
$taxonomy = sanitize_title($default_attr['name']);
if (isset($default_attr['slug'])) {
$taxonomy = $this->get_attribute_taxonomy_by_slug($default_attr['slug']);
}
if (isset($attributes[$taxonomy])) {
$_attribute = $attributes[$taxonomy];
if ($_attribute['is_variation']) {
$value = '';
if (isset($default_attr['option'])) {
if ($_attribute['is_taxonomy']) {
// Don't use wc_clean as it destroys sanitized characters
$value = sanitize_title(trim(stripslashes($default_attr['option'])));
} else {
$value = wc_clean(trim(stripslashes($default_attr['option'])));
}
}
if ($value) {
$default_attributes[$taxonomy] = $value;
}
}
}
}
update_post_meta($id, '_default_attributes', $default_attributes);
}
return true;
}
示例2: bulk_edit_variations
/**
* Bulk edit variations via AJAX
*/
public static function bulk_edit_variations()
{
ob_start();
check_ajax_referer('bulk-edit-variations', 'security');
// Check permissions again and make sure we have what we need
if (!current_user_can('edit_products') || empty($_POST['product_id']) || empty($_POST['bulk_action'])) {
die(-1);
}
$product_id = absint($_POST['product_id']);
$bulk_action = wc_clean($_POST['bulk_action']);
$data = !empty($_POST['data']) ? array_map('wc_clean', $_POST['data']) : array();
$variations = array();
if (apply_filters('woocommerce_bulk_edit_variations_need_children', true)) {
$variations = get_posts(array('post_parent' => $product_id, 'posts_per_page' => -1, 'post_type' => 'product_variation', 'fields' => 'ids', 'post_status' => array('publish', 'private')));
}
if (method_exists(__CLASS__, "variation_bulk_action_{$bulk_action}")) {
call_user_func(array(__CLASS__, "variation_bulk_action_{$bulk_action}"), $variations, $data);
} else {
do_action('woocommerce_bulk_edit_variations_default', $bulk_action, $data, $product_id, $variations);
}
do_action('woocommerce_bulk_edit_variations', $bulk_action, $data, $product_id, $variations);
// Sync and update transients
WC_Product_Variable::sync($product_id);
wc_delete_product_transients($product_id);
die;
}
示例3: save_variations
//.........这里部分代码省略.........
if (isset($variable_tax_class[$i]) && $variable_tax_class[$i] !== 'parent') {
update_post_meta($variation_id, '_tax_class', wc_clean($variable_tax_class[$i]));
} else {
delete_post_meta($variation_id, '_tax_class');
}
if ('yes' == $is_downloadable) {
update_post_meta($variation_id, '_download_limit', wc_clean($variable_download_limit[$i]));
update_post_meta($variation_id, '_download_expiry', wc_clean($variable_download_expiry[$i]));
$files = array();
$file_names = isset($_POST['_wc_variation_file_names'][$variation_id]) ? array_map('wc_clean', $_POST['_wc_variation_file_names'][$variation_id]) : array();
$file_urls = isset($_POST['_wc_variation_file_urls'][$variation_id]) ? array_map('wc_clean', $_POST['_wc_variation_file_urls'][$variation_id]) : array();
$file_url_size = sizeof($file_urls);
$allowed_file_types = get_allowed_mime_types();
for ($ii = 0; $ii < $file_url_size; $ii++) {
if (!empty($file_urls[$ii])) {
// Find type and file URL
if (0 === strpos($file_urls[$ii], 'http')) {
$file_is = 'absolute';
$file_url = esc_url_raw($file_urls[$ii]);
} elseif ('[' === substr($file_urls[$ii], 0, 1) && ']' === substr($file_urls[$ii], -1)) {
$file_is = 'shortcode';
$file_url = wc_clean($file_urls[$ii]);
} else {
$file_is = 'relative';
$file_url = wc_clean($file_urls[$ii]);
}
$file_name = wc_clean($file_names[$ii]);
$file_hash = md5($file_url);
// Validate the file extension
if (in_array($file_is, array('absolute', 'relative'))) {
$file_type = wp_check_filetype(strtok($file_url, '?'));
$parsed_url = parse_url($file_url, PHP_URL_PATH);
$extension = pathinfo($parsed_url, PATHINFO_EXTENSION);
if (!empty($extension) && !in_array($file_type['type'], $allowed_file_types)) {
WC_Admin_Meta_Boxes::add_error(sprintf(__('The downloadable file %s cannot be used as it does not have an allowed file type. Allowed types include: %s', 'woocommerce'), '<code>' . basename($file_url) . '</code>', '<code>' . implode(', ', array_keys($allowed_file_types)) . '</code>'));
continue;
}
}
// Validate the file exists
if ('relative' === $file_is && !apply_filters('woocommerce_downloadable_file_exists', file_exists($file_url), $file_url)) {
WC_Admin_Meta_Boxes::add_error(sprintf(__('The downloadable file %s cannot be used as it does not exist on the server.', 'woocommerce'), '<code>' . $file_url . '</code>'));
continue;
}
$files[$file_hash] = array('name' => $file_name, 'file' => $file_url);
}
}
// grant permission to any newly added files on any existing orders for this product prior to saving
do_action('woocommerce_process_product_file_download_paths', $post_id, $variation_id, $files);
update_post_meta($variation_id, '_downloadable_files', $files);
} else {
update_post_meta($variation_id, '_download_limit', '');
update_post_meta($variation_id, '_download_expiry', '');
update_post_meta($variation_id, '_downloadable_files', '');
}
update_post_meta($variation_id, '_variation_description', wp_kses_post($variable_description[$i]));
// Save shipping class
$variable_shipping_class[$i] = !empty($variable_shipping_class[$i]) ? (int) $variable_shipping_class[$i] : '';
wp_set_object_terms($variation_id, $variable_shipping_class[$i], 'product_shipping_class');
// Update Attributes
$updated_attribute_keys = array();
foreach ($attributes as $attribute) {
if ($attribute['is_variation']) {
$attribute_key = 'attribute_' . sanitize_title($attribute['name']);
$updated_attribute_keys[] = $attribute_key;
if ($attribute['is_taxonomy']) {
// Don't use wc_clean as it destroys sanitized characters
$value = isset($_POST[$attribute_key][$i]) ? sanitize_title(stripslashes($_POST[$attribute_key][$i])) : '';
} else {
$value = isset($_POST[$attribute_key][$i]) ? wc_clean(stripslashes($_POST[$attribute_key][$i])) : '';
}
update_post_meta($variation_id, $attribute_key, $value);
}
}
// Remove old taxonomies attributes so data is kept up to date - first get attribute key names
$delete_attribute_keys = $wpdb->get_col($wpdb->prepare("SELECT meta_key FROM {$wpdb->postmeta} WHERE meta_key LIKE 'attribute_%%' AND meta_key NOT IN ( '" . implode("','", $updated_attribute_keys) . "' ) AND post_id = %d;", $variation_id));
foreach ($delete_attribute_keys as $key) {
delete_post_meta($variation_id, $key);
}
do_action('woocommerce_save_product_variation', $variation_id, $i);
}
}
// Update parent if variable so price sorting works and stays in sync with the cheapest child
WC_Product_Variable::sync($post_id);
// Update default attribute options setting
$default_attributes = array();
foreach ($attributes as $attribute) {
if ($attribute['is_variation']) {
// Don't use wc_clean as it destroys sanitized characters
if (isset($_POST['default_attribute_' . sanitize_title($attribute['name'])])) {
$value = sanitize_title(trim(stripslashes($_POST['default_attribute_' . sanitize_title($attribute['name'])])));
} else {
$value = '';
}
if ($value) {
$default_attributes[sanitize_title($attribute['name'])] = $value;
}
}
}
update_post_meta($post_id, '_default_attributes', $default_attributes);
}
示例4: set_stock
/**
* Set stock level of the product variation.
*
* Uses queries rather than update_post_meta so we can do this in one query (to avoid stock issues).
* We cannot rely on the original loaded value in case another order was made since then.
*
* @param int $amount
* @param string $mode can be set, add, or subtract
* @return int new stock level
*/
public function set_stock($amount = null, $mode = 'set')
{
global $wpdb;
if (!is_null($amount) && true === $this->managing_stock()) {
// Ensure key exists
add_post_meta($this->variation_id, '_stock', 0, true);
// Update stock in DB directly
switch ($mode) {
case 'add':
$wpdb->query($wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = meta_value + %f WHERE post_id = %d AND meta_key='_stock'", $amount, $this->variation_id));
break;
case 'subtract':
$wpdb->query($wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = meta_value - %f WHERE post_id = %d AND meta_key='_stock'", $amount, $this->variation_id));
break;
default:
$wpdb->query($wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = %f WHERE post_id = %d AND meta_key='_stock'", $amount, $this->variation_id));
break;
}
// Clear caches
wp_cache_delete($this->variation_id, 'post_meta');
// Clear total stock transient
delete_transient('wc_product_total_stock_' . $this->id . WC_Cache_Helper::get_transient_version('product'));
// Stock status
$this->check_stock_status();
// Sync the parent
WC_Product_Variable::sync($this->id);
// Trigger action
do_action('woocommerce_variation_set_stock', $this);
} elseif (!is_null($amount)) {
return $this->parent->set_stock($amount, $mode);
}
return $this->get_stock_quantity();
}
示例5: set_stock
/**
* Set stock level of the product variation.
* @param int $amount
* @param bool $force_variation_stock If true, the variation's stock will be updated and not the parents.
* @return int
* @todo Need to return 0 if is_null? Or something. Should not be just return.
*/
function set_stock($amount = null, $force_variation_stock = false)
{
if (is_null($amount)) {
return;
}
if ($amount === '' && $force_variation_stock) {
// If amount is an empty string, stock management is being turned off at variation level
$this->variation_has_stock = false;
$this->stock = '';
unset($this->manage_stock);
// Update meta
update_post_meta($this->variation_id, '_stock', '');
// Refresh parent prices
WC_Product_Variable::sync($this->id);
} elseif ($this->variation_has_stock || $force_variation_stock) {
// Update stock amount
$this->stock = intval($amount);
$this->variation_has_stock = true;
$this->manage_stock = 'yes';
// Update meta
update_post_meta($this->variation_id, '_stock', $this->stock);
// Clear total stock transient
delete_transient('wc_product_total_stock_' . $this->id);
// Check parents out of stock attribute
if (!$this->is_in_stock()) {
// Check parent
$parent_product = get_product($this->id);
// Only continue if the parent has backorders off and all children are stock managed and out of stock
if (!$parent_product->backorders_allowed() && $parent_product->get_total_stock() <= get_option('woocommerce_notify_no_stock_amount')) {
$all_managed = true;
if (sizeof($parent_product->get_children()) > 0) {
foreach ($parent_product->get_children() as $child_id) {
$stock = get_post_meta($child_id, '_stock', true);
if ($stock == '') {
$all_managed = false;
break;
}
}
}
if ($all_managed) {
$this->set_stock_status('outofstock');
}
}
} elseif ($this->is_in_stock()) {
$this->set_stock_status('instock');
}
// Refresh parent prices
WC_Product_Variable::sync($this->id);
// Trigger action
do_action('woocommerce_product_set_stock', $this);
return $this->get_stock_quantity();
} else {
return parent::set_stock($amount);
}
}
示例6: dokan_save_variations
//.........这里部分代码省略.........
$date_from = wc_clean($variable_sale_price_dates_from[$i]);
$date_to = wc_clean($variable_sale_price_dates_to[$i]);
update_post_meta($variation_id, '_regular_price', $regular_price);
update_post_meta($variation_id, '_sale_price', $sale_price);
update_post_meta($variation_id, '_list_price_mrp', $mrp);
// Save Dates
if ($date_from) {
update_post_meta($variation_id, '_sale_price_dates_from', strtotime($date_from));
} else {
update_post_meta($variation_id, '_sale_price_dates_from', '');
}
if ($date_to) {
update_post_meta($variation_id, '_sale_price_dates_to', strtotime($date_to));
} else {
update_post_meta($variation_id, '_sale_price_dates_to', '');
}
if ($date_to && !$date_from) {
update_post_meta($variation_id, '_sale_price_dates_from', strtotime('NOW', current_time('timestamp')));
}
// Update price if on sale
if ($sale_price != '' && $date_to == '' && $date_from == '') {
update_post_meta($variation_id, '_price', $sale_price);
} else {
update_post_meta($variation_id, '_price', $regular_price);
}
if ($sale_price != '' && $date_from && strtotime($date_from) < strtotime('NOW', current_time('timestamp'))) {
update_post_meta($variation_id, '_price', $sale_price);
}
if ($date_to && strtotime($date_to) < strtotime('NOW', current_time('timestamp'))) {
update_post_meta($variation_id, '_price', $regular_price);
update_post_meta($variation_id, '_sale_price_dates_from', '');
update_post_meta($variation_id, '_sale_price_dates_to', '');
}
if (isset($variable_tax_class[$i]) && $variable_tax_class[$i] !== 'parent') {
update_post_meta($variation_id, '_tax_class', wc_clean($variable_tax_class[$i]));
} else {
delete_post_meta($variation_id, '_tax_class');
}
if ($is_downloadable == 'yes') {
update_post_meta($variation_id, '_download_limit', wc_clean($variable_download_limit[$i]));
update_post_meta($variation_id, '_download_expiry', wc_clean($variable_download_expiry[$i]));
$files = array();
$file_names = isset($_POST['_wc_variation_file_names'][$variation_id]) ? array_map('wc_clean', $_POST['_wc_variation_file_names'][$variation_id]) : array();
$file_urls = isset($_POST['_wc_variation_file_urls'][$variation_id]) ? array_map('esc_url_raw', array_map('trim', $_POST['_wc_variation_file_urls'][$variation_id])) : array();
$file_url_size = sizeof($file_urls);
for ($ii = 0; $ii < $file_url_size; $ii++) {
if (!empty($file_urls[$ii])) {
$files[md5($file_urls[$ii])] = array('name' => $file_names[$ii], 'file' => $file_urls[$ii]);
}
}
// grant permission to any newly added files on any existing orders for this product prior to saving
do_action('woocommerce_process_product_file_download_paths', $post_id, $variation_id, $files);
update_post_meta($variation_id, '_downloadable_files', $files);
} else {
update_post_meta($variation_id, '_download_limit', '');
update_post_meta($variation_id, '_download_expiry', '');
update_post_meta($variation_id, '_downloadable_files', '');
}
// Save shipping class
$variable_shipping_class[$i] = !empty($variable_shipping_class[$i]) ? (int) $variable_shipping_class[$i] : '';
wp_set_object_terms($variation_id, $variable_shipping_class[$i], 'product_shipping_class');
// Remove old taxonomies attributes so data is kept up to date
if ($variation_id) {
$wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE 'attribute_%%' AND post_id = %d;", $variation_id));
wp_cache_delete($variation_id, 'post_meta');
}
// Update taxonomies
foreach ($attributes as $attribute) {
if ($attribute['is_variation']) {
// Don't use wc_clean as it destroys sanitized characters
if (isset($_POST['attribute_' . sanitize_title($attribute['name'])][$i])) {
$value = sanitize_title(trim(stripslashes($_POST['attribute_' . sanitize_title($attribute['name'])][$i])));
} else {
$value = '';
}
update_post_meta($variation_id, 'attribute_' . sanitize_title($attribute['name']), $value);
}
}
do_action('woocommerce_save_product_variation', $variation_id, $i);
}
}
// Update parent if variable so price sorting works and stays in sync with the cheapest child
WC_Product_Variable::sync($post_id);
// Update default attribute options setting
$default_attributes = array();
foreach ($attributes as $attribute) {
if ($attribute['is_variation']) {
// Don't use wc_clean as it destroys sanitized characters
if (isset($_POST['default_attribute_' . sanitize_title($attribute['name'])])) {
$value = sanitize_title(trim(stripslashes($_POST['default_attribute_' . sanitize_title($attribute['name'])])));
} else {
$value = '';
}
if ($value) {
$default_attributes[sanitize_title($attribute['name'])] = $value;
}
}
}
update_post_meta($post_id, '_default_attributes', $default_attributes);
}
示例7: update_post_meta_fields
/**
* Update post meta fields.
*
* @param WP_Post $post Post data.
* @param WP_REST_Request $request Request data.
* @return bool|WP_Error
*/
protected function update_post_meta_fields($post, $request)
{
$product = wc_get_product($post);
// Check for featured/gallery images, upload it and set it.
if (isset($request['images'])) {
$product = $this->save_product_images($product, $request['images']);
}
// Save product meta fields.
$product = $this->save_product_meta($product, $request);
// Save variations.
if ($product->is_type('variable')) {
if (isset($request['variations']) && is_array($request['variations'])) {
$this->save_variations_data($product, $request);
} else {
// Just sync variations.
$product = WC_Product_Variable::sync($product, false);
}
}
$product->save();
return true;
}
示例8: dispatch
//.........这里部分代码省略.........
}
break;
case 3:
// Check access - cannot use nonce here as it will expire after multiple requests
if (!current_user_can('manage_woocommerce')) {
die;
}
add_filter('http_request_timeout', array($this, 'bump_request_timeout'));
if (function_exists('gc_enable')) {
gc_enable();
}
@set_time_limit(0);
@ob_flush();
@flush();
$wpdb->hide_errors();
$file = stripslashes($_POST['file']);
$mapping = json_decode(stripslashes($_POST['mapping']), true);
$start_pos = isset($_POST['start_pos']) ? absint($_POST['start_pos']) : 0;
$end_pos = isset($_POST['end_pos']) ? absint($_POST['end_pos']) : '';
$position = $this->import_start($file, $mapping, $start_pos, $end_pos);
$this->import();
$this->import_end();
$results = array();
$results['import_results'] = $this->import_results;
$results['processed_terms'] = $this->processed_terms;
$results['processed_posts'] = $this->processed_posts;
$results['post_orphans'] = $this->post_orphans;
$results['attachments'] = $this->attachments;
$results['upsell_skus'] = $this->upsell_skus;
$results['crosssell_skus'] = $this->crosssell_skus;
echo "<!--WC_START-->";
echo json_encode($results);
echo "<!--WC_END-->";
exit;
break;
case 4:
// Check access - cannot use nonce here as it will expire after multiple requests
if (!current_user_can('manage_woocommerce')) {
die;
}
add_filter('http_request_timeout', array($this, 'bump_request_timeout'));
if (function_exists('gc_enable')) {
gc_enable();
}
@set_time_limit(0);
@ob_flush();
@flush();
$wpdb->hide_errors();
$this->processed_terms = isset($_POST['processed_terms']) ? $_POST['processed_terms'] : array();
$this->processed_posts = isset($_POST['processed_posts']) ? $_POST['processed_posts'] : array();
$this->post_orphans = isset($_POST['post_orphans']) ? $_POST['post_orphans'] : array();
$this->crosssell_skus = isset($_POST['crosssell_skus']) ? array_filter((array) $_POST['crosssell_skus']) : array();
$this->upsell_skus = isset($_POST['upsell_skus']) ? array_filter((array) $_POST['upsell_skus']) : array();
_e('Cleaning up...', 'wc_csv_import') . ' ';
wp_defer_term_counting(true);
wp_defer_comment_counting(true);
_e('Clearing transients...', 'wc_csv_import') . ' ';
echo 'Reticulating Splines...' . ' ';
// Easter egg
// reset transients for products
if (function_exists('wc_delete_product_transients')) {
wc_delete_product_transients();
} else {
$woocommerce->clear_product_transients();
}
delete_transient('wc_attribute_taxonomies');
$wpdb->query("DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE ('_transient_wc_product_type_%')");
_e('Backfilling parents...', 'wc_csv_import') . ' ';
$this->backfill_parents();
if (!empty($this->upsell_skus)) {
_e('Linking upsells...', 'wc_csv_import') . ' ';
foreach ($this->upsell_skus as $post_id => $skus) {
$this->link_product_skus('upsell', $post_id, $skus);
}
}
if (!empty($this->crosssell_skus)) {
_e('Linking crosssells...', 'wc_csv_import') . ' ';
foreach ($this->crosssell_skus as $post_id => $skus) {
$this->link_product_skus('crosssell', $post_id, $skus);
}
}
if ('woocommerce_variation_csv' == $this->import_page && !empty($this->processed_posts)) {
_e('Syncing variations...', 'wc_csv_import') . ' ';
$synced = array();
foreach ($this->processed_posts as $post_id) {
$parent = wp_get_post_parent_id($post_id);
if (!in_array($parent, $synced)) {
WC_Product_Variable::sync($parent);
$synced[] = $parent;
}
}
}
// SUCCESS
_e('Finished. Import complete.', 'wc_csv_import');
$this->import_end();
exit;
break;
}
$this->footer();
}
示例9: update_post_meta_fields
/**
* Update post meta fields.
*
* @param WP_Post $post
* @param WP_REST_Request $request
* @return bool|WP_Error
*/
protected function update_post_meta_fields($post, $request)
{
try {
$product = wc_get_product($post);
// Check for featured/gallery images, upload it and set it.
if (isset($request['images'])) {
$this->save_product_images($product->id, $request['images']);
}
// Save product meta fields.
$this->save_product_meta($product, $request);
// Save variations.
if ($product->is_type('variable')) {
if (isset($request['variations']) && is_array($request['variations'])) {
$this->save_variations_data($product, $request);
} else {
// Just sync variations.
WC_Product_Variable::sync($product->id);
WC_Product_Variable::sync_stock_status($product->id);
}
}
return true;
} catch (WC_REST_Exception $e) {
return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode()));
}
}
示例10: bulk_edit_variations
//.........这里部分代码省略.........
case 'variable_width':
case 'variable_height':
if (empty($data['value'])) {
break;
}
$value = wc_clean($data['value']);
$field = str_replace('variable', '', $bulk_action);
$wpdb->query($wpdb->prepare("\n\t\t\t\t\tUPDATE {$wpdb->postmeta} AS postmeta\n\t\t\t\t\tINNER JOIN {$wpdb->posts} AS posts ON posts.post_parent = %d\n\t\t\t\t\tSET postmeta.meta_value = %s\n\t\t\t\t\tWHERE postmeta.meta_key = '%s'\n\t\t\t\t\tAND postmeta.post_id = posts.ID\n\t\t\t\t ", $product_id, $value, $field));
break;
case 'variable_download_limit':
case 'variable_download_expiry':
if (empty($data['value'])) {
break;
}
$value = wc_clean($data['value']);
$field = str_replace('variable', '', $bulk_action);
foreach ($variations as $variation_id) {
if ('yes' === get_post_meta($variation_id, '_downloadable', true)) {
update_post_meta($variation_id, $field, $value);
} else {
update_post_meta($variation_id, $field, '');
}
}
break;
case 'delete_all':
if (isset($data['allowed']) && 'true' === $data['allowed']) {
foreach ($variations as $variation_id) {
wp_delete_post($variation_id);
}
}
break;
case 'variable_regular_price_increase':
case 'variable_regular_price_decrease':
case 'variable_sale_price_increase':
case 'variable_sale_price_decrease':
if (empty($data['value'])) {
break;
}
$field = str_replace(array('variable', '_increase', '_decrease'), '', $bulk_action);
$operator = 'increase' === substr($bulk_action, -8) ? +1 : -1;
foreach ($variations as $variation_id) {
// Price fields
$regular_price = get_post_meta($variation_id, '_regular_price', true);
$sale_price = get_post_meta($variation_id, '_sale_price', true);
// Date fields
$date_from = get_post_meta($variation_id, '_sale_price_dates_from', true);
$date_to = get_post_meta($variation_id, '_sale_price_dates_to', true);
$date_from = !empty($date_from) ? date('Y-m-d', $date_from) : '';
$date_to = !empty($date_to) ? date('Y-m-d', $date_to) : '';
if ('%' === substr($data['value'], -1)) {
$percent = wc_format_decimal(substr($data['value'], 0, -1));
if ('_regular_price' === $field) {
$regular_price += $regular_price / 100 * $percent * $operator;
} else {
$sale_price += $sale_price / 100 * $percent * $operator;
}
} else {
if ('_regular_price' === $field) {
$regular_price += $data['value'];
} else {
$sale_price += $data['value'];
}
}
_wc_save_product_price($variation_id, $regular_price, $sale_price, $date_from, $date_to);
}
break;
case 'variable_sale_schedule':
if (!isset($data['date_from']) && !isset($data['date_to'])) {
break;
}
foreach ($variations as $variation_id) {
// Price fields
$regular_price = get_post_meta($variation_id, '_regular_price', true);
$sale_price = get_post_meta($variation_id, '_sale_price', true);
// Date fields
$date_from = get_post_meta($variation_id, '_sale_price_dates_from', true);
$date_to = get_post_meta($variation_id, '_sale_price_dates_to', true);
if ('false' === $data['date_from']) {
$date_from = !empty($date_from) ? date('Y-m-d', $date_from) : '';
} else {
$date_from = $data['date_from'];
}
if ('false' === $data['date_to']) {
$date_to = !empty($date_to) ? date('Y-m-d', $date_to) : '';
} else {
$date_to = $data['date_to'];
}
_wc_save_product_price($variation_id, $regular_price, $sale_price, $date_from, $date_to);
}
break;
default:
do_action('woocommerce_bulk_edit_variations_default', $bulk_action, $data, $product_id, $variations);
break;
}
do_action('woocommerce_bulk_edit_variations', $bulk_action, $data, $product_id, $variations);
// Sync and update transients
WC_Product_Variable::sync($product_id);
wc_delete_product_transients($product_id);
die;
}
示例11: foreach
}
/* update _price with sale price */
$rows = $wpdb->get_results($wpdb->prepare("SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_value !='' AND meta_key = %s", $pre_meta_key . '_sale_price'));
foreach ($rows as $row) {
update_post_meta($row->post_id, $pre_meta_key . '_price', $row->meta_value);
}
}
}
/* rename options regions */
delete_option('_oga_wppbc_countries_groups');
add_option('wc_price_based_country_regions', $regions);
/* sync variable products */
require_once WC()->plugin_path() . '/includes/class-wc-product-variable.php';
$rows = $wpdb->get_results($wpdb->prepare("SELECT distinct post_parent FROM {$wpdb->posts} where post_type = %s", 'product_variation'));
foreach ($rows as $row) {
WC_Product_Variable::sync($row->post_parent);
}
/* rename and update test options */
add_option('wc_price_based_country_test_mode', get_option('wc_price_based_country_debug_mode'));
delete_option('wc_price_based_country_debug_mode');
$test_ip = get_option('wc_price_based_country_debug_ip');
if ($test_ip) {
$country = WC_Geolocation::geolocate_ip($test_ip);
add_option('wc_price_based_country_test_country', $country['country']);
}
delete_option('wc_price_based_country_debug_ip');
/* unschedule geoip donwload */
if (wp_next_scheduled('wcpbc_update_geoip')) {
wp_clear_scheduled_hook('wcpbc_update_geoip');
}
delete_option('wc_price_based_country_update_geoip');
示例12: edit_product
/**
* Edit a product
*
* @since 2.2
* @param int $id the product ID
* @param array $data
* @return array
*/
public function edit_product($id, $data)
{
try {
if (!isset($data['product'])) {
throw new WC_API_Exception('woocommerce_api_missing_product_data', sprintf(__('No %1$s data specified to edit %1$s', 'woocommerce'), 'product'), 400);
}
$data = $data['product'];
$id = $this->validate_request($id, 'product', 'edit');
if (is_wp_error($id)) {
return $id;
}
$product = wc_get_product($id);
$data = apply_filters('woocommerce_api_edit_product_data', $data, $this);
// Product title.
if (isset($data['title'])) {
$product->set_name(wc_clean($data['title']));
}
// Product name (slug).
if (isset($data['name'])) {
$product->set_slug(wc_clean($data['name']));
}
// Product status.
if (isset($data['status'])) {
$product->set_status(wc_clean($data['status']));
}
// Product short description.
if (isset($data['short_description'])) {
// Enable short description html tags.
$post_excerpt = isset($data['enable_html_short_description']) && true === $data['enable_html_short_description'] ? $data['short_description'] : wc_clean($data['short_description']);
$product->set_short_description($post_excerpt);
}
// Product description.
if (isset($data['description'])) {
// Enable description html tags.
$post_content = isset($data['enable_html_description']) && true === $data['enable_html_description'] ? $data['description'] : wc_clean($data['description']);
$product->set_description($post_content);
}
// Validate the product type.
if (isset($data['type']) && !in_array(wc_clean($data['type']), array_keys(wc_get_product_types()))) {
throw new WC_API_Exception('woocommerce_api_invalid_product_type', sprintf(__('Invalid product type - the product type must be any of these: %s', 'woocommerce'), implode(', ', array_keys(wc_get_product_types()))), 400);
}
// Check for featured/gallery images, upload it and set it.
if (isset($data['images'])) {
$product = $this->save_product_images($product, $data['images']);
}
// Save product meta fields.
$product = $this->save_product_meta($product, $data);
// Save variations.
if ($product->is_type('variable')) {
if (isset($data['variations']) && is_array($data['variations'])) {
$this->save_variations($product, $data);
} else {
// Just sync variations.
$product = WC_Product_Variable::sync($product, false);
}
}
$product->save();
do_action('woocommerce_api_edit_product', $id, $data);
// Clear cache/transients.
wc_delete_product_transients($id);
return $this->get_product($id);
} catch (WC_Data_Exception $e) {
return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode()));
} catch (WC_API_Exception $e) {
return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode()));
}
}
示例13: updateProduct
//.........这里部分代码省略.........
$option_values = themex_array('option_values', $data);
if (is_array($option_names) && is_array($option_values)) {
$slugs = array();
$options = get_post_meta($ID, '_product_attributes', true);
if (empty($options) || !is_array($options)) {
$options = array();
}
foreach ($option_names as $index => $name) {
if (isset($option_values[$index])) {
$name = sanitize_text_field($name);
$slug = sanitize_title($name);
$value = implode('|', array_map('trim', explode(',', sanitize_text_field($option_values[$index]))));
if (!empty($slug) && !empty($name) && !empty($value)) {
$slugs[] = $slug;
$options[$slug] = array('name' => $name, 'value' => $value, 'position' => '0', 'is_visible' => 0, 'is_variation' => 1, 'is_taxonomy' => 0);
}
}
}
foreach ($options as $slug => $option) {
if (isset($option['is_taxonomy']) && empty($option['is_taxonomy']) && !in_array($slug, $slugs)) {
unset($options[$slug]);
}
}
update_post_meta($ID, '_product_attributes', $options);
}
//variations
$variation_ids = themex_array('variation_ids', $data);
$variation_stocks = themex_array('variation_stocks', $data);
$variation_regular_prices = themex_array('variation_regular_prices', $data);
$variation_sale_prices = themex_array('variation_sale_prices', $data);
if (is_array($variation_ids) && is_array($variation_stocks) && is_array($variation_regular_prices) && is_array($variation_sale_prices)) {
$variations = self::getVariations($ID, array('fields' => 'ids'));
$new_variations = array();
foreach ($variation_ids as $index => $variation_id) {
if (isset($variation_stocks[$index]) && isset($variation_regular_prices[$index]) && isset($variation_sale_prices[$index])) {
$variation_id = intval($variation_id);
$stock = intval($variation_stocks[$index]);
$regular_price = intval($variation_regular_prices[$index]);
$sale_price = $variation_sale_prices[$index];
if (!empty($stock) && (empty($variation_id) || !in_array($variation_id, $variations))) {
$variation_id = wp_insert_post(array('post_type' => 'product_variation', 'post_parent' => $ID, 'post_status' => 'publish', 'post_author' => 1, 'post_title' => sprintf(__('Variation #%s', 'makery'), count($variations) + 1)));
}
if (!empty($variation_id)) {
$new_variations[] = $variation_id;
if (($variation_index = array_search($variation_id, $variations)) !== false) {
unset($variations[$variation_index]);
}
update_post_meta($variation_id, '_manage_stock', 'yes');
update_post_meta($variation_id, '_stock_status', 'instock');
update_post_meta($variation_id, '_stock', $stock);
update_post_meta($variation_id, '_regular_price', $regular_price);
update_post_meta($variation_id, '_price', $regular_price);
if ($sale_price != '') {
update_post_meta($variation_id, '_sale_price', intval($sale_price));
update_post_meta($variation_id, '_price', intval($sale_price));
}
$options = self::getOptions($ID);
foreach ($options as $name => $option) {
$values = themex_array('variation_' . $name . 's', $data);
$items = array_map('trim', explode(',', $option['value']));
if (is_array($values) && isset($values[$index]) && (in_array($values[$index], $items) || $values[$index] == '')) {
update_post_meta($variation_id, 'attribute_' . $name, $values[$index]);
}
}
}
}
}
set_transient('wc_product_children_ids_' . $ID . WC_Cache_Helper::get_transient_version('product'), $new_variations, 84600 * 365);
foreach ($variations as $variation) {
wp_delete_post($variation, true);
}
WC_Product_Variable::sync($ID);
if (empty($new_variations)) {
wp_set_object_terms($ID, 'simple', 'product_type');
} else {
wp_set_object_terms($ID, 'variable', 'product_type');
}
}
//content
$args['post_content'] = trim(themex_value('content', $data));
$args['post_content'] = wp_kses($args['post_content'], array('strong' => array(), 'em' => array(), 'a' => array('href' => array(), 'title' => array(), 'target' => array()), 'p' => array(), 'br' => array()));
if (empty(ThemexInterface::$messages)) {
$status = get_post_status($ID);
if ($status == 'draft') {
$args['post_status'] = 'pending';
if (ThemexCore::checkOption('shop_approve')) {
$args['post_status'] = 'publish';
}
$redirect = true;
}
ThemexInterface::$messages[] = __('Product has been successfully saved', 'makery');
$_POST['success'] = true;
}
//update post
wp_update_post($args);
if ($redirect) {
wp_redirect(ThemexCore::getURL('shop-product', $ID));
exit;
}
}
示例14: woo_insert_update_data
//.........这里部分代码省略.........
// to skip query for blank value
//Code to handle the condition for the attribute visibility on product page issue while save action
if ($object == '_product_attributes' && isset($product_custom_fields['_product_attributes'][0])) {
continue;
}
if (empty($obj->id) || $obj->id == '') {
$query = "INSERT INTO {$wpdb->prefix}postmeta(post_id,meta_key,meta_value) values(" . $wpdb->_real_escape($post_id) . ", '" . $wpdb->_real_escape($object) . "', '" . $wpdb->_real_escape($postarr[$object]) . "')";
$var = $wpdb->query($query);
wp_set_object_terms($post_id, 'simple', 'product_type');
} else {
//$query = "UPDATE {$wpdb->prefix}postmeta SET meta_value = '".$wpdb->_real_escape($postarr[$object])."' WHERE post_id = " . $wpdb->_real_escape($post_id) . " AND meta_key = '" . $wpdb->_real_escape($object) . "'";
update_post_meta($wpdb->_real_escape($post_id), $wpdb->_real_escape($object), $wpdb->_real_escape($postarr[$object]));
if ($object == '_stock') {
if ($_POST['SM_IS_WOO21'] == "true" || $_POST['SM_IS_WOO22'] == "true") {
$woo_version = defined('WOOCOMMERCE_VERSION') ? WOOCOMMERCE_VERSION : $woocommerce->version;
if ($postarr['post_parent'] > 0) {
$woo_prod_obj_stock_status = new WC_Product_Variation($post_id);
} else {
$woo_prod_obj_stock_status = new WC_Product($post_id);
}
if (version_compare($woo_version, '2.4', ">=")) {
$woo_prod_obj_stock_status->check_stock_status();
} else {
$woo_prod_obj_stock_status->set_stock($wpdb->_real_escape($postarr[$object]));
}
}
}
}
}
}
//Code For updating the parent price of the product
if ($parent_id > 0) {
if ($_POST['SM_IS_WOO21'] == "true" || $_POST['SM_IS_WOO22'] == "true") {
WC_Product_Variable::sync($parent_id);
delete_transient('wc_product_children_' . $parent_id);
//added in woo24
} else {
variable_price_sync($parent_id);
}
}
} elseif ($_POST['active_module'] == 'Orders') {
foreach ($obj as $key => $value) {
if (in_array($key, $editable_fields)) {
if ($key == 'order_status') {
// $term_taxonomy_id = get_term_taxonomy_id ( $value );
// $query = "UPDATE {$wpdb->prefix}term_relationships SET term_taxonomy_id = " . $wpdb->_real_escape($term_taxonomy_id) . " WHERE object_id = " . $wpdb->_real_escape($obj->id) . ";";
// if ( $value == 'processing' || $value == 'completed' ) {
$order = new WC_Order($obj->id);
$order->update_status($value);
// }
} else {
$query = "UPDATE {$wpdb->prefix}postmeta SET meta_value = '" . $wpdb->_real_escape($value) . "' WHERE post_id = " . $wpdb->_real_escape($obj->id) . " AND meta_key = '" . $wpdb->_real_escape($key) . "';";
$wpdb->query($query);
}
}
}
} elseif ($_POST['active_module'] == 'Customers') {
//Query to get the email and customer_user for all the selected ids
$query_email = "SELECT DISTINCT(GROUP_CONCAT( meta_value\n ORDER BY meta_id SEPARATOR '###' ) )AS meta_value,\n GROUP_CONCAT(distinct meta_key\n ORDER BY meta_id SEPARATOR '###' ) AS meta_key\n FROM {$wpdb->prefix}postmeta \n WHERE meta_key in ('_billing_email','_customer_user') \n AND post_id={$wpdb->_real_escape}({$obj->id})";
$result_email = $wpdb->get_results($query_email, 'ARRAY_A');
$email = "";
$users = "";
for ($i = 0; $i < sizeof($result_email); $i++) {
$meta_key = explode("###", $result_email[$i]['meta_key']);
$meta_value = explode("###", $result_email[$i]['meta_value']);
$postmeta[$i] = array_combine($meta_key, $meta_value);
示例15: 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);
}