本文整理汇总了PHP中wc_get_product_types函数的典型用法代码示例。如果您正苦于以下问题:PHP wc_get_product_types函数的具体用法?PHP wc_get_product_types怎么用?PHP wc_get_product_types使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wc_get_product_types函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: wc_get_products
/**
* Products wrapper for get_posts.
*
* This function should be used for product retrieval so that we have a data agnostic
* way to get a list of products.
*
* Args:
* status array|string List of statuses to find. Default: any. Options: any, draft, pending, private and publish.
* type array|string Product type, e.g. Default: all. Options: all, simple, external, variable, variation, grouped.
* parent int post/product parent
* sku string Limit result set to products with specific SKU.
* category array Limit result set to products assigned to specific categories by slug
* e.g. array('hoodie', 'cap', 't-shirt').
* tag array Limit result set to products assigned to specific tags (by slug)
* e.g. array('funky', 'retro', 'designer')
* shipping_class array Limit results set to products in specific shipping classes (by slug)
* e.g. array('standard', 'next-day')
* limit int Maximum of products to retrieve.
* offset int Offset of products to retrieve.
* page int Page of products to retrieve. Ignored when using the 'offset' arg.
* exclude array Product IDs to exclude from the query.
* orderby string Order by date, title, id, modified, rand etc
* order string ASC or DESC
* return string Type of data to return. Allowed values:
* ids array of Product ids
* objects array of product objects (default)
* paginate bool If true, the return value will be an array with values:
* 'products' => array of data (return value above),
* 'total' => total number of products matching the query
* 'max_num_pages' => max number of pages found
*
* @since 2.7.0
* @param array $args Array of args (above)
* @return array|stdClass Number of pages and an array of product objects if
* paginate is true, or just an array of values.
*/
function wc_get_products($args)
{
$args = wp_parse_args($args, array('status' => array('draft', 'pending', 'private', 'publish'), 'type' => array_merge(array_keys(wc_get_product_types())), 'parent' => null, 'sku' => '', 'category' => array(), 'tag' => array(), 'limit' => get_option('posts_per_page'), 'offset' => null, 'page' => 1, 'exclude' => array(), 'orderby' => 'date', 'order' => 'DESC', 'return' => 'objects', 'paginate' => false, 'shipping_class' => array()));
// Handle some BW compatibility arg names where wp_query args differ in naming.
$map_legacy = array('numberposts' => 'limit', 'post_status' => 'status', 'post_parent' => 'parent', 'posts_per_page' => 'limit', 'paged' => 'page');
foreach ($map_legacy as $from => $to) {
if (isset($args[$from])) {
$args[$to] = $args[$from];
}
}
return WC_Data_Store::load('product')->get_products($args);
}
示例2: 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;
}
$data = apply_filters('woocommerce_api_edit_product_data', $data, $this);
// Product title.
if (isset($data['title'])) {
wp_update_post(array('ID' => $id, 'post_title' => wc_clean($data['title'])));
}
// Product name (slug).
if (isset($data['name'])) {
wp_update_post(array('ID' => $id, 'post_name' => sanitize_title($data['name'])));
}
// Product status.
if (isset($data['status'])) {
wp_update_post(array('ID' => $id, 'post_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']);
wp_update_post(array('ID' => $id, 'post_excerpt' => $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']);
wp_update_post(array('ID' => $id, 'post_content' => $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'])) {
$this->save_product_images($id, $data['images']);
}
// Save product meta fields
$this->save_product_meta($id, $data);
// Save variations
$product = get_product($id);
if ($product->is_type('variable')) {
if (isset($data['variations']) && is_array($data['variations'])) {
$this->save_variations($id, $data);
} else {
// Just sync variations
WC_Product_Variable::sync($id);
}
}
do_action('woocommerce_api_edit_product', $id, $data);
// Clear cache/transients
wc_delete_product_transients($id);
return $this->get_product($id);
} catch (WC_API_Exception $e) {
return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode()));
}
}
示例3: woocommerce_process_product_meta_coupons
/**
* Function to save coupon code to database
*
* @param int $post_id
* @param object $post
*/
public function woocommerce_process_product_meta_coupons($post_id, $post)
{
if (empty($post_id) || empty($post) || empty($_POST)) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (is_int(wp_is_post_revision($post))) {
return;
}
if (is_int(wp_is_post_autosave($post))) {
return;
}
if (empty($_POST['woocommerce_meta_nonce']) || !wp_verify_nonce($_POST['woocommerce_meta_nonce'], 'woocommerce_save_data')) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
if ($post->post_type != 'product') {
return;
}
$valid_product_types = function_exists('wc_get_product_types') ? array_keys(wc_get_product_types()) : array('simple', 'variable', 'subscription', 'variable-subscription');
if (!empty($_POST['product-type']) && in_array($_POST['product-type'], $valid_product_types)) {
if (!empty($_POST['_coupon_title'])) {
if ($this->is_wc_gte_23()) {
$coupon_titles = array_filter(array_map('trim', explode(',', $_POST['_coupon_title'])));
} else {
$coupon_titles = $_POST['_coupon_title'];
}
update_post_meta($post_id, '_coupon_title', $coupon_titles);
} else {
update_post_meta($post_id, '_coupon_title', array());
}
}
if ($_POST['product-type'] == 'subscription' || $_POST['product-type'] == 'variable-subscription') {
if (isset($_POST['send_coupons_on_renewals'])) {
update_post_meta($post_id, 'send_coupons_on_renewals', $_POST['send_coupons_on_renewals']);
} else {
update_post_meta($post_id, 'send_coupons_on_renewals', 'no');
}
}
}
示例4: output
/**
* Output the metabox.
*
* @param WP_Post $post
*/
public static function output($post)
{
global $post, $thepostid;
wp_nonce_field('woocommerce_save_data', 'woocommerce_meta_nonce');
$thepostid = $post->ID;
if ($terms = wp_get_object_terms($post->ID, 'product_type')) {
$product_type = sanitize_title(current($terms)->name);
} else {
$product_type = apply_filters('default_product_type', 'simple');
}
$type_box = '<label for="product-type"><select id="product-type" name="product-type"><optgroup label="' . esc_attr__('Product Type', 'woocommerce') . '">';
foreach (wc_get_product_types() as $value => $label) {
$type_box .= '<option value="' . esc_attr($value) . '" ' . selected($product_type, $value, false) . '>' . esc_html($label) . '</option>';
}
$type_box .= '</optgroup></select></label>';
$product_type_options = apply_filters('product_type_options', array('virtual' => array('id' => '_virtual', 'wrapper_class' => 'show_if_simple', 'label' => __('Virtual', 'woocommerce'), 'description' => __('Virtual products are intangible and aren\'t shipped.', 'woocommerce'), 'default' => 'no'), 'downloadable' => array('id' => '_downloadable', 'wrapper_class' => 'show_if_simple', 'label' => __('Downloadable', 'woocommerce'), 'description' => __('Downloadable products give access to a file upon purchase.', 'woocommerce'), 'default' => 'no')));
foreach ($product_type_options as $key => $option) {
$selected_value = get_post_meta($post->ID, '_' . $key, true);
if ('' == $selected_value && isset($option['default'])) {
$selected_value = $option['default'];
}
$type_box .= '<label for="' . esc_attr($option['id']) . '" class="' . esc_attr($option['wrapper_class']) . ' tips" data-tip="' . esc_attr($option['description']) . '">' . esc_html($option['label']) . ': <input type="checkbox" name="' . esc_attr($option['id']) . '" id="' . esc_attr($option['id']) . '" ' . checked($selected_value, 'yes', false) . ' /></label>';
}
?>
<div class="panel-wrap product_data">
<span class="type_box hidden"> — <?php
echo $type_box;
?>
</span>
<ul class="product_data_tabs wc-tabs">
<?php
$product_data_tabs = apply_filters('woocommerce_product_data_tabs', array('general' => array('label' => __('General', 'woocommerce'), 'target' => 'general_product_data', 'class' => array('hide_if_grouped')), 'inventory' => array('label' => __('Inventory', 'woocommerce'), 'target' => 'inventory_product_data', 'class' => array('show_if_simple', 'show_if_variable', 'show_if_grouped', 'show_if_external')), 'shipping' => array('label' => __('Shipping', 'woocommerce'), 'target' => 'shipping_product_data', 'class' => array('hide_if_virtual', 'hide_if_grouped', 'hide_if_external')), 'linked_product' => array('label' => __('Linked Products', 'woocommerce'), 'target' => 'linked_product_data', 'class' => array()), 'attribute' => array('label' => __('Attributes', 'woocommerce'), 'target' => 'product_attributes', 'class' => array()), 'variations' => array('label' => __('Variations', 'woocommerce'), 'target' => 'variable_product_options', 'class' => array('variations_tab', 'show_if_variable')), 'advanced' => array('label' => __('Advanced', 'woocommerce'), 'target' => 'advanced_product_data', 'class' => array())));
foreach ($product_data_tabs as $key => $tab) {
?>
<li class="<?php
echo $key;
?>
_options <?php
echo $key;
?>
_tab <?php
echo implode(' ', (array) $tab['class']);
?>
">
<a href="#<?php
echo $tab['target'];
?>
"><?php
echo esc_html($tab['label']);
?>
</a>
</li><?php
}
do_action('woocommerce_product_write_panel_tabs');
?>
</ul>
<div id="general_product_data" class="panel woocommerce_options_panel"><?php
echo '<div class="options_group show_if_external">';
// External URL
woocommerce_wp_text_input(array('id' => '_product_url', 'label' => __('Product URL', 'woocommerce'), 'placeholder' => 'http://', 'description' => __('Enter the external URL to the product.', 'woocommerce')));
// Button text
woocommerce_wp_text_input(array('id' => '_button_text', 'label' => __('Button text', 'woocommerce'), 'placeholder' => _x('Buy product', 'placeholder', 'woocommerce'), 'description' => __('This text will be shown on the button linking to the external product.', 'woocommerce')));
echo '</div>';
echo '<div class="options_group pricing show_if_simple show_if_external hidden">';
// Price
woocommerce_wp_text_input(array('id' => '_regular_price', 'label' => __('Regular price', 'woocommerce') . ' (' . get_woocommerce_currency_symbol() . ')', 'data_type' => 'price'));
// Special Price
woocommerce_wp_text_input(array('id' => '_sale_price', 'data_type' => 'price', 'label' => __('Sale price', 'woocommerce') . ' (' . get_woocommerce_currency_symbol() . ')', 'description' => '<a href="#" class="sale_schedule">' . __('Schedule', 'woocommerce') . '</a>'));
// Special Price date range
$sale_price_dates_from = ($date = get_post_meta($thepostid, '_sale_price_dates_from', true)) ? date_i18n('Y-m-d', $date) : '';
$sale_price_dates_to = ($date = get_post_meta($thepostid, '_sale_price_dates_to', true)) ? date_i18n('Y-m-d', $date) : '';
echo '<p class="form-field sale_price_dates_fields">
<label for="_sale_price_dates_from">' . __('Sale price dates', 'woocommerce') . '</label>
<input type="text" class="short" name="_sale_price_dates_from" id="_sale_price_dates_from" value="' . esc_attr($sale_price_dates_from) . '" placeholder="' . _x('From…', 'placeholder', 'woocommerce') . ' YYYY-MM-DD" maxlength="10" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" />
<input type="text" class="short" name="_sale_price_dates_to" id="_sale_price_dates_to" value="' . esc_attr($sale_price_dates_to) . '" placeholder="' . _x('To…', 'placeholder', 'woocommerce') . ' YYYY-MM-DD" maxlength="10" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" />
<a href="#" class="cancel_sale_schedule">' . __('Cancel', 'woocommerce') . '</a>' . wc_help_tip(__('The sale will end at the beginning of the set date.', 'woocommerce')) . '
</p>';
do_action('woocommerce_product_options_pricing');
echo '</div>';
echo '<div class="options_group show_if_downloadable hidden">';
?>
<div class="form-field downloadable_files">
<label><?php
_e('Downloadable files', 'woocommerce');
?>
</label>
<table class="widefat">
<thead>
<tr>
<th class="sort"> </th>
<th><?php
_e('Name', 'woocommerce');
?>
//.........这里部分代码省略.........
示例5: update
/**
* Update one or more products.
*
* ## OPTIONS
*
* <id>
* : Product ID
*
* --<field>=<value>
* : One or more fields to update.
*
* ## AVAILABLE_FIELDS
*
* For more fields, see: wp wc product create --help
*
* ## EXAMPLES
*
* wp wc product update 123 --title="New Product Title" --description="New description"
*
* @since 2.5.0
*/
public function update($args, $assoc_args)
{
try {
$id = $args[0];
$data = apply_filters('woocommerce_cli_update_product_data', $this->unflatten_array($assoc_args));
// Product title.
if (isset($data['title'])) {
wp_update_post(array('ID' => $id, 'post_title' => wc_clean($data['title'])));
}
// Product name (slug).
if (isset($data['name'])) {
wp_update_post(array('ID' => $id, 'post_name' => sanitize_title($data['name'])));
}
// Product status.
if (isset($data['status'])) {
wp_update_post(array('ID' => $id, 'post_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']);
wp_update_post(array('ID' => $id, 'post_excerpt' => $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']);
wp_update_post(array('ID' => $id, 'post_content' => $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_CLI_Exception('woocommerce_cli_invalid_product_type', sprintf(__('Invalid product type - the product type must be any of these: %s', 'woocommerce'), implode(', ', array_keys(wc_get_product_types()))));
}
// Check for featured/gallery images, upload it and set it
if (isset($data['images'])) {
$this->save_product_images($id, $data['images']);
}
// Save product meta fields
$this->save_product_meta($id, $data);
// Save variations
if (isset($data['type']) && 'variable' == $data['type'] && isset($data['variations']) && is_array($data['variations'])) {
$this->save_variations($id, $data);
}
do_action('woocommerce_cli_update_product', $id, $data);
// Clear cache/transients
wc_delete_product_transients($id);
WP_CLI::success("Updated product {$id}.");
} catch (WC_CLI_Exception $e) {
WP_CLI::error($e->getMessage());
}
}
示例6: get_collection_params
/**
* Get the query params for collections of attachments.
*
* @return array
*/
public function get_collection_params()
{
$params = parent::get_collection_params();
$params['slug'] = array('description' => __('Limit result set to products with a specific slug.', 'woocommerce'), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg');
$params['status'] = array('default' => 'any', 'description' => __('Limit result set to products assigned a specific status.', 'woocommerce'), 'type' => 'string', 'enum' => array_merge(array('any'), array_keys(get_post_statuses())), 'sanitize_callback' => 'sanitize_key', 'validate_callback' => 'rest_validate_request_arg');
$params['type'] = array('description' => __('Limit result set to products assigned a specific type.', 'woocommerce'), 'type' => 'string', 'enum' => array_keys(wc_get_product_types()), 'sanitize_callback' => 'sanitize_key', 'validate_callback' => 'rest_validate_request_arg');
$params['sku'] = array('description' => __('Limit result set to products with a specific SKU.', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg');
$params['featured'] = array('description' => __('Limit result set to featured products.', 'woocommerce'), 'type' => 'boolean', 'sanitize_callback' => 'wc_string_to_bool', 'validate_callback' => 'rest_validate_request_arg');
$params['category'] = array('description' => __('Limit result set to products assigned a specific category ID.', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'wp_parse_id_list', 'validate_callback' => 'rest_validate_request_arg');
$params['tag'] = array('description' => __('Limit result set to products assigned a specific tag ID.', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'wp_parse_id_list', 'validate_callback' => 'rest_validate_request_arg');
$params['shipping_class'] = array('description' => __('Limit result set to products assigned a specific shipping class ID.', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'wp_parse_id_list', 'validate_callback' => 'rest_validate_request_arg');
$params['attribute'] = array('description' => __('Limit result set to products with a specific attribute.', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg');
$params['attribute_term'] = array('description' => __('Limit result set to products with a specific attribute term ID (required an assigned attribute).', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'wp_parse_id_list', 'validate_callback' => 'rest_validate_request_arg');
if (wc_tax_enabled()) {
$params['tax_class'] = array('description' => __('Limit result set to products with a specific tax class.', 'woocommerce'), 'type' => 'string', 'enum' => array_map('sanitize_title', array_merge(array('standard'), WC_Tax::get_tax_classes())), 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg');
}
$params['in_stock'] = array('description' => __('Limit result set to products in stock or out of stock.', 'woocommerce'), 'type' => 'boolean', 'sanitize_callback' => 'wc_string_to_bool', 'validate_callback' => 'rest_validate_request_arg');
$params['on_sale'] = array('description' => __('Limit result set to products on sale.', 'woocommerce'), 'type' => 'boolean', 'sanitize_callback' => 'wc_string_to_bool', 'validate_callback' => 'rest_validate_request_arg');
$params['min_price'] = array('description' => __('Limit result set to products based on a minimum price.', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg');
$params['max_price'] = array('description' => __('Limit result set to products based on a maximum price.', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg');
return $params;
}
示例7: esc_attr_e
<div class="panel-wrap product_data">
<span class="type_box hidden"> —
<label for="product-type">
<select id="product-type" name="product-type">
<optgroup label="<?php
esc_attr_e('Product Type', 'woocommerce');
?>
">
<?php
foreach (wc_get_product_types() as $value => $label) {
?>
<option value="<?php
echo esc_attr($value);
?>
" <?php
echo selected($product_object->get_type(), $value, false);
?>
><?php
echo esc_html($label);
?>
</option>
<?php
}
?>
</optgroup>
</select>
</label>
<?php
foreach (self::get_product_type_options() as $key => $option) {
示例8: get_collection_params
/**
* Get the query params for collections of attachments.
*
* @return array
*/
public function get_collection_params()
{
$params = parent::get_collection_params();
$params['slug'] = array('description' => __('Limit result set to products with a specific slug.', 'woocommerce', 'woocommerce'), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg');
$params['status'] = array('default' => 'any', 'description' => __('Limit result set to products assigned a specific status.', 'woocommerce'), 'type' => 'string', 'enum' => array_merge(array('any'), array_keys(get_post_statuses())), 'sanitize_callback' => 'sanitize_key', 'validate_callback' => 'rest_validate_request_arg');
$params['type'] = array('description' => __('Limit result set to products assigned a specific type.', 'woocommerce'), 'type' => 'string', 'enum' => array_keys(wc_get_product_types()), 'sanitize_callback' => 'sanitize_key', 'validate_callback' => 'rest_validate_request_arg');
$params['category'] = array('description' => __('Limit result set to products assigned a specific category.', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg');
$params['tag'] = array('description' => __('Limit result set to products assigned a specific tag.', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg');
$params['shipping_class'] = array('description' => __('Limit result set to products assigned a specific shipping class.', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg');
$params['attribute'] = array('description' => __('Limit result set to products with a specific attribute.', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg');
$params['attribute_term'] = array('description' => __('Limit result set to products with a specific attribute term (required an assigned attribute).', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg');
$params['sku'] = array('description' => __('Limit result set to products with a specific SKU.', 'woocommerce'), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg');
return $params;
}
示例9: test_wc_get_product_types
/**
* Test wc_get_product_types()
*
* @since 2.3
*/
public function test_wc_get_product_types()
{
$product_types = (array) apply_filters('product_type_selector', array('simple' => __('Simple product', 'woocommerce'), 'grouped' => __('Grouped product', 'woocommerce'), 'external' => __('External/Affiliate product', 'woocommerce'), 'variable' => __('Variable product', 'woocommerce')));
$this->assertEquals($product_types, wc_get_product_types());
}
示例10: 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)
{
$data = isset($data['product']) ? $data['product'] : array();
$id = $this->validate_request($id, 'product', 'edit');
if (is_wp_error($id)) {
return $id;
}
$data = apply_filters('woocommerce_api_edit_product_data', $data, $this);
// Product name.
if (isset($data['title'])) {
wp_update_post(array('ID' => $id, 'post_title' => wc_clean($data['title'])));
}
// Product status.
if (isset($data['status'])) {
wp_update_post(array('ID' => $id, 'post_status' => wc_clean($data['status'])));
}
// Product short description.
if (isset($data['short_description'])) {
wp_update_post(array('ID' => $id, 'post_excerpt' => wc_clean($data['short_description'])));
}
// Product description.
if (isset($data['description'])) {
wp_update_post(array('ID' => $id, 'post_content' => wc_clean($data['description'])));
}
// Validate the product type
if (isset($data['type']) && !in_array(wc_clean($data['type']), array_keys(wc_get_product_types()))) {
return new WP_Error('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()))), array('status' => 400));
}
// Check for featured/gallery images, upload it and set it
if (isset($data['images'])) {
$images = $this->save_product_images($id, $data['images']);
if (is_wp_error($images)) {
return $images;
}
}
// Save product meta fields
$meta = $this->save_product_meta($id, $data);
if (is_wp_error($meta)) {
return $meta;
}
// Save variations
if (isset($data['type']) && 'variable' == $data['type'] && isset($data['variations']) && is_array($data['variations'])) {
$variations = $this->save_variations($id, $data);
if (is_wp_error($variations)) {
return $variations;
}
}
do_action('woocommerce_api_edit_product', $id, $data);
// Clear cache/transients
wc_delete_product_transients($id);
return $this->get_product($id);
}
示例11: add_settings
function add_settings()
{
$product_cats = array();
$product_categories = get_terms('product_cat', 'orderby=name&hide_empty=0');
foreach ($product_categories as $product_category) {
$product_cats[$product_category->term_id] = $product_category->name;
}
$products = wcj_get_products();
$settings = array(array('title' => __('Custom Price Labels - Globally', 'woocommerce-jetpack'), 'type' => 'title', 'desc' => __('This section lets you set price labels for all products globally.', 'woocommerce-jetpack'), 'id' => 'wcj_global_price_labels_options'), array('title' => __('Add before the price', 'woocommerce-jetpack'), 'desc_tip' => __('Enter text to add before all products prices. Leave blank to disable.', 'woocommerce-jetpack'), 'id' => 'wcj_global_price_labels_add_before_text', 'default' => '', 'type' => 'textarea', 'desc' => apply_filters('booster_get_message', '', 'desc'), 'custom_attributes' => apply_filters('booster_get_message', '', 'readonly'), 'css' => 'width:30%;min-width:300px;'), array('title' => __('Add after the price', 'woocommerce-jetpack'), 'desc_tip' => __('Enter text to add after all products prices. Leave blank to disable.', 'woocommerce-jetpack'), 'id' => 'wcj_global_price_labels_add_after_text', 'default' => '', 'type' => 'textarea', 'css' => 'width:30%;min-width:300px;'), array('title' => __('Add between regular and sale prices', 'woocommerce-jetpack'), 'desc_tip' => __('Enter text to add between regular and sale prices. Leave blank to disable.', 'woocommerce-jetpack'), 'id' => 'wcj_global_price_labels_between_regular_and_sale_text', 'default' => '', 'type' => 'textarea', 'desc' => apply_filters('booster_get_message', '', 'desc'), 'custom_attributes' => apply_filters('booster_get_message', '', 'readonly'), 'css' => 'width:30%;min-width:300px;'), array('title' => __('Remove from price', 'woocommerce-jetpack'), 'desc_tip' => __('Enter text to remove from all products prices. Leave blank to disable.', 'woocommerce-jetpack'), 'id' => 'wcj_global_price_labels_remove_text', 'default' => '', 'type' => 'textarea', 'desc' => apply_filters('booster_get_message', '', 'desc'), 'custom_attributes' => apply_filters('booster_get_message', '', 'readonly'), 'css' => 'width:30%;min-width:300px;'), array('title' => __('Replace in price', 'woocommerce-jetpack'), 'desc_tip' => __('Enter text to replace in all products prices. Leave blank to disable.', 'woocommerce-jetpack'), 'id' => 'wcj_global_price_labels_replace_text', 'default' => '', 'type' => 'textarea', 'desc' => apply_filters('booster_get_message', '', 'desc'), 'custom_attributes' => apply_filters('booster_get_message', '', 'readonly'), 'css' => 'width:30%;min-width:300px;'), array('title' => '', 'desc_tip' => __('Enter text to replace with. Leave blank to disable.', 'woocommerce-jetpack'), 'id' => 'wcj_global_price_labels_replace_with_text', 'default' => '', 'type' => 'textarea', 'desc' => apply_filters('booster_get_message', '', 'desc'), 'custom_attributes' => apply_filters('booster_get_message', '', 'readonly'), 'css' => 'width:30%;min-width:300px;'), array('title' => __('Products - Include', 'woocommerce-jetpack'), 'desc_tip' => __('Apply global price labels only for selected products. Leave blank to disable the option.', 'woocommerce-jetpack'), 'id' => 'wcj_global_price_labels_products_incl', 'default' => '', 'type' => 'multiselect', 'class' => 'chosen_select', 'css' => 'width: 450px;', 'options' => $products), array('title' => __('Products - Exclude', 'woocommerce-jetpack'), 'desc_tip' => __('Do not apply global price labels only for selected products. Leave blank to disable the option.', 'woocommerce-jetpack'), 'id' => 'wcj_global_price_labels_products_excl', 'default' => '', 'type' => 'multiselect', 'class' => 'chosen_select', 'css' => 'width: 450px;', 'options' => $products), array('title' => __('Product Categories - Include', 'woocommerce-jetpack'), 'desc_tip' => __('Apply global price labels only for selected product categories. Leave blank to disable the option.', 'woocommerce-jetpack'), 'id' => 'wcj_global_price_labels_product_cats_incl', 'default' => '', 'type' => 'multiselect', 'class' => 'chosen_select', 'css' => 'width: 450px;', 'options' => $product_cats), array('title' => __('Product Categories - Exclude', 'woocommerce-jetpack'), 'desc_tip' => __('Do not apply global price labels only for selected product categories. Leave blank to disable the option.', 'woocommerce-jetpack'), 'id' => 'wcj_global_price_labels_product_cats_excl', 'default' => '', 'type' => 'multiselect', 'class' => 'chosen_select', 'css' => 'width: 450px;', 'options' => $product_cats), array('title' => __('Product Types - Include', 'woocommerce-jetpack'), 'desc_tip' => __('Apply global price labels only for selected product types. Leave blank to disable the option.', 'woocommerce-jetpack'), 'id' => 'wcj_global_price_labels_product_types_incl', 'default' => '', 'type' => 'multiselect', 'class' => 'chosen_select', 'css' => 'width: 450px;', 'options' => array_merge(wc_get_product_types(), array('variation' => __('Variable product\'s variation', 'woocommerce-jetpack')))), array('type' => 'sectionend', 'id' => 'wcj_global_price_labels_options'), array('title' => __('Custom Price Labels - Per Product', 'woocommerce-jetpack'), 'type' => 'title', 'id' => 'wcj_local_price_labels_options'), array('title' => __('Enable', 'woocommerce-jetpack'), 'desc' => __('This will add metaboxes to each product\'s admin edit page.', 'woocommerce-jetpack'), 'id' => 'wcj_local_price_labels_enabled', 'default' => 'yes', 'type' => 'checkbox'), array('type' => 'sectionend', 'id' => 'wcj_local_price_labels_options'));
return $settings;
}
示例12: add_product
/**
* Add a new product
*
* @since 2.2
* @param array $data posted data
* @return array
*/
public function add_product($data)
{
// Check permisions
if (!current_user_can('publish_products')) {
return new WP_Error('woocommerce_api_user_cannot_create_product', __('You do not have permission to create products', 'woocommerce'), array('status' => 401));
}
// Check if product title is specified
if (!isset($data['title'])) {
return new WP_Error('woocommerce_api_missing_product_title', sprintf(__('Missing parameter %s'), 'title'), array('status' => 400));
}
// Check product type
if (!isset($data['type'])) {
$data['type'] = 'simple';
} else {
if ($data['type'] == 'booking') {
} elseif ($data['type'] == 'virtual') {
} elseif (!in_array(wc_clean($data['type']), array_keys(wc_get_product_types()))) {
return new WP_Error('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()))), array('status' => 400));
}
}
$new_product = array('post_title' => $data['title'], 'post_status' => isset($data['status']) ? wc_clean($data['status']) : 'publish', 'post_type' => 'product', 'post_excerpt' => isset($data['short_description']) ? wc_clean($data['short_description']) : '', 'post_content' => isset($data['description']) ? wc_clean($data['description']) : '', 'post_author' => get_current_user_id());
$product_id = wp_insert_post($new_product, true);
if (is_wp_error($product_id)) {
return new WP_Error('woocommerce_api_cannot_create_product', $product_id->get_error_message(), array('status' => 400));
}
if (isset($data['sku'])) {
update_post_meta($product_id, '_sku', $data['sku'], true);
}
if (isset($data['type'])) {
wp_set_object_terms($product_id, $data['type'], 'product_type');
}
if (isset($data['regular_price'])) {
# code...
update_post_meta($product_id, '_regular_price', $data['regular_price'], true);
update_post_meta($product_id, '_price', $data['regular_price'], true);
}
if (isset($data['managing_stock'])) {
# code...
update_post_meta($product_id, '_manage_stock', 'yes');
}
if (isset($data['stock'])) {
# code...
update_post_meta($product_id, '_stock', $data['stock']);
}
if (isset($data['visibility'])) {
# code...
update_post_meta($product_id, '_visibility', $data['visibility']);
}
// Check for featured/gallery images, upload it and set it
if (isset($data['images'])) {
$gallery_ids = array();
foreach ($data['images'] as $product_image) {
if (isset($product_image['position']) && isset($product_image['src']) && $product_image['position'] == 0) {
$upload = $this->upload_product_image(wc_clean($product_image['src']));
if (is_wp_error($upload)) {
return new WP_Error('woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), array('status' => 400));
}
$attachment_id = $this->set_product_image_as_attachment($upload, $product_id);
set_post_thumbnail($product_id, $attachment_id);
} else {
if (isset($product_image['src'])) {
/*
$upload = $this->upload_product_image( wc_clean( $product_image['src'] ) );
//generate metadata
//wp_generate_attachment_metadata( $attachmentid, $upload['file'] );
if ( is_wp_error( $upload ) ) {
return new WP_Error( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), array( 'status' => 400 ) );
}
$attachment_id = $this->set_product_image_as_attachment( $upload, $product_id );
$upload = $this->upload_product_image( wc_clean( $product_image['src'] ) );
*/
// Upload an Image
$image = media_sideload_image($product_image['src'], $product_id);
// Remove any unwanted HTML, keep just a plain URL (or whatever is in the image src="..." )
$image = preg_replace('/.*(?<=src=["\'])([^"\']*)(?=["\']).*/', '$1', $image);
// Get the Attachment ID
$attachment_id = $this->get_attachment_id_from_src($image);
set_post_thumbnail($product_id, $attachment_id);
}
}
}
}
return $product_id;
}