当前位置: 首页>>代码示例>>PHP>>正文


PHP WC_Coupon::get_error_message方法代码示例

本文整理汇总了PHP中WC_Coupon::get_error_message方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Coupon::get_error_message方法的具体用法?PHP WC_Coupon::get_error_message怎么用?PHP WC_Coupon::get_error_message使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在WC_Coupon的用法示例。


在下文中一共展示了WC_Coupon::get_error_message方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: add_discount

 /**
  * Applies a coupon code passed to the method.
  *
  * @param string $coupon_code - The code to apply
  * @return bool	True if the coupon is applied, false if it does not exist or cannot be applied
  */
 public function add_discount($coupon_code)
 {
     // Coupons are globally disabled
     if (!wc_coupons_enabled()) {
         return false;
     }
     // Sanitize coupon code
     $coupon_code = apply_filters('woocommerce_coupon_code', $coupon_code);
     // Get the coupon
     $the_coupon = new WC_Coupon($coupon_code);
     // Check it can be used with cart
     if (!$the_coupon->is_valid()) {
         wc_add_notice($the_coupon->get_error_message(), 'error');
         return false;
     }
     // Check if applied
     if ($this->has_discount($coupon_code)) {
         $the_coupon->add_coupon_message(WC_Coupon::E_WC_COUPON_ALREADY_APPLIED);
         return false;
     }
     // If its individual use then remove other coupons
     if ($the_coupon->get_individual_use()) {
         $this->applied_coupons = apply_filters('woocommerce_apply_individual_use_coupon', array(), $the_coupon, $this->applied_coupons);
     }
     if ($this->applied_coupons) {
         foreach ($this->applied_coupons as $code) {
             $coupon = new WC_Coupon($code);
             if ($coupon->get_individual_use() && false === apply_filters('woocommerce_apply_with_individual_use_coupon', false, $the_coupon, $coupon, $this->applied_coupons)) {
                 // Reject new coupon
                 $coupon->add_coupon_message(WC_Coupon::E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY);
                 return false;
             }
         }
     }
     $this->applied_coupons[] = $coupon_code;
     // Choose free shipping
     if ($the_coupon->get_free_shipping()) {
         $packages = WC()->shipping->get_packages();
         $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
         foreach ($packages as $i => $package) {
             $chosen_shipping_methods[$i] = 'free_shipping';
         }
         WC()->session->set('chosen_shipping_methods', $chosen_shipping_methods);
     }
     $the_coupon->add_coupon_message(WC_Coupon::WC_COUPON_SUCCESS);
     do_action('woocommerce_applied_coupon', $coupon_code);
     return true;
 }
开发者ID:WPprodigy,项目名称:woocommerce,代码行数:54,代码来源:class-wc-cart.php

示例2: add_discount

 /**
  * Applies a coupon code passed to the method.
  *
  * @param string $coupon_code - The code to apply
  * @return bool	True if the coupon is applied, false if it does not exist or cannot be applied
  */
 public function add_discount($coupon_code)
 {
     global $woocommerce;
     // Coupons are globally disabled
     if (!$woocommerce->cart->coupons_enabled()) {
         return false;
     }
     // Sanitize coupon code
     $coupon_code = apply_filters('woocommerce_coupon_code', $coupon_code);
     // Get the coupon
     $the_coupon = new WC_Coupon($coupon_code);
     if ($the_coupon->id) {
         // Check it can be used with cart
         if (!$the_coupon->is_valid()) {
             $woocommerce->add_error($the_coupon->get_error_message());
             return false;
         }
         // Check if applied
         if ($woocommerce->cart->has_discount($coupon_code)) {
             $the_coupon->add_coupon_message(WC_Coupon::E_WC_COUPON_ALREADY_APPLIED);
             return false;
         }
         // If its individual use then remove other coupons
         if ($the_coupon->individual_use == 'yes') {
             $this->applied_coupons = apply_filters('woocommerce_apply_individual_use_coupon', array(), $the_coupon, $this->applied_coupons);
         }
         if ($this->applied_coupons) {
             foreach ($this->applied_coupons as $code) {
                 $existing_coupon = new WC_Coupon($code);
                 if ($existing_coupon->individual_use == 'yes' && false === apply_filters('woocommerce_apply_with_individual_use_coupon', false, $the_coupon, $existing_coupon, $this->applied_coupons)) {
                     // Reject new coupon
                     $existing_coupon->add_coupon_message(WC_Coupon::E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY);
                     return false;
                 }
             }
         }
         $this->applied_coupons[] = $coupon_code;
         // Choose free shipping
         if ($the_coupon->enable_free_shipping()) {
             $woocommerce->session->chosen_shipping_method = 'free_shipping';
         }
         $this->calculate_totals();
         $the_coupon->add_coupon_message(WC_Coupon::WC_COUPON_SUCCESS);
         do_action('woocommerce_applied_coupon', $coupon_code);
         return true;
     } else {
         $the_coupon->add_coupon_message(WC_Coupon::E_WC_COUPON_NOT_EXIST);
         return false;
     }
     return false;
 }
开发者ID:shahadat014,项目名称:geleyi,代码行数:57,代码来源:class-wc-cart.php


注:本文中的WC_Coupon::get_error_message方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。