本文整理汇总了PHP中WC_Coupon::get_product_ids方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Coupon::get_product_ids方法的具体用法?PHP WC_Coupon::get_product_ids怎么用?PHP WC_Coupon::get_product_ids使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Coupon
的用法示例。
在下文中一共展示了WC_Coupon::get_product_ids方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: check_get_coupon_response
/**
* Ensure valid coupon data response.
* @since 2.7.0
* @param array $response
* @param WC_Coupon $coupon
*/
protected function check_get_coupon_response($response, $coupon)
{
$this->assertEquals((int) $coupon->get_id(), $response['id']);
$this->assertEquals($coupon->get_code(), $response['code']);
$this->assertEquals($coupon->get_discount_type(), $response['type']);
$this->assertEquals($coupon->get_amount(), $response['amount']);
$this->assertEquals($coupon->get_individual_use(), $response['individual_use']);
$this->assertEquals($coupon->get_product_ids(), $response['product_ids']);
$this->assertEquals($coupon->get_excluded_product_ids(), $response['exclude_product_ids']);
$this->assertEquals((int) $coupon->get_usage_limit(), $response['usage_limit']);
$this->assertEquals((int) $coupon->get_usage_limit_per_user(), $response['usage_limit_per_user']);
$this->assertEquals((int) $coupon->get_limit_usage_to_x_items(), $response['limit_usage_to_x_items']);
$this->assertEquals((int) $coupon->get_usage_count(), $response['usage_count']);
$this->assertEquals($coupon->get_expiry_date(), $response['expiry_date']);
$this->assertEquals($coupon->get_free_shipping(), $response['enable_free_shipping']);
$this->assertEquals($coupon->get_product_categories(), $response['product_category_ids']);
$this->assertEquals($coupon->get_excluded_product_categories(), $response['exclude_product_category_ids']);
$this->assertEquals($coupon->get_exclude_sale_items(), $response['exclude_sale_items']);
$this->assertEquals(wc_format_decimal($coupon->get_minimum_amount(), 2), $response['minimum_amount']);
$this->assertEquals(wc_format_decimal($coupon->get_maximum_amount(), 2), $response['maximum_amount']);
$this->assertEquals($coupon->get_email_restrictions(), $response['customer_emails']);
$this->assertEquals($coupon->get_description(), $response['description']);
$this->assertArrayHasKey('created_at', $response);
$this->assertArrayHasKey('updated_at', $response);
}
示例2: get_coupon
/**
* Get the coupon for the given ID
*
* @since 2.1
* @param int $id the coupon ID
* @param string $fields fields to include in response
* @return array|WP_Error
*/
public function get_coupon($id, $fields = null)
{
$id = $this->validate_request($id, 'shop_coupon', 'read');
if (is_wp_error($id)) {
return $id;
}
$coupon = new WC_Coupon($id);
if (0 === $coupon->get_id()) {
throw new WC_API_Exception('woocommerce_api_invalid_coupon_id', __('Invalid coupon ID', 'woocommerce'), 404);
}
$coupon_data = array('id' => $coupon->get_id(), 'code' => $coupon->get_code(), 'type' => $coupon->get_discount_type(), 'created_at' => $this->server->format_datetime($coupon->get_date_created(), false, true), 'updated_at' => $this->server->format_datetime($coupon->get_date_modified(), false, true), 'amount' => wc_format_decimal($coupon->get_amount(), 2), 'individual_use' => $coupon->get_individual_use(), 'product_ids' => array_map('absint', (array) $coupon->get_product_ids()), 'exclude_product_ids' => array_map('absint', (array) $coupon->get_excluded_product_ids()), 'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null, 'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null, 'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(), 'usage_count' => (int) $coupon->get_usage_count(), 'expiry_date' => $this->server->format_datetime($coupon->get_date_expires(), false, true), 'enable_free_shipping' => $coupon->get_free_shipping(), 'product_category_ids' => array_map('absint', (array) $coupon->get_product_categories()), 'exclude_product_category_ids' => array_map('absint', (array) $coupon->get_excluded_product_categories()), 'exclude_sale_items' => $coupon->get_exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->get_minimum_amount(), 2), 'customer_emails' => $coupon->get_email_restrictions());
return array('coupon' => apply_filters('woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server));
}
示例3: prepare_item_for_response
/**
* Prepare a single coupon output for response.
*
* @param WP_Post $post Post object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $data
*/
public function prepare_item_for_response($post, $request)
{
// Get the coupon code.
$code = wc_get_coupon_code_by_id($post->ID);
$coupon = new WC_Coupon($code);
$data = array('id' => $coupon->get_id(), 'code' => $coupon->get_code(), 'date_created' => wc_rest_prepare_date_response($post->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($post->post_modified_gmt), 'discount_type' => $coupon->get_discount_type(), 'description' => $coupon->get_description(), 'amount' => wc_format_decimal($coupon->get_amount(), 2), 'expiry_date' => $coupon->get_expiry_date() ? wc_rest_prepare_date_response($coupon->get_expiry_date()) : null, 'usage_count' => (int) $coupon->get_usage_count(), 'individual_use' => $coupon->get_individual_use(), 'product_ids' => array_map('absint', (array) $coupon->get_product_ids()), 'exclude_product_ids' => array_map('absint', (array) $coupon->get_excluded_product_ids()), 'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null, 'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null, 'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(), 'free_shipping' => $coupon->get_free_shipping(), 'product_categories' => array_map('absint', (array) $coupon->get_product_categories()), 'excluded_product_categories' => array_map('absint', (array) $coupon->get_excluded_product_categories()), 'exclude_sale_items' => $coupon->get_exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->get_minimum_amount(), 2), 'maximum_amount' => wc_format_decimal($coupon->get_maximum_amount(), 2), 'email_restrictions' => $coupon->get_email_restrictions(), 'used_by' => $coupon->get_used_by());
$context = !empty($request['context']) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object($data, $request);
$data = $this->filter_response_by_context($data, $context);
// Wrap the data in a response object.
$response = rest_ensure_response($data);
$response->add_links($this->prepare_links($post));
/**
* Filter the data for a response.
*
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
* prepared for the response.
*
* @param WP_REST_Response $response The response object.
* @param WP_Post $post Post object.
* @param WP_REST_Request $request Request object.
*/
return apply_filters("woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request);
}
示例4: output
/**
* Output the metabox.
*
* @param WP_Post $post
*/
public static function output($post)
{
wp_nonce_field('woocommerce_save_data', 'woocommerce_meta_nonce');
$coupon = new WC_Coupon($post->ID);
?>
<style type="text/css">
#edit-slug-box, #minor-publishing-actions { display:none }
</style>
<div id="coupon_options" class="panel-wrap coupon_data">
<div class="wc-tabs-back"></div>
<ul class="coupon_data_tabs wc-tabs" style="display:none;">
<?php
$coupon_data_tabs = apply_filters('woocommerce_coupon_data_tabs', array('general' => array('label' => __('General', 'woocommerce'), 'target' => 'general_coupon_data', 'class' => 'general_coupon_data'), 'usage_restriction' => array('label' => __('Usage restriction', 'woocommerce'), 'target' => 'usage_restriction_coupon_data', 'class' => ''), 'usage_limit' => array('label' => __('Usage limits', 'woocommerce'), 'target' => 'usage_limit_coupon_data', 'class' => '')));
foreach ($coupon_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
}
?>
</ul>
<div id="general_coupon_data" class="panel woocommerce_options_panel"><?php
// Type
woocommerce_wp_select(array('id' => 'discount_type', 'label' => __('Discount type', 'woocommerce'), 'options' => wc_get_coupon_types()));
// Amount
woocommerce_wp_text_input(array('id' => 'coupon_amount', 'label' => __('Coupon amount', 'woocommerce'), 'placeholder' => wc_format_localized_price(0), 'description' => __('Value of the coupon.', 'woocommerce'), 'data_type' => 'price', 'desc_tip' => true));
// Free Shipping
woocommerce_wp_checkbox(array('id' => 'free_shipping', 'label' => __('Allow free shipping', 'woocommerce'), 'description' => sprintf(__('Check this box if the coupon grants free shipping. A <a href="%s" target="_blank">free shipping method</a> must be enabled in your shipping zone and be set to require "a valid free shipping coupon" (see the "Free Shipping Requires" setting).', 'woocommerce'), 'https://docs.woocommerce.com/document/free-shipping/')));
// Expiry date
$expiry_date = $coupon->get_date_expires() ? date('Y-m-d', $coupon->get_date_expires()) : '';
woocommerce_wp_text_input(array('id' => 'expiry_date', 'value' => esc_attr($expiry_date), 'label' => __('Coupon expiry date', 'woocommerce'), 'placeholder' => esc_attr__('YYYY-MM-DD', 'woocommerce'), 'description' => '', 'class' => 'date-picker', 'custom_attributes' => array('pattern' => "[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])")));
do_action('woocommerce_coupon_options');
?>
</div>
<div id="usage_restriction_coupon_data" class="panel woocommerce_options_panel"><?php
echo '<div class="options_group">';
// minimum spend
woocommerce_wp_text_input(array('id' => 'minimum_amount', 'label' => __('Minimum spend', 'woocommerce'), 'placeholder' => __('No minimum', 'woocommerce'), 'description' => __('This field allows you to set the minimum spend (subtotal, including taxes) allowed to use the coupon.', 'woocommerce'), 'data_type' => 'price', 'desc_tip' => true));
// maximum spend
woocommerce_wp_text_input(array('id' => 'maximum_amount', 'label' => __('Maximum spend', 'woocommerce'), 'placeholder' => __('No maximum', 'woocommerce'), 'description' => __('This field allows you to set the maximum spend (subtotal, including taxes) allowed when using the coupon.', 'woocommerce'), 'data_type' => 'price', 'desc_tip' => true));
// Individual use
woocommerce_wp_checkbox(array('id' => 'individual_use', 'label' => __('Individual use only', 'woocommerce'), 'description' => __('Check this box if the coupon cannot be used in conjunction with other coupons.', 'woocommerce')));
// Exclude Sale Products
woocommerce_wp_checkbox(array('id' => 'exclude_sale_items', 'label' => __('Exclude sale items', 'woocommerce'), 'description' => __('Check this box if the coupon should not apply to items on sale. Per-item coupons will only work if the item is not on sale. Per-cart coupons will only work if there are no sale items in the cart.', 'woocommerce')));
echo '</div><div class="options_group">';
// Product ids
?>
<p class="form-field"><label><?php
_e('Products', 'woocommerce');
?>
</label>
<input type="hidden" class="wc-product-search" data-multiple="true" style="width: 50%;" name="product_ids" data-placeholder="<?php
esc_attr_e('Search for a product…', 'woocommerce');
?>
" data-action="woocommerce_json_search_products_and_variations" data-selected="<?php
$product_ids = $coupon->get_product_ids();
$json_ids = array();
foreach ($product_ids as $product_id) {
$product = wc_get_product($product_id);
if (is_object($product)) {
$json_ids[$product_id] = wp_kses_post($product->get_formatted_name());
}
}
echo esc_attr(json_encode($json_ids));
?>
" value="<?php
echo implode(',', array_keys($json_ids));
?>
" /> <?php
echo wc_help_tip(__('Products which need to be in the cart to use this coupon or, for "Product Discounts", which products are discounted.', 'woocommerce'));
?>
</p>
<?php
// Exclude Product ids
?>
<p class="form-field"><label><?php
_e('Exclude products', 'woocommerce');
?>
//.........这里部分代码省略.........
示例5: get_coupon
/**
* Get the coupon for the given ID
*
* @since 2.1
* @param int $id the coupon ID
* @param string $fields fields to include in response
* @return array|WP_Error
*/
public function get_coupon($id, $fields = null)
{
global $wpdb;
$id = $this->validate_request($id, 'shop_coupon', 'read');
if (is_wp_error($id)) {
return $id;
}
// get the coupon code
$code = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM {$wpdb->posts} WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $id));
if (is_null($code)) {
return new WP_Error('woocommerce_api_invalid_coupon_id', __('Invalid coupon ID', 'woocommerce'), array('status' => 404));
}
$coupon = new WC_Coupon($code);
$coupon_post = get_post($coupon->get_id());
$coupon_data = array('id' => $coupon->get_id(), 'code' => $coupon->get_code(), 'type' => $coupon->get_discount_type(), 'created_at' => $this->server->format_datetime($coupon_post->post_date_gmt), 'updated_at' => $this->server->format_datetime($coupon_post->post_modified_gmt), 'amount' => wc_format_decimal($coupon->get_amount(), 2), 'individual_use' => $coupon->get_individual_use(), 'product_ids' => array_map('absint', (array) $coupon->get_product_ids()), 'exclude_product_ids' => array_map('absint', (array) $coupon->get_excluded_product_ids()), 'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null, 'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null, 'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(), 'usage_count' => (int) $coupon->get_usage_count(), 'expiry_date' => $this->server->format_datetime($coupon->get_expiry_date()), 'enable_free_shipping' => $coupon->get_free_shipping(), 'product_category_ids' => array_map('absint', (array) $coupon->get_product_categories()), 'exclude_product_category_ids' => array_map('absint', (array) $coupon->get_excluded_product_categories()), 'exclude_sale_items' => $coupon->get_exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->get_minimum_amount(), 2), 'customer_emails' => $coupon->get_email_restrictions());
return array('coupon' => apply_filters('woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server));
}
示例6: format_posts_to_items
/**
* Format posts from WP_Query result to items in which each item contain
* common properties of item, for instance `post_title` will be `code`.
*
* @since 2.5.0
* @param array $posts Array of post
* @return array Items
*/
protected function format_posts_to_items($posts)
{
$items = array();
foreach ($posts as $post) {
$coupon = new WC_Coupon();
$coupon->read($post->ID);
$coupon_usage_limit = $coupon->get_usage_limit();
$coupon_usage_limit_per_user = $coupon->get_usage_limit_per_user();
$coupon_date_expires = $coupon->get_date_expires();
$items[] = array('id' => $post->ID, 'code' => $post->post_title, 'type' => $coupon->get_discount_type(), 'created_at' => $this->format_datetime($post->post_date_gmt), 'updated_at' => $this->format_datetime($post->post_modified_gmt), 'amount' => wc_format_decimal($coupon->get_amount(), 2), 'individual_use' => $coupon->get_individual_use(), 'product_ids' => implode(', ', is_array($coupon->get_product_ids()) ? $coupon->get_product_ids() : array()), 'exclude_product_ids' => implode(', ', is_array($coupon->get_excluded_product_ids()) ? $coupon->get_excluded_product_ids() : array()), 'usage_limit' => !empty($coupon_usage_limit) ? $coupon_usage_limit : null, 'usage_limit_per_user' => !empty($coupon_usage_limit_per_user) ? $coupon_usage_limit_per_user : null, 'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(), 'usage_count' => (int) $coupon->get_usage_count(), 'expiry_date' => !empty($coupon_date_expires) ? $this->format_datetime($coupon_date_expires) : null, 'free_shipping' => $coupon->get_free_shipping(), 'product_category_ids' => implode(', ', is_array($coupon->get_product_categories()) ? $coupon->get_product_categories() : array()), 'exclude_product_category_ids' => implode(', ', is_array($coupon->get_excluded_product_categories()) ? $coupon->get_excluded_product_categories() : array()), 'exclude_sale_items' => $coupon->get_exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->get_minimum_amount(), 2), 'maximum_amount' => wc_format_decimal($coupon->get_maximum_amount(), 2), 'customer_emails' => implode(', ', is_array($coupon->get_email_restrictions()) ? $coupon->get_email_restrictions() : array()), 'description' => $post->post_excerpt);
}
return $items;
}
示例7: get_coupon
/**
* Get the coupon for the given ID
*
* @since 2.1
* @param int $id the coupon ID
* @param string $fields fields to include in response
* @return array|WP_Error
*/
public function get_coupon($id, $fields = null)
{
try {
$id = $this->validate_request($id, 'shop_coupon', 'read');
if (is_wp_error($id)) {
return $id;
}
// get the coupon code
$code = wc_get_coupon_code_by_id($id);
if (empty($code)) {
throw new WC_API_Exception('woocommerce_api_invalid_coupon_id', __('Invalid coupon ID', 'woocommerce'), 404);
}
$coupon = new WC_Coupon($code);
$coupon_post = get_post($coupon->get_id());
$coupon_data = array('id' => $coupon->get_id(), 'code' => $coupon->get_code(), 'type' => $coupon->get_discount_type(), 'created_at' => $this->server->format_datetime($coupon_post->post_date_gmt), 'updated_at' => $this->server->format_datetime($coupon_post->post_modified_gmt), 'amount' => wc_format_decimal($coupon->get_amount(), 2), 'individual_use' => $coupon->get_individual_use(), 'product_ids' => array_map('absint', (array) $coupon->get_product_ids()), 'exclude_product_ids' => array_map('absint', (array) $coupon->get_excluded_product_ids()), 'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null, 'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null, 'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(), 'usage_count' => (int) $coupon->get_usage_count(), 'expiry_date' => $coupon->get_date_expires() ? $this->server->format_datetime($coupon->get_date_expires()) : null, 'enable_free_shipping' => $coupon->get_free_shipping(), 'product_category_ids' => array_map('absint', (array) $coupon->get_product_categories()), 'exclude_product_category_ids' => array_map('absint', (array) $coupon->get_excluded_product_categories()), 'exclude_sale_items' => $coupon->get_exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->get_minimum_amount(), 2), 'maximum_amount' => wc_format_decimal($coupon->get_maximum_amount(), 2), 'customer_emails' => $coupon->get_email_restrictions(), 'description' => $coupon_post->post_excerpt);
return array('coupon' => apply_filters('woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server));
} catch (WC_API_Exception $e) {
return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode()));
}
}
示例8: test_read_manual_coupon
/**
* Developers can create manual coupons (code only). This test will make sure this works correctly
* and some of our backwards compat handling works correctly as well.
* @since 2.7.0
*/
public function test_read_manual_coupon()
{
$code = 'manual_coupon_' . time();
$coupon = new WC_Coupon($code);
$coupon->read_manual_coupon($code, array('id' => true, 'type' => 'fixed_cart', 'amount' => 0, 'individual_use' => true, 'product_ids' => array(), 'exclude_product_ids' => array(), 'usage_limit' => '', 'usage_count' => '', 'expiry_date' => '', 'free_shipping' => false, 'product_categories' => array(), 'exclude_product_categories' => array(), 'exclude_sale_items' => false, 'minimum_amount' => '', 'maximum_amount' => 100, 'customer_email' => ''));
$this->assertEquals($code, $coupon->get_code());
$this->assertEquals(true, $coupon->get_individual_use());
$this->assertEquals(100, $coupon->get_maximum_amount());
/**
* test our back compat logic: passing in product_ids/exclude_product_ids in as strings
* and passing free_shipping, exclude_sale_items, and individual_use in as yes|no strings.
* setting these values this way will also throw a deprecated notice so we will let
* PHPUnit know that its okay to continue.
*/
$legacy_keys = array('product_ids', 'exclude_product_ids', 'individual_use', 'free_shipping', 'exclude_sale_items');
$this->expected_doing_it_wrong = array_merge($this->expected_doing_it_wrong, $legacy_keys);
$code = 'bc_manual_coupon_' . time();
$coupon = new WC_Coupon($code);
$coupon->read_manual_coupon($code, array('id' => true, 'type' => 'fixed_cart', 'amount' => 0, 'individual_use' => 'yes', 'product_ids' => '', 'exclude_product_ids' => '5,6', 'usage_limit' => '', 'usage_count' => '', 'expiry_date' => '', 'free_shipping' => 'no', 'product_categories' => array(), 'exclude_product_categories' => array(), 'exclude_sale_items' => 'no', 'minimum_amount' => '', 'maximum_amount' => 100, 'customer_email' => ''));
$this->assertEquals($code, $coupon->get_code());
$this->assertEquals(true, $coupon->get_individual_use());
$this->assertEquals(false, $coupon->get_free_shipping());
$this->assertEquals(false, $coupon->get_exclude_sale_items());
$this->assertEquals(array(5, 6), $coupon->get_excluded_product_ids());
$this->assertEquals(array(), $coupon->get_product_ids());
}