anced_woo_discount_rules_run_discount_rules', $run_rule, $cart_object); } /** * Apply discount for the products in cart * @param $cart_object * @return boolean */ function applyCartProductDiscount($cart_object) { remove_action('woocommerce_cart_calculate_fees', array($this, 'applyCartDiscount')); Helper::clearPromotionMessages(); $do_apply_discount = $this->doApplyDiscount($cart_object); if($do_apply_discount){ $this->calculateCartPageDiscounts(); $processed_rule = false; if (!empty(self::$calculated_cart_item_discount)) { if (isset($cart_object->cart_contents) && !empty($cart_object->cart_contents)) { foreach ($cart_object->cart_contents as $key => $cart_item) { if (array_key_exists($key, self::$calculated_cart_item_discount)) { $processed_rule = true; $product_id = isset($cart_item['product_id']) ? $cart_item['product_id'] : 0; if (isset($cart_item['variation_id']) && !empty($cart_item['variation_id'])) { $product_id = $cart_item['variation_id']; } if (empty($product_id)) { return false; } $product_obj = isset($cart_item['data']) ? $cart_item['data'] : $cart_item; $item_quantity = isset($cart_item['quantity']) ? $cart_item['quantity'] : 0; //product price $initial_price = self::$calculated_cart_item_discount[$key]['initial_price']; $discounted_price = self::$calculated_cart_item_discount[$key]['discounted_price']; $calculator = self::$calculator; $total_discounts = $calculator::$total_discounts; $price = $discounted_price; $price = apply_filters('advanced_woo_discount_rules_discounted_price_of_cart_item', $price, $cart_item, $cart_object, self::$calculated_cart_item_discount[$key]); $do_apply_price_rules = apply_filters('advanced_woo_discount_rules_do_apply_price_discount', true, $price, $cart_item, $cart_object, self::$calculated_cart_item_discount[$key]); if($do_apply_price_rules){ self::$woocommerce_helper->setCartProductPrice($product_obj, $price); $product_obj->awdr_product_original_price = $initial_price; } } } } } // Disable third party coupon when rule applied $this->removeThirdPartyCouponIfRequired(self::$calculated_cart_item_discount, $processed_rule); do_action('advanced_woo_discount_rules_after_apply_discount', $cart_object, self::$calculated_cart_item_discount); } add_action('woocommerce_cart_calculate_fees', array($this, 'applyCartDiscount')); } /** * Calculate cart discounts * @return bool */ function calculateCartPageDiscounts() { $cart_items = self::$woocommerce_helper->getCart(); if (!empty($cart_items)) { foreach ($cart_items as $cart_key => $cart_item) { $product_id = self::$woocommerce_helper->getProductId(isset($cart_item['data']) ? $cart_item['data'] : $cart_item); $product = self::$woocommerce_helper->getProductFromCartItem($cart_item, $product_id); $quantity = $cart_item['quantity']; $calculate_discount_for_item = apply_filters('advanced_woo_discount_rules_calculate_discount_for_cart_item', true, $cart_item); if($calculate_discount_for_item){ $prices = self::calculateInitialAndDiscountedPrice($product, $quantity, true, false, $cart_item); if ($prices) { //add the cart quantity //Here discounts are calculated for per item $prices['cart_quantity'] = $quantity; $prices['product_id'] = $product_id; $apply_discount = $this->didAppliedDiscountAlready($cart_key, $prices); if ($apply_discount) { self::$calculated_cart_item_discount[$cart_key] = apply_filters('advanced_woo_discount_rules_cart_item_discount_prices', $prices, $cart_item, $product); } } } } } return true; } /** * Did applied discount already * * @param $cart_key string * @param $prices array * @return boolean * */ public function didAppliedDiscountAlready($cart_key, $prices) { $apply_discount = true; if (isset(self::$calculated_cart_item_discount[$cart_key])) { if (isset(self::$calculated_cart_item_discount[$cart_key]['discounted_price'])) { if (self::$calculated_cart_item_discount[$cart_key]['discounted_price'] > $prices['discounted_price']) { $apply_discount = false; } } } return apply_filters('advanced_woo_discount_rules_did_discount_applied_already', $apply_discount, self::$calculated_cart_item_discount, $cart_key, $prices); } /** * laod the bulk table discount message manually * @param $product * @param false $get_variable_product_table * @return false|string */ function showBulkTableInPositionManually($product, $get_variable_product_table = false) { if (!empty($product)) { $bulk_discounts_ranges = self::$calculator->getDefaultLayoutMessagesByRules($product, $get_variable_product_table); if(empty($bulk_discounts_ranges)){ $bulk_discounts_ranges['layout']['type'] = 'default'; $bulk_discounts_ranges['layout']['bulk_variant_table'] = 'default_variant_empty'; } $override_path = get_theme_file_path('advanced_woo_discount_rules/discount_table.php'); $bulk_table_template_path = WDR_PLUGIN_PATH . 'App/Views/Templates/discount_table.php'; if (file_exists($override_path)) { $bulk_table_template_path = $override_path; } if($get_variable_product_table){ return self::$template_helper->setPath($bulk_table_template_path)->setData(array('ranges' => $bulk_discounts_ranges, 'woocommerce' => self::$woocommerce_helper, 'base' => $this))->render(); } self::$template_helper->setPath($bulk_table_template_path)->setData(array('ranges' => $bulk_discounts_ranges, 'woocommerce' => self::$woocommerce_helper, 'base' => $this))->display(); } } /** * Show the bulk table discount message */ function showBulkTableInPosition() { global $product; if (!empty($product)) { $this->showBulkTableInPositionManually($product); } } /** * load the advanced table discount message manually */ function showAdvancedTableInPositionManually($product) { if (!empty($product)) { $bulk_discounts_ranges = self::$calculator->getAdvancedLayoutMessagesByRules($product); $bulk_table_template_path = WDR_PLUGIN_PATH . 'App/Views/Templates/discount_table.php'; self::$template_helper->setPath($bulk_table_template_path)->setData(array('ranges' => $bulk_discounts_ranges, 'woocommerce' => self::$woocommerce_helper))->display(); } } /** * Show the advanced table discount message */ function showAdvancedTableInPosition() { global $product; if (!empty($product)) { $this->showAdvancedTableInPositionManually($product); } } /** * save discounts of order for future * * @param $order_id * @param $items * @return bool */ function orderItemsSaved($order_id, $items) { $applied_rules = array(); $buy_x_get_x_free_discounts = isset(Rule::$additional_discounts['buy_x_get_x_discounts']) ? Rule::$additional_discounts['buy_x_get_x_discounts'] : ''; if(!empty($buy_x_get_x_free_discounts)){ $this->orderItemsSavedForBXGXFree($buy_x_get_x_free_discounts, $order_id, $items); } $buy_x_get_y_free_discounts = isset(Rule::$additional_discounts['buy_x_get_y_discounts']) ? Rule::$additional_discounts['buy_x_get_y_discounts'] : ''; if(!empty($buy_x_get_y_free_discounts)){ $this->orderItemsSavedForBXGYFree($buy_x_get_y_free_discounts, $order_id, $items); } if (!empty(self::$calculated_cart_item_discount)) { foreach (self::$calculated_cart_item_discount as $cart_key => $discount) { $product_id = isset($discount['product_id']) ? $discount['product_id'] : 0; if (empty($product_id)) { return false; } $save_order_item_discounts_array = array(); $initial_price = floatval(isset($discount['initial_price_with_tax']) ? $discount['initial_price_with_tax'] : 0); $discounted_price = floatval(isset($discount['discounted_price_with_tax']) ? $discount['discounted_price_with_tax'] : 0); $cart_quantity = floatval(isset($discount['cart_quantity']) ? $discount['cart_quantity'] : 0); $total_discount_details = isset($discount['total_discount_details']) ? $discount['total_discount_details'] : array(); $cart_discount_details = isset($discount['cart_discount_details']) ? $discount['cart_discount_details'] : array(); if (!empty($total_discount_details)) { $save_order_item_discounts_array = isset($total_discount_details[$cart_key])? $total_discount_details[$cart_key]: array(); } if (!empty($save_order_item_discounts_array) || !empty($cart_discount_details)) { foreach ($save_order_item_discounts_array as $key => $value) { $simple_discount = $bulk_discount = $set_discount = $cart_discount = 0; $rule_id = $key; $cart_discount = isset($cart_discount_details[$rule_id]['cart_discount']) ? $cart_discount_details[$rule_id]['cart_discount'] : '0'; $cart_shipping = (isset($cart_discount_details[$rule_id]['cart_shipping']) && !empty($cart_discount_details[$rule_id]['cart_shipping'])) ? $cart_discount_details[$rule_id]['cart_shipping'] : 'no'; $cart_discount_label = isset($cart_discount_details[$rule_id]['cart_discount_label']) ? $cart_discount_details[$rule_id]['cart_discount_label'] : ''; $simple_discount = isset($value['simple_discount']) ? $value['simple_discount'] : 0; if(is_array($simple_discount)){ $simple_discount = $simple_discount['discount_price'] * $cart_quantity; } else { $simple_discount = $simple_discount * $cart_quantity; } $bulk_discount = isset($value['bulk_discount']) ? $value['bulk_discount'] : 0; if(is_array($bulk_discount)){ $bulk_discount = $bulk_discount['discount_price'] * $cart_quantity; } else { $bulk_discount = $bulk_discount * $cart_quantity; } $set_discount = isset($value['set_discount']['discount_value']) ? $value['set_discount']['discount_value'] : 0; $set_discounted_price_quantity = isset($value['set_discount']['discounted_price_quantity']) ? $value['set_discount']['discounted_price_quantity'] : 0; if(!empty($set_discounted_price_quantity)){ $set_discount = $set_discount * $set_discounted_price_quantity; }else{ $set_discount = $set_discount * $cart_quantity; } $bxgx_discount_price = isset($value['buy_x_get_x_discount']['discount_price_per_quantity']) ? $value['buy_x_get_x_discount']['discount_price_per_quantity'] : 0; $bxgx_discount_qty = isset($value['buy_x_get_x_discount']['discount_quantity']) ? $value['buy_x_get_x_discount']['discount_quantity'] : 0; $bxgx_discount = $bxgx_discount_price * $bxgx_discount_qty; $bxgy_discount_price = isset($value['buy_x_get_y_discount']['discount_price_per_quantity']) ? $value['buy_x_get_y_discount']['discount_price_per_quantity'] : 0; $bxgy_discount_qty = isset($value['buy_x_get_y_discount']['discount_quantity']) ? $value['buy_x_get_y_discount']['discount_quantity'] : 0; $bxgy_discount = $bxgy_discount_price * $bxgy_discount_qty; $bxgy_cheapest_discount_price = isset($value['buy_x_get_y_cheapest_in_cart_discount']['discount_price_per_quantity']) ? $value['buy_x_get_y_cheapest_in_cart_discount']['discount_price_per_quantity'] : 0; $bxgy_cheapest_discount_qty = isset($value['buy_x_get_y_cheapest_in_cart_discount']['discount_quantity']) ? $value['buy_x_get_y_cheapest_in_cart_discount']['discount_quantity'] : 0; $bxgy_cheapest_discount = $bxgy_cheapest_discount_price * $bxgy_cheapest_discount_qty; $buy_x_get_y_cheapest_additional = isset($value['buy_x_get_y_cheapest_in_cart_discount']['additional_discounts']) ? $value['buy_x_get_y_cheapest_in_cart_discount']['additional_discounts'] : ''; $bxgy_cheapest_from_product_discount_price = isset($value['buy_x_get_y_cheapest_from_products_discount']['discount_price_per_quantity']) ? $value['buy_x_get_y_cheapest_from_products_discount']['discount_price_per_quantity'] : 0; $bxgy_cheapest_from_product_discount_qty = isset($value['buy_x_get_y_cheapest_from_products_discount']['discount_quantity']) ? $value['buy_x_get_y_cheapest_from_products_discount']['discount_quantity'] : 0; $bxgy_cheapest_from_product_discount = $bxgy_cheapest_from_product_discount_price * $bxgy_cheapest_from_product_discount_qty; $bogo_cheapest_aditional_sum = 0; if(!empty($buy_x_get_y_cheapest_additional)) { $bogo_cheapest_aditional = array(); foreach ($buy_x_get_y_cheapest_additional as $aditional) { $bogo_cheapest_discount_aditional = isset($aditional['discount_price_per_quantity']) ? $aditional['discount_price_per_quantity'] : 0; $bogo_cheapest_quantity_aditional = isset($aditional['discount_quantity']) ? $aditional['discount_quantity'] : 0; $bogo_cheapest_aditional[] = $bogo_cheapest_discount_aditional * $bogo_cheapest_quantity_aditional; } $bogo_cheapest_aditional_sum = array_sum($bogo_cheapest_aditional); } $discount_price = $simple_discount + $bulk_discount + $set_discount + $bxgx_discount + $bxgy_discount + $bxgy_cheapest_discount + $bogo_cheapest_aditional_sum + $bxgy_cheapest_from_product_discount; if ($discount_price < 0) { $discount_price = 0; } if($discount_price != 0){ $applied_rules[] = $rule_id; } DBTable::saveOrderItemDiscounts($order_id, $product_id, $initial_price, $discounted_price, $discount_price, $cart_quantity, $rule_id, $simple_discount, $bulk_discount, $set_discount, $cart_discount, $cart_discount_label, $cart_shipping); } if (!empty($cart_discount_details)) { foreach ($cart_discount_details as $key => $value) { if (!in_array($key, $applied_rules)) { $rule_id = $key; //$cart_discount = isset($cart_discount_details[$rule_id]['cart_discount']) ? $cart_discount_details[$rule_id]['cart_discount'] : ''; $cart_shipping = (isset($cart_discount_details[$rule_id]['cart_shipping']) && !empty($cart_discount_details[$rule_id]['cart_shipping'])) ? $cart_discount_details[$rule_id]['cart_shipping'] : 'no'; $cart_discount_label = isset($cart_discount_details[$rule_id]['cart_discount_label']) ? $cart_discount_details[$rule_id]['cart_discount_label'] : ''; $cart_discount = isset($cart_discount_details[$rule_id]['cart_discount_price']) ? $cart_discount_details[$rule_id]['cart_discount_price'] : 0; if($cart_discount != 0){ $applied_rules[] = $rule_id; } DBTable::saveOrderItemDiscounts($order_id, 0, 0, $discounted_price, 0, 0, $rule_id, 0, 0, 0, $cart_discount, $cart_discount_label, $cart_shipping); } } } } } } else { $cart = self::$woocommerce_helper->getCart(); $cart_discount_details = self::$calculator->getCartDiscountPrices($cart, true); $simple_discount = $bulk_discount = $set_discount = $discount_price = 0; foreach ($cart_discount_details as $key => $value) { $rule_id = $key; $cart_discount = isset($cart_discount_details[$rule_id]['cart_discount']) ? $cart_discount_details[$rule_id]['cart_discount'] : ''; $cart_shipping = (isset($cart_discount_details[$rule_id]['cart_shipping']) && !empty($cart_discount_details[$rule_id]['cart_shipping'])) ? $cart_discount_details[$rule_id]['cart_shipping'] : 'no'; $cart_discount_label = isset($cart_discount_details[$rule_id]['cart_discount_label']) ? $cart_discount_details[$rule_id]['cart_discount_label'] : ''; DBTable::saveOrderItemDiscounts($order_id, 0, 0, $discount_price, 0, 0, $rule_id, $simple_discount, $bulk_discount, $set_discount, $cart_discount, $cart_discount_label, $cart_shipping); } } $calc = self::$calculator; $applied_rules = $calc::$applied_rules; if (!empty($applied_rules)) { foreach ($applied_rules as $rule) { $used_limits = intval($rule->getUsedLimits()) + 1; DBTable::updateRuleUsedCount($rule->getId(), $used_limits); } } if (!empty(self::$calculated_cart_discount)) { if (isset(self::$calculated_cart_discount["discount"]) && !empty(self::$calculated_cart_discount["discount"])) { $discount_details = json_encode(self::$calculated_cart_discount["discount"]); $free_shipping = 'no'; self::$calculated_cart_discount["free_shipping"] = apply_filters('advanced_woo_discount_rules_isset_free_shipping','no'); if (isset(self::$calculated_cart_discount["free_shipping"]) && self::$calculated_cart_discount["free_shipping"] == 'yes') { $order = self::$woocommerce_helper->getOrder($order_id); if (!empty($order)) { if (self::$woocommerce_helper->orderHasShippingMethod($order, 'wdr_free_shipping')) { $free_shipping = 'yes'; } } } DBTable::saveOrderDiscounts($order_id, $free_shipping, $discount_details); } } } /** * Bxgx free product amount data * @param $buy_x_get_x_free_discounts * @param $order_id * @param $items */ function orderItemsSavedForBXGXFree($buy_x_get_x_free_discounts, $order_id, $items){ if(!empty($buy_x_get_x_free_discounts)){ foreach ($buy_x_get_x_free_discounts as $buy_x_get_x_free_key => $buy_x_get_x_free_discount){ $simple_discount = $bulk_discount = $set_discount = $cart_discount = 0; $rule_id = isset($buy_x_get_x_free_discount['rule_id']) ? $buy_x_get_x_free_discount['rule_id'] : 0; $variant_item_id = isset($buy_x_get_x_free_discount['variation_id']) ? $buy_x_get_x_free_discount['variation_id'] : ''; $item_id = isset($buy_x_get_x_free_discount['product_id']) ? $buy_x_get_x_free_discount['product_id'] : 0; if(!empty($variant_item_id)){ $item_id = $variant_item_id; } $free_product = isset($buy_x_get_x_free_discount['cart_item']['data']) ? $buy_x_get_x_free_discount['cart_item']['data'] : ''; $discount = Woocommerce::getProductPrice($free_product); $discount_quantity = isset($buy_x_get_x_free_discount['discount_quantity']) ? $buy_x_get_x_free_discount['discount_quantity'] : 0; $discount_price = $discount * $discount_quantity; $cart_shipping = 'no'; $cart_discount_label = ''; if($discount_price != 0){ $applied_rules[] = $rule_id; } DBTable::saveOrderItemDiscounts($order_id, $item_id, 0, 0, $discount_price, $discount_quantity, $rule_id, $simple_discount, $bulk_discount, $set_discount, $cart_discount, $cart_discount_label, $cart_shipping); } } } /** * Bxgy free product amount data * @param $buy_x_get_y_free_discounts * @param $order_id * @param $items */ function orderItemsSavedForBXGYFree($buy_x_get_y_free_discounts, $order_id, $items){ if(!empty($buy_x_get_y_free_discounts)){ foreach ($buy_x_get_y_free_discounts as $buy_x_get_y_free_key => $buy_x_get_y_free_discount){ $simple_discount = $bulk_discount = $set_discount = $cart_discount = $discount_quantity = 0; $rule_id = isset($buy_x_get_y_free_discount['rule_id']) ? $buy_x_get_y_free_discount['rule_id'] : 0; $variant_item_id = isset($buy_x_get_y_free_discount['variation_id']) ? $buy_x_get_y_free_discount['variation_id'] : 0; $item_id = isset($buy_x_get_y_free_discount['product_id']) ? $buy_x_get_y_free_discount['product_id'] : 0; if(!empty($variant_item_id)){ $item_id = $variant_item_id; } if($item_id != 0){ $free_product = isset($buy_x_get_y_free_discount['cart_item']['data']) ? $buy_x_get_y_free_discount['cart_item']['data'] : ''; $discount = Woocommerce::getProductPrice($free_product); $discount_quantity = isset($buy_x_get_y_free_discount['discount_quantity']) ? $buy_x_get_y_free_discount['discount_quantity'] : 0; $discount_price = $discount * $discount_quantity; }else{ $discount_price = 0; $discount_products = isset($buy_x_get_y_free_discount['discount_products']) ? $buy_x_get_y_free_discount['discount_products'] : array(); if(!empty($discount_products)){ foreach ($discount_products as $product_id){ $free_product = Woocommerce::getProduct((int)$product_id); $discount = Woocommerce::getProductPrice($free_product); $discount_quantity = isset($buy_x_get_y_free_discount['discount_quantity']) ? $buy_x_get_y_free_discount['discount_quantity'] : 0; $discount_price += $discount * $discount_quantity; } } } $cart_shipping = 'no'; $cart_discount_label = ''; if($discount_price != 0){ $applied_rules[] = $rule_id; } DBTable::saveOrderItemDiscounts($order_id, $item_id, 0, 0, $discount_price, $discount_quantity, $rule_id, $simple_discount, $bulk_discount, $set_discount, $cart_discount, $cart_discount_label, $cart_shipping); } } } /** * Show the discount promotion message */ function showAppliedRulesMessages() { $message = self::$config->getConfig('applied_rule_message', __('Discount {{title}} has been applied to your cart.', 'woo-discount-rules')); $message = Helper::getCleanHtml($message); $message = __($message, 'woo-discount-rules'); $calc = self::$calculator; $applied_rules = $calc::$applied_rules; if (!empty($applied_rules)) { foreach ($applied_rules as $rule) { $title = $rule->getTitle(); $title = __($title, 'woo-discount-rules'); $message_to_display = str_replace('{{title}}', $title, $message); $message_to_display = apply_filters('advanced_woo_discount_rules_message_to_display_when_rules_applied', $message_to_display, $rule); self::$woocommerce_helper->printNotice($message_to_display, 'success'); } } } /** * Display promotional message in check out while processing order review * */ public function displayPromotionMessagesInCheckout(){ echo "
"; $this->showAppliedRulesMessages(); echo "
"; echo ""; } /** * Load outer div for displaying promotional message in check out * */ public function displayPromotionMessagesInCheckoutContainer(){ echo "
"; echo "
"; } /** * Show the modified price on cart * @param $item_price * @param $cart_item * @param $cart_item_key * @return mixed|void */ function getCartPriceHtml($item_price, $cart_item, $cart_item_key){ $show_strikeout_on_cart = self::$config->getConfig('show_strikeout_on_cart', 1); if (!empty($show_strikeout_on_cart)) { $new_item_price_html = ''; if (isset(self::$calculated_cart_item_discount[$cart_item_key])) { $initial_price = self::$calculated_cart_item_discount[$cart_item_key]['initial_price']; $discounted_price = self::$calculated_cart_item_discount[$cart_item_key]['discounted_price']; $discount_lines = self::$calculated_cart_item_discount[$cart_item_key]['discount_lines']; if(!empty($discount_lines)){ $calculator = self::$calculator; $product_obj = isset($cart_item['data']) ? $cart_item['data'] : $cart_item; $initial_price_with_tax_call = $discounted_price_with_tax_call = 0; if(!empty($initial_price)){ $initial_price_with_tax_call = $calculator->mayHaveTax($product_obj, $initial_price); } $has_non_applied_lines = false; if(isset($discount_lines['non_applied']) && !empty($discount_lines['non_applied'])){ if(isset($discount_lines['non_applied']['quantity']) && $discount_lines['non_applied']['quantity'] > 0){ $has_non_applied_lines = true; } } $discount_line_count = count($discount_lines); $has_multiple_strikeout_lines = false; foreach ($discount_lines as $key => $discount_line){ if($key !== 'non_applied'){ $has_multiple_strikeout_lines = true; $discounted_price = $discount_line['discounted_price']; $discounted_qty = $discount_line['quantity']; $discounted_price_with_tax_call = $calculator->mayHaveTax($product_obj, $discounted_price); if($has_non_applied_lines == false && $discount_line_count === 2){ $new_item_price_html .= '
'.$this->getStrikeoutPrice($initial_price_with_tax_call, $discounted_price_with_tax_call).'
'; } else { $new_item_price_html .= '
'.$this->getStrikeoutPrice($initial_price_with_tax_call, $discounted_price_with_tax_call).' x '.$discounted_qty.'
'; } } } if($has_multiple_strikeout_lines){ if(isset($discount_lines['non_applied']) && !empty($discount_lines['non_applied'])){ if(isset($discount_lines['non_applied']['quantity']) && $discount_lines['non_applied']['quantity'] > 0){ $discounted_qty = $discount_lines['non_applied']['quantity']; $new_item_price_html .= '
'.self::$woocommerce_helper->formatPrice($initial_price_with_tax_call).' x '.$discounted_qty.'
'; } } } } if($new_item_price_html !== ''){ return apply_filters('advanced_woo_discount_rules_cart_strikeout_price_html', $new_item_price_html, $item_price, $cart_item, $cart_item_key); } } } return $item_price; } /** * Show the modified price on cart * @param $item_subtotal_price * @param $cart_item * @param $cart_item_key * @return string */ function getCartProductSubtotalPriceHtml($item_subtotal_price, $cart_item, $cart_item_key) { if (isset(self::$calculated_cart_item_discount[$cart_item_key]) && !empty(self::$calculated_cart_item_discount[$cart_item_key])) { $discount = $this->getDiscountPerItem(self::$calculated_cart_item_discount[$cart_item_key]); if($discount > 0){ $total_discount = self::$woocommerce_helper->formatPrice($discount); $subtotal_additional_text = '
' . $this->getYouSavedText($total_discount); $item_subtotal_price .= apply_filters('advanced_woo_discount_rules_line_item_subtotal_saved_text', $subtotal_additional_text, $total_discount, $discount); } } return $item_subtotal_price; } /** * get You saved text * @param $discount * @return string|null */ function getYouSavedText($discount) { if (!empty($discount)) { $text = self::$config->getConfig('you_saved_text', __("You saved {{total_discount}}", 'woo-discount-rules')); $text = __($text, 'woo-discount-rules'); $text = Helper::getCleanHtml($text); $message = str_replace('{{total_discount}}', $discount, $text); return '
' . $message . '
'; } return NULL; } /** * Get the discount per item * @param $discount_details * @return float|int */ function getDiscountPerItem($discount_details) { /** * Field name changed. For backward compatible we have to check the key exists * saved_amount_with_tax => saved_amount_based_on_tax_settings * initial_price_with_tax => initial_price_based_on_tax_settings (only for orders) * discounted_price_with_tax => discounted_price_based_on_tax_settings (only for orders) * */ $saved_amount_with_tax_field_name = 'saved_amount_with_tax'; $initial_price_with_tax_field_name = 'initial_price_with_tax'; $discounted_price_with_tax_field_name = 'discounted_price_with_tax'; if(isset($discount_details['saved_amount_based_on_tax_settings'])){ $saved_amount_with_tax_field_name = 'saved_amount_based_on_tax_settings'; } if(isset($discount_details['initial_price_based_on_tax_settings'])){ $initial_price_with_tax_field_name = 'initial_price_based_on_tax_settings'; } if(isset($discount_details['discounted_price_based_on_tax_settings'])){ $discounted_price_with_tax_field_name = 'discounted_price_based_on_tax_settings'; } if(isset($discount_details[$saved_amount_with_tax_field_name])){ if ($discount_details[$saved_amount_with_tax_field_name] > 0) { return $discount_details[$saved_amount_with_tax_field_name]; } } $discounted_price = isset($discount_details[$discounted_price_with_tax_field_name]) ? $discount_details[$discounted_price_with_tax_field_name] : 0; $initial_price = isset($discount_details[$initial_price_with_tax_field_name]) ? $discount_details[$initial_price_with_tax_field_name] : 0; $cart_quantity = isset($discount_details['cart_quantity']) ? $discount_details['cart_quantity'] : 0; $total_discount_per_quantity = $initial_price - $discounted_price; if ($discounted_price >= 0 && $total_discount_per_quantity > 0) { if (empty($total_discount_per_quantity)) { $total_discount_per_quantity = $initial_price; } $total_discount_per_quantity = $total_discount_per_quantity * $cart_quantity; return $total_discount_per_quantity; } return 0; } /** * Show the modified price on cart * @param $cart_total_price * @return string */ function getCartTotalPriceHtml($cart_total_price) { if (!empty(self::$calculated_cart_item_discount)) { $total_discount = 0; foreach (self::$calculated_cart_item_discount as $discount_details) { if (!empty($discount_details)) { $total_discount += $this->getDiscountPerItem($discount_details); } } if (empty($total_discount)) { return $cart_total_price; } $total_discount = self::$woocommerce_helper->formatPrice($total_discount); $cart_total_price .= '
' . $this->getYouSavedText($total_discount); } return $cart_total_price; } /** * Adding the discount per order item in item meta * @param $item * @param $cart_item_key * @param $values * @param $order */ function onCreateWoocommerceOrderLineItem($item, $cart_item_key, $values, $order) { $meta_discount_details = isset(self::$calculated_cart_item_discount[$cart_item_key]) ? self::$calculated_cart_item_discount[$cart_item_key] : array(); if(!empty($meta_discount_details)){ $item_quantity = isset($item->legacy_values['quantity']) ? $item->legacy_values['quantity'] : 0; $product = isset($item->legacy_values['data']) ? $item->legacy_values['data'] : array(); $item_product_id = Woocommerce::getProductId($product); $item_cart_discount_details = isset($meta_discount_details['cart_discount_details']) ? $meta_discount_details['cart_discount_details'] : array(); if(!empty($item_cart_discount_details)){ foreach ($item_cart_discount_details as $key => $detail){ $rule_applied_product_id = isset($detail['applied_product_ids']) ? $detail['applied_product_ids'] : array(); $cart_discount_price = isset( $meta_discount_details['cart_discount_details'][$key]['cart_discount_product_price'][$item_product_id][$key])? $meta_discount_details['cart_discount_details'][$key]['cart_discount_product_price'][$item_product_id][$key] : 0; $meta_discount_details['cart_discount_details'][$key]['cart_discount_price'] = $cart_discount_price; $cart_discount_price = $cart_discount_price / $item_quantity; $meta_discount_details['cart_discount_details'][$key]['cart_discount_product_price'] = $cart_discount_price; unset($meta_discount_details['cart_discount_details'][$key]['applied_product_ids']); if(!in_array($item_product_id,$rule_applied_product_id)){ unset( $meta_discount_details['cart_discount_details'][$key]); } } } if(isset($meta_discount_details['initial_price_with_tax'])){ $meta_discount_details['initial_price_based_on_tax_settings'] = $meta_discount_details['initial_price_with_tax']; unset($meta_discount_details['initial_price_with_tax']); } if(isset($meta_discount_details['discounted_price_with_tax'])){ $meta_discount_details['discounted_price_based_on_tax_settings'] = $meta_discount_details['discounted_price_with_tax']; unset($meta_discount_details['discounted_price_with_tax']); } self::$woocommerce_helper->setOrderItemMeta($item, '_advanced_woo_discount_item_total_discount', $meta_discount_details); } } /** * Calculate the product's initial and discount price * @param $product * @param $quantity * @param $is_cart * @param $ajax_price * @return array|bool */ static function calculateInitialAndDiscountedPrice($product, $quantity, $is_cart = false, $ajax_price = false, $cart_item = array()) { return self::$calculator->getProductPriceToDisplay($product, $quantity, $is_cart, $ajax_price, $cart_item); } /** * Re-calculate cart total * */ public static function reCalculateCartTotal() { if (function_exists('WC')) { if(isset(WC()->cart) && WC()->cart != null){ if (method_exists(WC()->cart, 'calculate_totals')) { WC()->cart->calculate_totals(); } } } } /** * @param $discount_details * @param $initial_price * @param $discounted_price * @param $product_obj * @param $price_html * @param $cart_item_qty * @param $ajax_price * @return bool|string */ function getSetDiscountItemPriceHtml($discount_details, $initial_price, $discounted_price, $product_obj, $price_html, $cart_item_qty=0, $ajax_price = false) { if (is_array($discount_details) && !empty($discount_details)) { $bogo_cheapest_aditional = array(); $calculator = self::$calculator; $other_discounts = $total_discount_price = $total_quantity = $current_product_quantity = $discount_operator = $bogo_cheapest_aditional_sum = 0; $partially_qualified_set = array(); $partially_qualified_set_amount_duplicate = array(); $apply_full_set = false; $int_increment = 0; foreach ($discount_details as $detail_key => $detail) { $bogo_cheapest_discount = $bogo_cheapest_quantity = 0; //BOGO cheapest discount $bogo_cheap_in_cart = isset($detail['buy_x_get_y_cheapest_in_cart_discount']) ? $detail['buy_x_get_y_cheapest_in_cart_discount'] : ''; $bogo_cheap_in_products = isset($detail['buy_x_get_y_cheapest_from_products_discount']) ? $detail['buy_x_get_y_cheapest_from_products_discount'] : ''; $bogo_cheap_in_category = isset($detail['buy_x_get_y_cheapest_from_categories_discount']) ? $detail['buy_x_get_y_cheapest_from_categories_discount'] : ''; $bogo_auto_add = isset($detail['buy_x_get_y_discount']) ? $detail['buy_x_get_y_discount'] : ''; $bogo_getx = isset($detail['buy_x_get_x_discount']) ? $detail['buy_x_get_x_discount'] : ''; $buy_x_get_y_cheapest_additional = isset($detail['buy_x_get_y_cheapest_in_cart_discount']['additional_discounts']) ? $detail['buy_x_get_y_cheapest_in_cart_discount']['additional_discounts'] : ''; if(!empty($buy_x_get_y_cheapest_additional)){ $all_matched_rule = self::$config->getConfig('apply_product_discount_to', 'biggest_discount');//all foreach ($buy_x_get_y_cheapest_additional as $aditional){ if($all_matched_rule != 'all' && $price_html){ $price_html = $this->buy_x_get_y_cheapest_additional_strike_out($product_obj, $bogo_cheap_in_cart, $initial_price, $cart_item_qty); if(!empty($price_html)){ return $price_html; } } $bogo_cheapest_discount_aditional = isset($aditional['discount_price_per_quantity']) ? $aditional['discount_price_per_quantity'] : 0; $bogo_cheapest_quantity_aditional = isset($aditional['discount_quantity']) ? $aditional['discount_quantity'] : 0; $bogo_cheapest_aditional[] = $bogo_cheapest_discount_aditional*$bogo_cheapest_quantity_aditional; } if($all_matched_rule == 'all' && !empty($cart_item_qty)){ $bogo_cheapest_discount_extra = isset($bogo_cheap_in_cart['discount_price_per_quantity']) ? $bogo_cheap_in_cart['discount_price_per_quantity'] : 0; $bogo_cheapest_quantity_extra = isset($bogo_cheap_in_cart['discount_quantity']) ? $bogo_cheap_in_cart['discount_quantity'] : 0; $bogo_cheapest_aditional_discount = array_sum($bogo_cheapest_aditional); if($bogo_cheapest_quantity_extra > 1){ $bogo_cheapest_quantity_extra = (int)$bogo_cheapest_quantity_extra - 1; $bogo_cheapest_aditional_discount += $bogo_cheapest_discount_extra*$bogo_cheapest_quantity_extra; } $bogo_cheapest_aditional_discount = $bogo_cheapest_aditional_discount / $cart_item_qty; $discounted_price = $discounted_price-$bogo_cheapest_aditional_discount; $bogo_cheap_in_cart = array(); }else{ $bogo_cheapest_aditional_sum = array_sum($bogo_cheapest_aditional); } } if(!empty($bogo_cheap_in_cart)){ $bogo_cheapest_discount = isset($bogo_cheap_in_cart['discount_price_per_quantity']) ? $bogo_cheap_in_cart['discount_price_per_quantity'] : 0; $bogo_cheapest_quantity = isset($bogo_cheap_in_cart['discount_quantity']) ? $bogo_cheap_in_cart['discount_quantity'] : 0; }elseif(!empty($bogo_cheap_in_products)){ $bogo_cheapest_discount = isset($bogo_cheap_in_products['discount_price_per_quantity']) ? $bogo_cheap_in_products['discount_price_per_quantity'] : 0; $bogo_cheapest_quantity = isset($bogo_cheap_in_products['discount_quantity']) ? $bogo_cheap_in_products['discount_quantity'] : 0; }elseif (!empty($bogo_cheap_in_category)){ $bogo_cheapest_discount = isset($bogo_cheap_in_category['discount_price_per_quantity']) ? $bogo_cheap_in_category['discount_price_per_quantity'] : 0; $bogo_cheapest_quantity = isset($bogo_cheap_in_category['discount_quantity']) ? $bogo_cheap_in_category['discount_quantity'] : 0; }elseif(!empty($bogo_auto_add)){ $bogo_cheapest_discount = isset($bogo_auto_add['discount_price_per_quantity']) ? $bogo_auto_add['discount_price_per_quantity'] : 0; $bogo_cheapest_quantity = isset($bogo_auto_add['discount_quantity']) ? $bogo_auto_add['discount_quantity'] : 0; }else{ $bogo_cheapest_discount = isset($bogo_getx['discount_price_per_quantity']) ? $bogo_getx['discount_price_per_quantity'] : 0; $bogo_cheapest_quantity = isset($bogo_getx['discount_quantity']) ? $bogo_getx['discount_quantity'] : 0; } $bogo_partial_set = ''; if(!empty($bogo_cheapest_quantity) && $bogo_cheapest_quantity < $cart_item_qty){ $bogo_partial_set = 'yes'; }elseif($bogo_cheapest_quantity == $cart_item_qty){ $bogo_cheapest_full_matched_set = $bogo_cheapest_discount; } $int_increment++; //per product quantity for set discount $fully_qualified_set = $discounted_price_per_set = $bogo_cheapest_full_matched_set = 0; $total_quantity = $cart_item_qty; $current_product_quantity = $cart_item_qty; if(!empty($detail['set_discount'])){ $total_quantity = isset($detail['set_discount']['total_quantity']) ? $detail['set_discount']['total_quantity'] : 0; $discount_operator = isset($detail['set_discount']['discount_operator']) ? $detail['set_discount']['discount_operator'] : ""; //original price quantity for set discount $original_price_quantity = isset($detail['set_discount']['original_price_quantity']) ? $detail['set_discount']['original_price_quantity'] : 0; //min set quantity $discounted_price_quantity = isset($detail['set_discount']['discounted_price_quantity']) ? $detail['set_discount']['discounted_price_quantity'] : 0; //set discounted price $discounted_price_per_set = isset($detail['set_discount']['discount_value']) ? $detail['set_discount']['discount_value'] : 0; $discounted_price_quantities = isset($detail['set_discount']['discounted_price_quantity']) ? $detail['set_discount']['discounted_price_quantity'] : 0; if (!empty($total_quantity)) { self::$set_total_quantity = $total_quantity; } if (empty($total_quantity)) { $total_quantity = self::$set_total_quantity; } } //simple discounted price if(isset($detail['simple_discount']) && is_array($detail['simple_discount'])){ $simple_discount = isset($detail['simple_discount']['discount_price']) ? $detail['simple_discount']['discount_price'] : 0; } else { $simple_discount = isset($detail['simple_discount']) ? $detail['simple_discount'] : 0; } //bulk discounted price if(isset($detail['bulk_discount']) && is_array($detail['bulk_discount'])){ $bulk_discounts = isset($detail['bulk_discount']['discount_price']) ? $detail['bulk_discount']['discount_price'] : 0; } else { $bulk_discounts = isset($detail['bulk_discount']) ? $detail['bulk_discount'] : 0; } //set discounted price quantity $total_discount_price += $discounted_price_per_set + $simple_discount + $bulk_discounts + $bogo_cheapest_discount; if (empty($original_price_quantity) && empty($discounted_price_quantity) && empty($bogo_partial_set)) { if (!empty($discounted_price_per_set)) { $fully_qualified_set = $discounted_price_per_set; } } else { if(!empty($detail['set_discount']) && !empty($discounted_price_per_set) && !empty($discounted_price_quantities)){ if (isset($partially_qualified_set[$discounted_price_per_set]) && !empty($partially_qualified_set[$discounted_price_per_set])) { $partially_qualified_set_amount_duplicate[$discounted_price_per_set . '-' . $int_increment] = $discounted_price_quantities; } else { $partially_qualified_set[$discounted_price_per_set] = $discounted_price_quantities; } } if(!empty($bogo_partial_set)){ $int_increment++; if (isset($partially_qualified_set[$bogo_cheapest_discount]) && !empty($partially_qualified_set[$bogo_cheapest_discount])) { $partially_qualified_set_amount_duplicate[$bogo_cheapest_discount . '-' . $int_increment] = $bogo_cheapest_quantity; } else { $partially_qualified_set[$bogo_cheapest_discount] = $bogo_cheapest_quantity; } } } $other_discounts += $simple_discount + $bulk_discounts + $fully_qualified_set + $bogo_cheapest_full_matched_set + $bogo_cheapest_aditional_sum; } $initial_price_with_tax_call = $discounted_price_with_tax_call = 0; if (empty($partially_qualified_set) || $apply_full_set == true) { if ($price_html) { if($ajax_price){ $discounted_price = $initial_price - $discounted_price; if($discounted_price < 0){ $discounted_price = 0; } if(!empty($initial_price)){ $initial_price_with_tax_call = $calculator->mayHaveTax($product_obj, $initial_price); } if(!empty($discounted_price)){ $discounted_price_with_tax_call = $calculator->mayHaveTax($product_obj, $discounted_price); } $item_price = $this->getStrikeoutPrice($initial_price_with_tax_call, $discounted_price_with_tax_call); return $item_price.Woocommerce::getProductPriceSuffix($product_obj, $discounted_price_with_tax_call); } if(!empty($initial_price)){ $initial_price_with_tax_call = $calculator->mayHaveTax($product_obj, $initial_price); } if(!empty($discounted_price)){ $discounted_price_with_tax_call = $calculator->mayHaveTax($product_obj, $discounted_price); } $item_price = $this->getStrikeoutPrice($initial_price_with_tax_call, $discounted_price_with_tax_call); return $item_price.Woocommerce::getProductPriceSuffix($product_obj, $discounted_price_with_tax_call); } else { if($discounted_price < 0){ $discounted_price = 0; } return $discounted_price; ///return false; } } else { $original_item_price_html = $this->getSetStrikeoutPrice($initial_price, $partially_qualified_set, $total_discount_price, $other_discounts, $total_quantity, $product_obj, $price_html, $current_product_quantity, $discount_operator, $partially_qualified_set_amount_duplicate); return $original_item_price_html; } } else { return false; } } /** * buy_x_get_y_cheapest_additional_strike_out * @param $product_obj * @param $bogo_cheap_in_cart * @param $initial_price * @param $cart_item_qty * @return string */ public function buy_x_get_y_cheapest_additional_strike_out($product_obj, $bogo_cheap_in_cart, $initial_price, $cart_item_qty) { $price_html = $original_price_with_tax_call = $discounted_price_with_tax_call = ''; $bogo_cheap_in_cart_first_discount = isset($bogo_cheap_in_cart['discount_price_per_quantity']) ? $bogo_cheap_in_cart['discount_price_per_quantity'] : 0; $bogo_cheap_in_cart_first_discount_qty = isset($bogo_cheap_in_cart['discount_quantity']) ? $bogo_cheap_in_cart['discount_quantity'] : 0; $discounted_price = $bogo_cheap_in_cart_first_discount * $bogo_cheap_in_cart_first_discount_qty; $discounted_price = $initial_price - $discounted_price; if(!empty($initial_price)){ $original_price_with_tax_call = self::$calculator->mayHaveTax($product_obj, $initial_price); } if(!empty($discounted_price)){ $discounted_price_with_tax_call = self::$calculator->mayHaveTax($product_obj, $discounted_price); } $price_suffix = Woocommerce::getProductPriceSuffix($product_obj, $discounted_price_with_tax_call); $price_html .= '' . self::$woocommerce_helper->formatPrice($original_price_with_tax_call) . ' ' . self::$woocommerce_helper->formatPrice($discounted_price_with_tax_call) .$price_suffix. ' x ' . $bogo_cheap_in_cart_first_discount_qty . '
'; $bogo_cheap_in_cart_total_discount_qty[] = $bogo_cheap_in_cart_first_discount_qty; $bogo_cheap_in_cart_additional_discounts = $bogo_cheap_in_cart['additional_discounts']; foreach ($bogo_cheap_in_cart_additional_discounts as $additional_discount){ $bogo_cheapest_discount_aditional = isset($additional_discount['discount_price_per_quantity']) ? $additional_discount['discount_price_per_quantity'] : 0; $bogo_cheapest_quantity_aditional = isset($additional_discount['discount_quantity']) ? $additional_discount['discount_quantity'] : 0; $discounted_price = $bogo_cheapest_discount_aditional * $bogo_cheapest_quantity_aditional; $discounted_price = $initial_price - $discounted_price; if(!empty($discounted_price)){ $discounted_price_with_tax_call = self::$calculator->mayHaveTax($product_obj, $discounted_price); } $price_suffix = Woocommerce::getProductPriceSuffix($product_obj, $discounted_price_with_tax_call); $price_html .= '' . self::$woocommerce_helper->formatPrice($original_price_with_tax_call) . ' ' . self::$woocommerce_helper->formatPrice($discounted_price_with_tax_call) .$price_suffix. ' x ' . $bogo_cheapest_quantity_aditional . '
'; $bogo_cheap_in_cart_total_discount_qty[] = $bogo_cheapest_quantity_aditional; } $discount_applied_qty = array_sum($bogo_cheap_in_cart_total_discount_qty); if($discount_applied_qty < $cart_item_qty){ $not_applied = $cart_item_qty - $discount_applied_qty; $price_suffix = Woocommerce::getProductPriceSuffix($product_obj, $original_price_with_tax_call); $price_html .= '' . self::$woocommerce_helper->formatPrice($original_price_with_tax_call) .$price_suffix. ' x ' . $not_applied . ''; } return $price_html; } /** * Calculate discount for product or calculate discount from custom price * @param $price * @param $product * @param int $quantity * @param int $custom_price * @param string $get_only * @return bool */ static function calculateProductDiscountPrice($price, $product, $quantity = 1, $custom_price = 0, $get_only = 'discounted_price', $manual_request = false, $is_cart = true) { if (!is_a($product, 'WC_Product')) { if (is_integer($product)) { $product = self::$woocommerce_helper->getProduct($product); } else { return false; } } if (!$product) { return false; } $discounts = self::$calculator->mayApplyPriceDiscount($product, $quantity, $custom_price, false, array(), $is_cart, $manual_request); if ($discounts) { switch ($get_only) { case 'all': $product_id = self::$woocommerce_helper->getProductId($product); if (isset($discounts['total_discount_details'][$product_id])) { $discounts['total_discount_details'] = $discounts['total_discount_details'][$product_id]; } if (isset($discounts['cart_discount_details'])) { unset($discounts['cart_discount_details']); } $price = $discounts; break; default: case 'discounted_price': $price = isset($discounts['discounted_price']) ? $discounts['discounted_price'] : $price; break; } } else { return false; } return $price; } /** * Change the default template for sale badge * * @param $located string * @param $template_name string * @param $args array * @param $template_path string * @param $default_path string * @return string * */ public static function changeTemplateForSaleTag($located, $template_name, $args, $template_path, $default_path){ if($template_name == 'single-product/sale-flash.php'){ $located = Helper::getTemplatePath('sale-flash.php', WDR_PLUGIN_PATH . 'App/Views/Templates/single-product/sale-flash.php', 'single-product'); } else if($template_name == 'loop/sale-flash.php'){ $located = Helper::getTemplatePath('sale-flash.php', WDR_PLUGIN_PATH . 'App/Views/Templates/loop/sale-flash.php', 'loop'); } return $located; } /** * Remove on sale flash except our hooks * */ function removeOnSaleFlashEvent(){ $allowed_hooks = array( //Filters "woocommerce_sale_flash" => array( "Wdr\App\Controllers\ManageDiscount|replaceSaleTagText" ), ); $this->removeOtherEvents($allowed_hooks); } /** * Suppress third party discount plugins from accessing the discount data */ function suppressOtherDiscountPlugins() { $allowed_hooks = array( //Filters "woocommerce_get_price_html" => array("Wdr\App\Controllers\ManageDiscount|getPriceHtml", "Wdr\App\Controllers\ManageDiscount|getPriceHtmlSalePriceAdjustment"), "woocommerce_product_is_on_sale" => array("Wdr\App\Controllers\ManageDiscount|isProductInSale"), "woocommerce_product_get_sale_price" => array(), "woocommerce_product_get_regular_price" => array(), "woocommerce_variable_price_html" => array("Wdr\App\Controllers\ManageDiscount|getVariablePriceHtml"), "woocommerce_cart_item_price" => array("Wdr\App\Controllers\ManageDiscount|getCartPriceHtml"), "woocommerce_cart_item_subtotal" => array("Wdr\App\Controllers\ManageDiscount|getCartProductSubtotalPriceHtml"), //Actions "woocommerce_checkout_order_processed" => array(), "woocommerce_before_calculate_totals" => array("Wdr\App\Controllers\ManageDiscount|applyCartProductDiscount"), //nothing allowed! ); $allowed_hooks = apply_filters('advanced_woo_discount_rules_exclude_hooks_from_removing', $allowed_hooks); $this->removeOtherEvents($allowed_hooks); } /** * Remove methods from the event * Exclude from the list * * @param $allowed_hooks array * */ function removeOtherEvents($allowed_hooks){ global $wp_filter; foreach ($wp_filter as $hook_name => $hook_obj) { if (preg_match('#^woocommerce_#', $hook_name)) { if (isset($allowed_hooks[$hook_name])) { $wp_filter[$hook_name] = $this->removeWrongCallbacks($hook_obj, $allowed_hooks[$hook_name]); } } } } /** * Remove the third party call backs * @param $hook_obj * @param $allowed_hooks * @return mixed */ function removeWrongCallbacks($hook_obj, $allowed_hooks) { $new_callbacks = array(); foreach ($hook_obj->callbacks as $priority => $callbacks) { $priority_callbacks = array(); foreach ($callbacks as $idx => $callback_details) { if ($this->isCallbackMatch($callback_details, $allowed_hooks)) { $priority_callbacks[$idx] = $callback_details; } } if ($priority_callbacks) { $new_callbacks[$priority] = $priority_callbacks; } } $hook_obj->callbacks = $new_callbacks; return $hook_obj; } /** * check the call back matched or not * @param $callback_details * @param $allowed_hooks * @return bool */ function isCallbackMatch($callback_details, $allowed_hooks) { $result = false; foreach ($allowed_hooks as $callback_name) { list($class_name, $func_name) = explode("|", $callback_name); if(empty($callback_details['function'])){ continue; } if(is_string($callback_details['function'])){ continue; } if(is_object($callback_details['function'])){ if($this->is_closure($callback_details['function'])){ continue; } } if (count($callback_details['function']) != 2) { continue; } if(!is_object($callback_details['function'][0])){ continue; } if ($class_name == get_class($callback_details['function'][0]) AND $func_name == $callback_details['function'][1]) { $result = true; break;// done! } } return $result; } function is_closure($t) { if(class_exists('\Closure')){ return $t instanceof \Closure; } else { return false; } } /** * Display you saved text in order pages * @param $total * @param $order * @return string */ function displayTotalSavingsInOrderAfterOrderTotal($total, $order) { if (!is_object($order)) { if (!empty($order) && is_int($order)) { $order = self::$woocommerce_helper->getOrder($order); } } $items = $order->get_items(); $total_discount = $this->getItemTotalDiscount($items); $save_text = NULL; if (!empty($total_discount)) { $total_discounted_price = self::$woocommerce_helper->formatPrice($total_discount, array('currency' => self::$woocommerce_helper->getOrderCurrency($order))); $subtotal_additional_text = $this->getYouSavedText($total_discounted_price); $save_text = apply_filters('advanced_woo_discount_rules_order_saved_text', $subtotal_additional_text, $total_discounted_price, $total_discount); } return $total . $save_text; } /** * Calculate items total discount * @param $items * @return float|int */ function getItemTotalDiscount($items) { $total_discount = 0; if (!empty($items)) { foreach ($items as $key => $item) { $discount_details = $item->get_meta('_advanced_woo_discount_item_total_discount'); if (!empty($discount_details)) { $total_discount += $this->getDiscountPerItem($discount_details); } } } return $total_discount; } /** * order line item saved text * @param $subtotal * @param $item * @param $order * @return string */ function orderSubTotalDiscountDetails($subtotal, $item, $order) { $discount_details = self::$woocommerce_helper->getOrderItemMeta($item, '_advanced_woo_discount_item_total_discount'); if (!empty($discount_details)) { $total_discount = $this->getDiscountPerItem($discount_details); if (!empty($total_discount)) { $total_discounted_price = self::$woocommerce_helper->formatPrice($total_discount, array('currency' => self::$woocommerce_helper->getOrderCurrency($order))); $subtotal_additional_text = $this->getYouSavedText($total_discounted_price); $subtotal .= apply_filters('advanced_woo_discount_rules_order_saved_text', $subtotal_additional_text, $total_discounted_price, $total_discount); } } return $subtotal; } /** * Show order discount details * @param $item_id * @param $item * @param $order */ public function orderItemMetaDiscountDetails($item_id, $item, $order) { $discount_details = self::$woocommerce_helper->getOrderItemMeta($item, '_advanced_woo_discount_item_total_discount'); if (!empty($discount_details)) { $total_discount = $this->getDiscountPerItem($discount_details); if (!empty($total_discount)) { $total_discounted_price = self::$woocommerce_helper->formatPrice($total_discount, array('currency' => self::$woocommerce_helper->getOrderCurrency($order))); $subtotal_additional_text = $this->getYouSavedText($total_discounted_price); echo apply_filters('advanced_woo_discount_rules_order_saved_text', $subtotal_additional_text, $total_discounted_price, $total_discount); } } } /** * Hide Zero Coupon Value * @param $coupon_html * @param $coupon * @return mixed */ public function hideZeroCouponValue($coupon_html, $coupon) { $hide_zero_value_coupon = apply_filters('advanced_woo_discount_rules_hide_zero_value_coupon', true, $coupon); if ($hide_zero_value_coupon) { $rule_helper = new Rule(); $original_coupon_html = $coupon_html; $all_coupon_codes = $rule_helper->getCouponsFromDiscountRules(); $coupon_code = self::$woocommerce_helper->getCouponCode($coupon); $virtual_coupon = (isset($all_coupon_codes['custom_coupons']) && !empty($all_coupon_codes['custom_coupons'])) ? $all_coupon_codes['custom_coupons'] : array(); $woo_coupons = (isset($all_coupon_codes['woo_coupons'][0]) && !empty($all_coupon_codes['woo_coupons'][0])) ? $all_coupon_codes['woo_coupons'][0] : array(); if (!empty($woo_coupons) && in_array($coupon_code, $woo_coupons)) { $zero_price_html = '-' . self::$woocommerce_helper->formatPrice(0); $coupon_html = str_replace($zero_price_html, '', $coupon_html); } else if (!empty($virtual_coupon) && in_array($coupon_code, $virtual_coupon)) { $zero_price_html = '-' . self::$woocommerce_helper->formatPrice(0); $coupon_html = str_replace($zero_price_html, '', $coupon_html); } $coupon_html = apply_filters('advanced_woo_discount_rules_hide_zero_value_coupon_html', $coupon_html, $original_coupon_html, $coupon); } return $coupon_html; } /** * Display promotional messages * */ public function displayPromotionMessages(){ $messages = Helper::getPromotionMessages(); if(!empty($messages) && is_array($messages)) { foreach ($messages as $message) { wc_print_notice($message, "notice"); } } } /** * Load outer div for displaying promotional message in check out * */ public function displaySubTotalPromotionMessagesInCheckoutContainer(){ echo "
"; echo "
"; } /** * Display promotional message in check out while processing order review * */ public function displaySubTotalPromotionMessagesInCheckout(){ echo "
"; $this->displayPromotionMessages(); echo "
"; echo ""; } /** * Export Data via CSV */ public function awdrExportCsv(){ if (isset($_POST['wdr-export']) && isset($_POST['security'])) { if(wp_verify_nonce($_POST['security'],'awdr_export_rules')) { ob_end_clean(); $rule_helper = new Rule(); $rules = $rule_helper->exportRuleByName('all'); $file_name = 'advanced-discount-rules-' . date("Y-m-d-h-i-a") . '.csv'; $file = fopen('php://output', 'w'); header('Content-type: application/csv'); header('Content-Disposition: attachment; filename=' . $file_name); $export_csv_separator = apply_filters('advanced_woo_discount_rules_csv_import_export_separator', ','); fputcsv($file, array('id', 'enabled', 'deleted', 'exclusive', 'title', 'priority', 'apply_to', 'filters', 'conditions', 'product_adjustments', 'cart_adjustments', 'buy_x_get_x_adjustments', 'buy_x_get_y_adjustments', 'bulk_adjustments', 'set_adjustments', 'other_discounts', 'date_from', 'date_to', 'usage_limits', 'rule_language', 'used_limits', 'additional', 'max_discount_sum', 'advanced_discount_message', 'discount_type', 'used_coupons', 'created_by', 'created_on', 'modified_by', 'modified_on'), $export_csv_separator); foreach ($rules as $rule_row) { $row_data = (array)$rule_row; fputcsv($file, $row_data, $export_csv_separator); } exit; } } } /** * Display total savings in order * @param $order */ public function displayTotalSavingsThroughDiscountInOrder($order){ if (!is_object($order)) { if (!empty($order) && is_int($order)) { $order = self::$woocommerce_helper->getOrder($order); } } $items = $order->get_items(); $total_discount = $this->getItemTotalDiscount($items); $save_text = NULL; if (!empty($total_discount)) { $total_discounted_price = self::$woocommerce_helper->formatPrice($total_discount, array('currency' => self::$woocommerce_helper->getOrderCurrency($order))); $subtotal_additional_text = $this->getYouSavedText($total_discounted_price); $save_text = apply_filters('advanced_woo_discount_rules_order_saved_text', $subtotal_additional_text, $total_discounted_price, $total_discount); } echo $save_text; } /** * Include tax in fee * */ public function applyTaxInFee($fee, $cart){ if(Woocommerce::isTaxEnabled()){ if(Woocommerce::isEnteredPriceIncludeTax()){ if(class_exists('\WC_Tax')){ $fee_taxs = \WC_Tax::calc_tax( $fee, \WC_Tax::get_rates( '', \WC()->cart->get_customer() ), true ); if(!empty($fee_taxs)){ foreach ($fee_taxs as $key => $val){ $fee = $fee - $val; } } } } } return $fee; } } Vordpres › Greška

Postoji kritična greška na vašem veb mestu.

Saznajte više o otklanjanju grešaka Vordpresu.