本文整理汇总了PHP中WC_Coupon::save方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Coupon::save方法的具体用法?PHP WC_Coupon::save怎么用?PHP WC_Coupon::save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Coupon
的用法示例。
在下文中一共展示了WC_Coupon::save方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1:
/**
* Test coupon saving.
* @since 2.7.0
*/
function test_coupon_save()
{
$coupon = WC_Helper_Coupon::create_coupon();
$coupon_id = $coupon->get_id();
$coupon->set_code('dummycoupon2');
$coupon->save();
$coupon->read($coupon_id);
// Read from DB to retest
$this->assertEquals('dummycoupon2', $coupon->get_code());
$this->assertEquals($coupon_id, $coupon->get_id());
$new_coupon = new WC_Coupon();
$new_coupon->set_code('dummycoupon3');
$new_coupon->save();
$new_coupon_id = $new_coupon->get_id();
$this->assertEquals('dummycoupon3', $new_coupon->get_code());
$this->assertNotEquals(0, $new_coupon_id);
}
示例2: save
/**
* Save meta box data.
*
* @param int $post_id
* @param WP_Post $post
*/
public static function save($post_id, $post)
{
global $wpdb;
// Check for dupe coupons
$coupon_code = apply_filters('woocommerce_coupon_code', $post->post_title);
$id_from_code = wc_get_coupon_id_by_code($coupon_code, $post_id);
if ($id_from_code) {
WC_Admin_Meta_Boxes::add_error(__('Coupon code already exists - customers will use the latest coupon with this code.', 'woocommerce'));
}
$coupon = new WC_Coupon($post_id);
$coupon->set_props(array('code' => $post->post_title, 'discount_type' => wc_clean($_POST['discount_type']), 'amount' => wc_format_decimal($_POST['coupon_amount']), 'date_expires' => wc_clean($_POST['expiry_date']), 'individual_use' => isset($_POST['individual_use']), 'product_ids' => array_filter(array_map('intval', explode(',', $_POST['product_ids']))), 'excluded_product_ids' => array_filter(array_map('intval', explode(',', $_POST['exclude_product_ids']))), 'usage_limit' => absint($_POST['usage_limit']), 'usage_limit_per_user' => absint($_POST['usage_limit_per_user']), 'limit_usage_to_x_items' => absint($_POST['limit_usage_to_x_items']), 'free_shipping' => isset($_POST['free_shipping']), 'product_categories' => array_filter(array_map('intval', (array) $_POST['product_categories'])), 'excluded_product_categories' => array_filter(array_map('intval', (array) $_POST['exclude_product_categories'])), 'exclude_sale_items' => isset($_POST['exclude_sale_items']), 'minimum_amount' => wc_format_decimal($_POST['minimum_amount']), 'maximum_amount' => wc_format_decimal($_POST['maximum_amount']), 'email_restrictions' => array_filter(array_map('trim', explode(',', wc_clean($_POST['customer_email']))))));
$coupon->save();
do_action('woocommerce_coupon_options_save', $post_id);
}
示例3: 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)
{
$coupon = new WC_Coupon($post->post_title);
if (isset($request['amount'])) {
$coupon->set_amount($request['amount']);
}
if (isset($request['individual_use'])) {
$coupon->set_individual_use($request['individual_use']);
}
if (isset($request['product_ids'])) {
$coupon->set_product_ids($request['product_ids']);
}
if (isset($request['exclude_product_ids'])) {
$coupon->set_excluded_product_ids($request['exclude_product_ids']);
}
if (isset($request['usage_limit'])) {
$coupon->set_usage_limit($request['usage_limit']);
}
if (isset($request['usage_limit_per_user'])) {
$coupon->set_usage_limit_per_user($request['usage_limit_per_user']);
}
if (isset($request['limit_usage_to_x_items'])) {
$coupon->set_limit_usage_to_x_items($request['limit_usage_to_x_items']);
}
if (isset($request['usage_count'])) {
$coupon->set_usage_count($request['usage_count']);
}
if (isset($request['expiry_date'])) {
$coupon->set_expiry_date($request['expiry_date']);
}
if (isset($request['free_shipping'])) {
$coupon->set_free_shipping($request['free_shipping']);
}
if (isset($request['product_categories'])) {
$coupon->set_product_categories($request['product_categories']);
}
if (isset($request['excluded_product_categories'])) {
$coupon->set_excluded_product_categories($request['excluded_product_categories']);
}
if (isset($request['exclude_sale_items'])) {
$coupon->set_exclude_sale_items($request['exclude_sale_items']);
}
if (isset($request['minimum_amount'])) {
$coupon->set_minimum_amount($request['minimum_amount']);
}
if (isset($request['maximum_amount'])) {
$coupon->set_maximum_amount($request['maximum_amount']);
}
if (isset($request['email_restrictions'])) {
$coupon->set_email_restrictions($request['email_restrictions']);
}
$coupon->save();
return true;
}