Check if a coupon is valid for a product.
public is_valid_for_product ( WC_Product $product, $values = [] ) : boolean | ||
$product | WC_Product | |
return | boolean |
public function ins_ajax_get_order_item() { global $woocommerce; //get post variables $cart_id = $_POST['cart_id']; //initialize total discount $total_discount = 0; //get cart item detail $cart_item = $woocommerce->cart->cart_contents[$cart_id]; //get line item product data $product = get_product($cart_item['product_id']); //set data for display $this->json_return = array('success' => true, 'call' => 'show_dialog', 'dialog_method' => 'get_line_item', 'html' => $this->ins_get_template('instore-edit-item-display-html'), 'item' => array('cart_id' => $cart_id, 'product_id' => $cart_item['product_id'], 'title' => $product->get_title(), 'sku' => $product->get_sku(), 'description' => $cart_item['data']->post->post_excerpt, 'regular_price' => $product->get_regular_price(), 'on_sale' => $product->is_on_sale() ? 'Yes' : 'No', 'sale_price' => $product->is_on_sale() ? $product->get_sale_price() : 'N/A', 'price' => $product->get_price(), 'quantity' => $cart_item['quantity'], 'subtotal' => $woocommerce->cart->get_product_subtotal($product, $cart_item['quantity'])), 'options' => array('modal' => true, 'autoOpen' => true, 'width' => 800, 'title' => 'Edit Order Item')); if ($woocommerce->cart->coupons_enabled() && ($coupons = $woocommerce->cart->applied_coupons)) { foreach ($coupons as $key => $coupons) { $coupon = new WC_Coupon($coupon); if ($coupon->is_valid_for_product($product)) { $applied_coupons[$coupon->code] = array('amount' => $coupon->amount, 'type' => $coupon->type, 'total' => get_discounted_amount($product->get_price(), $cart_item, $coupon->individual_use)); } } } if (isset($applied_coupons)) { $this->json_return['html'] = $this->ins_get_template('instore-discount-item-display-html'); $this->json_return['applied_coupons'] = $applied_coupons; } echo json_encode($this->json_return); die; }
/** * Apply sign up fee or recurring fee discount * * @since 1.2 */ public static function apply_subscription_discount($original_price, $cart_item, $cart) { _deprecated_function(__METHOD__, '2.0.10', 'Have moved to filtering on "woocommerce_coupon_get_discount_amount" to return discount amount. See: ' . __CLASS__ . '::get_discount_amount()'); $product_id = $cart_item['data']->is_type(array('subscription_variation')) ? $cart_item['data']->variation_id : $cart_item['data']->id; if (!WC_Subscriptions_Product::is_subscription($product_id)) { return $original_price; } $price = $calculation_price = $original_price; $calculation_type = WC_Subscriptions_Cart::get_calculation_type(); if (!empty($cart->applied_coupons)) { foreach ($cart->applied_coupons as $code) { $coupon = new WC_Coupon($code); // Pre 2.5 is_valid_for_product() does not use wc_get_product_coupon_types() if (WC_Subscriptions::is_woocommerce_pre('2.5')) { $is_valid_for_product = true; } else { $is_valid_for_product = $coupon->is_valid_for_product(wc_get_product($product_id), $cart_item); } if ($coupon->apply_before_tax() && $coupon->is_valid() && $is_valid_for_product) { $apply_recurring_coupon = $apply_recurring_percent_coupon = $apply_initial_coupon = $apply_initial_percent_coupon = false; // Apply recurring fee discounts to recurring total calculations if ('recurring_total' == $calculation_type) { $apply_recurring_coupon = 'recurring_fee' == $coupon->type ? true : false; $apply_recurring_percent_coupon = 'recurring_percent' == $coupon->type ? true : false; } if ('none' == $calculation_type) { // If all items have a free trial we don't need to apply recurring coupons to the initial total if (!WC_Subscriptions_Cart::all_cart_items_have_free_trial()) { if ('recurring_fee' == $coupon->type) { $apply_initial_coupon = true; } if ('recurring_percent' == $coupon->type) { $apply_initial_percent_coupon = true; } } // Apply sign-up discounts to initial total if (!empty($cart_item['data']->subscription_sign_up_fee)) { if ('sign_up_fee' == $coupon->type) { $apply_initial_coupon = true; } if ('sign_up_fee_percent' == $coupon->type) { $apply_initial_percent_coupon = true; } $calculation_price = $cart_item['data']->subscription_sign_up_fee; } } if ($apply_recurring_coupon || $apply_initial_coupon) { $discount_amount = $calculation_price < $coupon->amount ? $calculation_price : $coupon->amount; // Recurring coupons only apply when there is no free trial (carts can have a mix of free trial and non free trial items) if ($apply_initial_coupon && 'recurring_fee' == $coupon->type && !empty($cart_item['data']->subscription_trial_length)) { $discount_amount = 0; } $cart->discount_cart = $cart->discount_cart + $discount_amount * $cart_item['quantity']; $cart = self::increase_coupon_discount_amount($cart, $coupon->code, $discount_amount * $cart_item['quantity']); $price = $price - $discount_amount; } elseif ($apply_recurring_percent_coupon) { $discount_amount = round($calculation_price / 100 * $coupon->amount, WC()->cart->dp); $cart->discount_cart = $cart->discount_cart + $discount_amount * $cart_item['quantity']; $cart = self::increase_coupon_discount_amount($cart, $coupon->code, $discount_amount * $cart_item['quantity']); $price = $price - $discount_amount; } elseif ($apply_initial_percent_coupon) { // Recurring coupons only apply when there is no free trial (carts can have a mix of free trial and non free trial items) if ('recurring_percent' == $coupon->type && empty($cart_item['data']->subscription_trial_length)) { $amount_to_discount = $cart_item['data']->subscription_price; } else { $amount_to_discount = 0; } // Sign up fee coupons only apply to sign up fees if ('sign_up_fee_percent' == $coupon->type) { $amount_to_discount = $cart_item['data']->subscription_sign_up_fee; } $discount_amount = round($amount_to_discount / 100 * $coupon->amount, WC()->cart->dp); $cart->discount_cart = $cart->discount_cart + $discount_amount * $cart_item['quantity']; $cart = self::increase_coupon_discount_amount($cart, $coupon->code, $discount_amount * $cart_item['quantity']); $price = $price - $discount_amount; } } } if ($price < 0) { $price = 0; } } return $price; }
/** * Function to apply discounts to a product and get the discounted price (before tax is applied). * * @access public * @param mixed $values * @param mixed $price * @param bool $add_totals (default: false) * @return float price */ public function get_discounted_price($values, $price, $add_totals = false) { if (!$price) { return $price; } if (!empty($this->applied_coupons)) { foreach ($this->applied_coupons as $code) { $coupon = new WC_Coupon($code); if ($coupon->apply_before_tax() && $coupon->is_valid()) { if ($coupon->is_valid_for_product($values['data']) || $coupon->is_valid_for_cart()) { $discount_amount = $coupon->get_discount_amount($price, $values, $single = true); $price = max($price - $discount_amount, 0); if ($add_totals) { $this->discount_cart += $discount_amount * $values['quantity']; $this->increase_coupon_discount_amount($code, $discount_amount * $values['quantity']); $this->increase_coupon_applied_count($code, $values['quantity']); } } } } } return apply_filters('woocommerce_get_discounted_price', $price, $values, $this); }
function load_cart_contents() { global $woocommerce; $cart = $woocommerce->cart->cart_contents; if (sizeof($cart > 0)) { foreach ($cart as $cart_item_key => $cart_item) { $woocommerce->cart->calculate_totals(); $product_id = $cart_item['product_id']; $product = $cart_item['data']; $coupons = $woocommerce->cart->applied_coupons; $price = $product->get_price(); $quantity = $cart_item['quantity']; $subtotal = $price * $quantity; if ($coupons) { foreach ($coupons as $key => $coupon) { $coupon = new WC_Coupon($coupon); if ($coupon->is_valid_for_product($product)) { $discount = $coupon->get_discount_amount($price, $cart_item); $subtotal = $price * $quantity - $discount; } } } ?> <tr class="selectable ins_order_item" id="<?php echo esc_attr($cart_item_key); ?> "> <td class="ins_product_id"><?php echo esc_html($product_id); ?> </td> <td class="ins_product_title"><?php echo $product->get_title(); ?> </td> <td class="ins_product_price"><?php echo wc_price($price); ?> </td> <td class="ins_product_quantity"><?php echo $quantity; ?> </td> <td class="ins_product_subtotal"><?php echo wc_price($subtotal); ?> </td> <?php $coupons = $woocommerce->cart->applied_coupons; if ($coupons) { foreach ($coupons as $coupon) { $coupon = new WC_Coupon($coupon); if ($coupon->is_valid_for_product($cart_item['data'])) { ?> <tr class="ins_item_discounts"> <td style="text-align:center;"><?php echo __('Discount', 'instore'); ?> </td> <td class="disc_code" style="text-align:center;"><?php echo esc_html($coupon->code); ?> </td> <td class="regular_price" style="text-align:center;"> Reg price <?php echo wc_price($product->get_price()); ?> </td> <td class="disc_amount" style="text-align:right;">- <?php echo wc_price($discount); ?> </td> </tr> <?php } } } ?> </tr> <?php } } }
/** * Does the coupon have a value? (autocoupon should not be applied if it has no value) * @param WC_Coupon $coupon The coupon data * @return bool True if it has a value (discount, free shipping, whatever) otherwise false) **/ function coupon_has_a_value($coupon) { $has_a_value = false; if ($coupon->enable_free_shipping()) { $has_a_value = true; } else { //Test whether discount > 0 //See WooCommerce: class-wc-cart.php function get_discounted_price global $woocommerce; foreach ($woocommerce->cart->get_cart() as $cart_item) { if ($coupon->is_valid_for_cart() || $coupon->is_valid_for_product($cart_item['data'], $cart_item)) { if ($coupon->get_discount_amount($cart_item['data']->price, $cart_item) > 0) { $has_a_value = true; break; } } } } return apply_filters('wjecf_coupon_has_a_value', $has_a_value, $coupon); }
/** * * Gets custom price on the order page * * @param WC_Product $product * @param int $item_id * @param str $start - Start date * @param str $end - End date * @param array $order_item * @param array $coupons * @return array $item_prices - Item prices (subtotal, total, tax subtotal and tax total) * **/ public function easy_booking_get_booking_price($product, $item_id, $start, $end, $order_item, $coupons) { if (!$product) { return false; } $calc_mode = $this->options['easy_booking_calc_mode']; // Calculation mode (Days or Nights) // Get booking duration $start_diff = strtotime($start); $end_diff = strtotime($end); $diff = absint($start_diff - $end_diff) * 1000; $days = $diff / 86400000; if ($days === 0) { $days = 1; } // If calculation mode is set to "Days", add one day if ($calc_mode === 'days' && $start != $end) { $duration = absint($days + 1); } elseif ($calc_mode === 'days' && $start === $end) { $duration = absint($days); } else { $duration = absint($days); } if ($product->is_taxable()) { $price = $product->get_price_excluding_tax(); // Product price excluding tax } else { $price = $product->get_price(); // Product price } // Price for x days $new_price = apply_filters('easy_booking_get_order_item_price', $price * $duration, $product, $item_id, $duration); if ($product->is_taxable()) { $item_tax_class = $order_item['tax_class']; $product_taxes = $this->easy_booking_get_product_taxes($new_price, $item_tax_class); // Product taxes without potential discounts foreach ($product_taxes as $_tax_id => $_tax_value) { $tax_subtotal[$_tax_id] = $_tax_value; } $tax_amount = WC_Tax::get_tax_total($product_taxes); } if ($coupons) { foreach ($coupons as $code) { $coupon = new WC_Coupon($code); if ($coupon->is_valid_for_product($product)) { $coupon_amount = $coupon->get_discount_amount($new_price, $order_item, true); // Discounted amount for item price $total = $new_price - $coupon_amount; // New price with discount if (!empty($product_taxes)) { foreach ($product_taxes as $_tax_id => $_tax_value) { $tax_discount[$_tax_id] = $coupon->get_discount_amount($_tax_value, $order_item, true); // Discounted amount for item taxes $tax_total[$_tax_id] = $_tax_value - $tax_discount[$_tax_id]; // Product taxes with discount } } } else { if (!empty($product_taxes)) { foreach ($product_taxes as $_tax_id => $_tax_value) { $tax_total[$_tax_id] = $_tax_value; // No valid coupon - Product taxes unchanged } } $total = $new_price; // No valid coupon - Product price unchanged } } } else { if (!empty($product_taxes)) { foreach ($product_taxes as $_tax_id => $_tax_value) { $tax_total[$_tax_id] = $_tax_value; // No coupon - Product taxes unchanged } } $total = $new_price; // No coupon - Product price unchanged } $new_price = $new_price * $order_item['quantity']; // Multiply subtotal by item quantity $total = $total * $order_item['quantity']; // Multiply total by item quantity if (!empty($product_taxes)) { foreach ($tax_subtotal as $tax_subtotal_id => $tax_subtotal_amount) { $tax_subtotal[$tax_subtotal_id] = $tax_subtotal_amount * $order_item['quantity']; } // Multiply tax subtotal by item quantity foreach ($tax_total as $tax_total_id => $tax_total_amount) { $tax_total[$tax_total_id] = $tax_total_amount * $order_item['quantity']; } // Multiply tax total by item quantity // Format taxes $line_taxes = array_map('wc_format_decimal', $tax_total); $line_subtotal_taxes = array_map('wc_format_decimal', $tax_subtotal); $item_prices['tax_subtotal'] = $line_subtotal_taxes; $item_prices['tax_total'] = $line_taxes; } $item_prices['subtotal'] = floatval(wc_format_decimal($new_price, 0)); $item_prices['total'] = floatval(wc_format_decimal($total, 0)); return $item_prices; }