types = Array( 'percent_total_amount' => __('Percentage of cart total amount', 'aco-woo-dynamic-pricing'), 'percent_product_price' => __('Percentage of product price', 'aco-woo-dynamic-pricing'), 'fixed_product_price' => __('Fixed price of product price', 'aco-woo-dynamic-pricing'), 'fixed_cart_amount' => __('Fixed price of cart total amount', 'aco-woo-dynamic-pricing'), 'cart_quantity' => __('Quantity based discount', 'aco-woo-dynamic-pricing') ); } /** * * Ensures only one instance of AWDP is loaded or can be loaded. * * @since 1.0.0 * @static * @see WordPress_Plugin_Template() * @return Main AWDP instance */ public static function instance($file = '', $version = '1.0.0') { if (is_null(self::$_instance)) { self::$_instance = new self($file, $version); } return self::$_instance; } /** * @return bool */ public function isActive() { return $this->_active; } // Cart Price View public function cart_discount_items ( $item_price, $cart_item ) { $prod_ID = $cart_item['data']->get_data()['slug']; $cart_prod_id = ( $cart_item['variation_id'] == 0 || $cart_item['variation_id'] == '' ) ? $cart_item['product_id'] : $cart_item['variation_id']; $QNitems = $this->wdpQNitems; if( $this->converted_rate == '' && $cart_item['data']->get_ID() != '' && array_key_exists ( $prod_ID, $this->wdp_discounted_price ) ) { $this->converted_rate = $this->get_con_unit($cart_item['data'], wc_remove_number_precision($this->wdp_discounted_price[$prod_ID])); } if ($this->discount_rules != false && sizeof($this->discount_rules) >= 1 && $this->check_discount($prod_ID) && false == $this->awdp_cart_rules) { $discounted_price = abs(wc_remove_number_precision($this->wdp_discounted_price[$prod_ID])); $product_actual_price = isset ( $this->actual_price ) ? ( ( array_key_exists($prod_ID, $this->actual_price) && $this->actual_price[$prod_ID] > 0 ) ? $this->actual_price[$prod_ID] : ( ( $cart_item['data']->get_data()['price'] == 0 && isset($cart_item['data']->price) ) ? $cart_item['data']->price : $cart_item['data']->get_data()['price'] ) ) : ( ( $cart_item['data']->get_data()['price'] == 0 && isset($cart_item['data']->price) ) ? $cart_item['data']->price : $cart_item['data']->get_data()['price'] ); $quantity = 1; $product = wc_get_product( $cart_prod_id ); if ($product_actual_price > 0) { if (WC()->cart->display_prices_including_tax()) { $product_actual_price = wc_get_price_including_tax($product, array( 'qty' => $quantity, 'price' => $product_actual_price, )); if( $discounted_price > 0.0000000001 ) { $discounted_price = wc_get_price_including_tax($product, array( 'qty' => $quantity, 'price' => $discounted_price, )); } } else { $product_actual_price = wc_get_price_excluding_tax($product, array( 'qty' => $quantity, 'price' => $product_actual_price )); if( $discounted_price > 0.0000000001 ) { $discounted_price = wc_get_price_excluding_tax($product, array( 'qty' => $quantity, 'price' => $discounted_price, )); } } } else { $product_actual_price = $product_actual_price; $discounted_price = $discounted_price; } if ( ( sizeof($QNitems) > 0 ) && array_search ( $cart_prod_id, array_column ( $QNitems, 'product_id' ) ) !== false ) { $qn_index = array_search ( $cart_prod_id, array_column ( $QNitems, 'product_id' ) ); $qn_price = wc_remove_number_precision ( $QNitems[$qn_index]['discounted_price'] ); if ($qn_price > 0) { if (WC()->cart->display_prices_including_tax()) { $qn_price = wc_get_price_including_tax($product, array( 'qty' => $quantity, 'price' => $qn_price, )); } else { $qn_price = wc_get_price_excluding_tax($product, array( 'qty' => $quantity, 'price' => $qn_price )); } } else { $qn_price = $qn_price; } if( ( $discounted_price > 0.0000000001 ) && ( $discounted_price < $product_actual_price ) ) { $item_price = wc_format_sale_price($discounted_price * $this->converted_rate, $qn_price * $this->converted_rate ); } else { $item_price = wc_format_sale_price($product_actual_price * $this->converted_rate, $qn_price * $this->converted_rate ); } } else if (($discounted_price > 0.0000000001 && ($discounted_price < $product_actual_price)) || $discounted_price == 0) { $item_price = wc_format_sale_price($product_actual_price * $this->converted_rate, $discounted_price * $this->converted_rate); } else if ($discounted_price > $product_actual_price) { $item_price = wc_format_sale_price($product_actual_price * $this->converted_rate, $discounted_price * $this->converted_rate); } } return $item_price; } // Price View HTML public function get_product_price_html($item_price, $product) { $prod_ID = $product->get_data()['slug']; if( $this->converted_rate == '' && $product->get_ID() != '' && array_key_exists ( $prod_ID, $this->wdp_discounted_price ) ) { $this->converted_rate = $this->get_con_unit($product, wc_remove_number_precision($this->wdp_discounted_price[$prod_ID])); } // Variable Product if( $product->is_type( 'variable' ) ) { // Price display for variable product $variations = $product->get_available_variations(); if($variations) { if(array_key_exists($prod_ID, $this->variations) && $this->variations[$prod_ID]){ $wdp_max_price = $product->get_variation_price( 'max', true ); $wdp_min_price = $product->get_variation_price( 'min', true ); $wdp_dsc_max_price = max($this->variations[$prod_ID]); $wdp_dsc_min_price = min($this->variations[$prod_ID]); if( $wdp_min_price == $wdp_dsc_min_price && $wdp_max_price == $wdp_dsc_max_price ) { return $item_price; } else { if ( $wdp_dsc_max_price !== $wdp_dsc_min_price ) { $item_price = '
'.wc_format_price_range( $wdp_min_price, $wdp_max_price ).' '.wc_format_price_range( $wdp_dsc_min_price, $wdp_dsc_max_price ).'
| ' . $awdp_qn_label . '*' . ' | '; $tr_pr = '||||||||||||||||||||||||||||||||||
| ' . $awdp_pc_label . ' | '; } else { $table = '
| ' . $awdp_qn_label . '*' . ' | ' . $awdp_pc_label . ' | ' . $quantity_rule['start_range'] . ' | '; } else if ($quantity_rule['end_range']) { $tr_qn .= '' . $quantity_rule['start_range'] . ' - ' . $quantity_rule['end_range'] . ' | '; } else { $tr_qn .= '' . $quantity_rule['start_range'] . ' + | '; } if ( $value_display == 'discount_value' ) { if ($discount_typ == 'percentage') { $tr_pr .= '' .(float)$discount_val . '% OFF | '; } else if ($discount_typ == 'fixed') { $tr_pr .= '' .(float)$discount_val . ' OFF | '; } } else { if ($discounted_new_min_price == $discounted_new_max_price) { $tr_pr .= '' . $discounted_new_min_price . ' | '; } else { $tr_pr .= '' . $discounted_new_min_price . ' - ' . $discounted_new_max_price . ' | '; } } } else { if ($quantity_rule['start_range'] == $quantity_rule['end_range']) { $table .= '
| ' . $quantity_rule['start_range'] . ' | '; if ( $value_display == 'discount_value' ) { if ($discount_typ == 'percentage') { $table .= '' .(float)$discount_val . '% OFF | '; } else if ($discount_typ == 'fixed') { $table .= '' .(float)$discount_val . ' OFF | '; } } else { if ($discounted_new_min_price == $discounted_new_max_price) { $table .= '' . $discounted_new_min_price . ' | '; } else { $table .= '' . $discounted_new_min_price . ' - ' . $discounted_new_max_price . ' | '; } } $table .= '
| ' . $quantity_rule['start_range'] . ' - ' . $quantity_rule['end_range'] . ' | '; if ( $value_display == 'discount_value' ) { if ($discount_typ == 'percentage') { $table .= '' .(float)$discount_val . '% OFF | '; } else if ($discount_typ == 'fixed') { $table .= '' .(float)$discount_val . ' OFF | '; } } else { if ($discounted_new_min_price == $discounted_new_max_price) { $table .= '' . $discounted_new_min_price . ' | '; } else { $table .= '' . $discounted_new_min_price . ' - ' . $discounted_new_max_price . ' | '; } } $table .= '
| ' . $quantity_rule['start_range'] . ' + | '; if ( $value_display == 'discount_value' ) { if ($discount_typ == 'percentage') { $table .= '' .(float)$discount_val . '% OFF | '; } else if ($discount_typ == 'fixed') { $table .= '' .(float)$discount_val . ' OFF | '; } } else { if ($discounted_new_min_price == $discounted_new_max_price) { $table .= '' . $discounted_new_min_price . ' | '; } else { $table .= '' . $discounted_new_min_price . ' - ' . $discounted_new_max_price . ' | '; } } $table .= '' . $quantity_rule['start_range'] . ' | '; } else if ($quantity_rule['end_range']) { $tr_qn .= '' . $quantity_rule['start_range'] . ' - ' . $quantity_rule['end_range'] . ' | '; } else { $tr_qn .= '' . $quantity_rule['start_range'] . ' + | '; } $tr_pr .= '' . $discounted_new_price . ' | '; } else { if ($quantity_rule['start_range'] == $quantity_rule['end_range']) { $table .= '
| ' . $quantity_rule['start_range'] . ' | ' . $discounted_new_price . ' | |||
| ' . $quantity_rule['start_range'] . ' - ' . $quantity_rule['end_range'] . ' | ' . $discounted_new_price . ' | |||
| ' . $quantity_rule['start_range'] . ' + | ' . $discounted_new_price . ' |
*'.$discount_description.'
' : '*'.$awdp_qn_label.' refers to discounted items (products with discount) count on cart.
| ' . $awdp_qn_label . ' | '; $tr_pr = '||||||||||||||||||||||||||||||||||
| ' . $awdp_pc_label . ' | '; } else { $table = '
| ' . $awdp_qn_label . ' | ' . $awdp_pc_label . ' | ' . $quantity_rule['start_range'] . ' | '; } else if ($quantity_rule['end_range']) { $tr_qn .= '' . $quantity_rule['start_range'] . ' - ' . $quantity_rule['end_range'] . ' | '; } else { $tr_qn .= '' . $quantity_rule['start_range'] . ' + | '; } if ( $value_display == 'discount_value' ) { if ($discount_typ == 'percentage') { $tr_pr .= '' .(float)$discount_val . '% OFF | '; } else if ($discount_typ == 'fixed') { $tr_pr .= '' .(float)$discount_val . ' OFF | '; } } else { if ($discounted_new_min_price == $discounted_new_max_price) { $tr_pr .= '' . $discounted_new_min_price . ' | '; } else { $tr_pr .= '' . $discounted_new_min_price . ' - ' . $discounted_new_max_price . ' | '; } } } else { if ($quantity_rule['start_range'] == $quantity_rule['end_range']) { $table .= '
| ' . $quantity_rule['start_range'] . ' | '; if ( $value_display == 'discount_value' ) { if ($discount_typ == 'percentage') { $table .= '' .(float)$discount_val . '% OFF | '; } else if ($discount_typ == 'fixed') { $table .= '' .(float)$discount_val . ' OFF | '; } } else { if ($discounted_new_min_price == $discounted_new_max_price) { $table .= '' . $discounted_new_min_price . ' | '; } else { $table .= '' . $discounted_new_min_price . ' - ' . $discounted_new_max_price . ' | '; } } $table .= '
| ' . $quantity_rule['start_range'] . ' - ' . $quantity_rule['end_range'] . ' | '; if ( $value_display == 'discount_value' ) { if ($discount_typ == 'percentage') { $table .= '' .(float)$discount_val . '% OFF | '; } else if ($discount_typ == 'fixed') { $table .= '' .(float)$discount_val . ' OFF | '; } } else { if ($discounted_new_min_price == $discounted_new_max_price) { $table .= '' . $discounted_new_min_price . ' | '; } else { $table .= '' . $discounted_new_min_price . ' - ' . $discounted_new_max_price . ' | '; } } $table .= '
| ' . $quantity_rule['start_range'] . ' + | '; if ( $value_display == 'discount_value' ) { if ($discount_typ == 'percentage') { $table .= '' .(float)$discount_val . '% OFF | '; } else if ($discount_typ == 'fixed') { $table .= '' .(float)$discount_val . ' OFF | '; } } else { if ($discounted_new_min_price == $discounted_new_max_price) { $table .= '' . $discounted_new_min_price . ' | '; } else { $table .= '' . $discounted_new_min_price . ' - ' . $discounted_new_max_price . ' | '; } } $table .= '' . $quantity_rule['start_range'] . ' | '; } else if ($quantity_rule['end_range']) { $tr_qn .= '' . $quantity_rule['start_range'] . ' - ' . $quantity_rule['end_range'] . ' | '; } else { $tr_qn .= '' . $quantity_rule['start_range'] . ' + | '; } $tr_pr .= '' . $discounted_new_price . ' | '; } else { if ($quantity_rule['start_range'] == $quantity_rule['end_range']) { $table .= '
| ' . $quantity_rule['start_range'] . ' | ' . $discounted_new_price . ' | |||
| ' . $quantity_rule['start_range'] . ' - ' . $quantity_rule['end_range'] . ' | ' . $discounted_new_price . ' | |||
| ' . $quantity_rule['start_range'] . ' + | ' . $discounted_new_price . ' |
*'.$discount_description.'
' : '*'.$awdp_qn_label.' refers to discounted items (products with discount) count on cart.
'; // Pricinig Table if ($rule['pricing_table'] == 1) $this->pricing_table[$item->get_id()][$rule['id']] = $table; // Discount if ($cart_total > 0) { $this->wdpCartDicount[$rule['id']] = (float)$discount_amt; $this->wdpCartDiscountValues[$rule['id']]['type'] = $disc_act_type; $this->wdpCartDiscountValues[$rule['id']]['calc'] = $disc_calc_type; $this->wdpCartDiscountValues[$rule['id']]['quantity'] = $wdp_cart_quantity; $this->wdpCartDiscountValues[$rule['id']]['items_count'] = $wdp_cart_items; $this->wdpCartDiscountValues[$rule['id']]['value'] = $disc_act_val; $this->wdpCartDiscountValues[$rule['id']]['products'] = $wdp_applicable_ids; } // End Total Cart Quantity /////////////////////////////////////////////////////////////// } else if ( $quantity_type == 'type_product' ) { // Product Quantity Discount if (WC()->cart) { $cart_contents = WC()->cart->get_cart(); } else { $cart_contents = []; } $prod_QNT = []; $item_id = $item->get_ID(); if ($cart_contents) { foreach ($cart_contents as $cart_content) { $cart_id = $cart_content['data']->get_ID(); if ($item_id == $cart_id) $prod_QNT[$cart_content['data']->get_data()['slug']] = $cart_content['quantity']; } } $awdp_pc_title = get_option('awdp_pc_title') ? get_option('awdp_pc_title') : __("Quantity Discounts", "aco-woo-dynamic-pricing"); $awdp_pc_label = get_option('awdp_pc_label') ? get_option('awdp_pc_label') : __("Price", "aco-woo-dynamic-pricing"); $awdp_qn_label = get_option('awdp_qn_label') ? get_option('awdp_qn_label') : __("Quantity", "aco-woo-dynamic-pricing"); if ($table_layout == 'horizontal') { $table = '| ' . $awdp_qn_label . ' | '; $tr_pr = '||||||||||||||||||||||||||||||||||
| ' . $awdp_pc_label . ' | '; } else { $table = '
| ' . $awdp_qn_label . ' | ' . $awdp_pc_label . ' | ' . $quantity_rule['start_range'] . ' | '; } else if ($quantity_rule['end_range']) { $tr_qn .= '' . $quantity_rule['start_range'] . ' - ' . $quantity_rule['end_range'] . ' | '; } else { $tr_qn .= '' . $quantity_rule['start_range'] . ' + | '; } if ( $value_display == 'discount_value' ) { if ($discount_typ == 'percentage') { $tr_pr .= '' .(float)$discount_val . '% OFF | '; } else if ($discount_typ == 'fixed') { $tr_pr .= '' .(float)$discount_val . ' OFF | '; } } else { if ($discounted_new_min_price == $discounted_new_max_price) { $tr_pr .= '' . $discounted_new_min_price . ' | '; } else { $tr_pr .= '' . $discounted_new_min_price . ' - ' . $discounted_new_max_price . ' | '; } } } else { if ($quantity_rule['start_range'] == $quantity_rule['end_range']) { $table .= '
| ' . $quantity_rule['start_range'] . ' | '; if ( $value_display == 'discount_value' ) { if ($discount_typ == 'percentage') { $table .= '' .(float)$discount_val . '% OFF | '; } else if ($discount_typ == 'fixed') { $table .= '' .(float)$discount_val . ' OFF | '; } } else { if ($discounted_new_min_price == $discounted_new_max_price) { $table .= '' . $discounted_new_min_price . ' | '; } else { $table .= '' . $discounted_new_min_price . ' - ' . $discounted_new_max_price . ' | '; } } $table .= '
| ' . $quantity_rule['start_range'] . ' - ' . $quantity_rule['end_range'] . ' | '; if ( $value_display == 'discount_value' ) { if ($discount_typ == 'percentage') { $table .= '' .(float)$discount_val . '% OFF | '; } else if ($discount_typ == 'fixed') { $table .= '' .(float)$discount_val . ' OFF | '; } } else { if ($discounted_new_min_price == $discounted_new_max_price) { $table .= '' . $discounted_new_min_price . ' | '; } else { $table .= '' . $discounted_new_min_price . ' - ' . $discounted_new_max_price . ' | '; } } $table .= '
| ' . $quantity_rule['start_range'] . ' + | '; if ( $value_display == 'discount_value' ) { if ($discount_typ == 'percentage') { $table .= '' .(float)$discount_val . '% OFF | '; } else if ($discount_typ == 'fixed') { $table .= '' .(float)$discount_val . ' OFF | '; } } else { if ($discounted_new_min_price == $discounted_new_max_price) { $table .= '' . $discounted_new_min_price . ' | '; } else { $table .= '' . $discounted_new_min_price . ' - ' . $discounted_new_max_price . ' | '; } } $table .= '' . $quantity_rule['start_range'] . ' | '; } else if ($quantity_rule['end_range']) { $tr_qn .= '' . $quantity_rule['start_range'] . ' - ' . $quantity_rule['end_range'] . ' | '; } else { $tr_qn .= '' . $quantity_rule['start_range'] . ' + | '; } $tr_pr .= '' . $discounted_new_price . ' | '; } else { if ($quantity_rule['start_range'] == $quantity_rule['end_range']) { $table .= '
| ' . $quantity_rule['start_range'] . ' | ' . $discounted_new_price . ' | |||
| ' . $quantity_rule['start_range'] . ' - ' . $quantity_rule['end_range'] . ' | ' . $discounted_new_price . ' | |||
| ' . $quantity_rule['start_range'] . ' + | ' . $discounted_new_price . ' |