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])); $actual_price = isset ($this->actual_price) ? (array_key_exists($prod_ID, $this->actual_price) ? $this->actual_price[$prod_ID] : $cart_item['data']->get_data()['price']) : $cart_item['data']->get_data()['price']; $quantity = 1; $product = wc_get_product( $cart_prod_id ); if ($actual_price > 0) { if (WC()->cart->display_prices_including_tax()) { $actual_price = wc_get_price_including_tax($product, array( 'qty' => $quantity, 'price' => $actual_price, )); if( $discounted_price > 0.0000000001 ) { $discounted_price = wc_get_price_including_tax($product, array( 'qty' => $quantity, 'price' => $discounted_price, )); } } else { $actual_price = wc_get_price_excluding_tax($product, array( 'qty' => $quantity, 'price' => $actual_price )); if( $discounted_price > 0.0000000001 ) { $discounted_price = wc_get_price_excluding_tax($product, array( 'qty' => $quantity, 'price' => $discounted_price, )); } } } else { $actual_price = $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 < $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($actual_price * $this->converted_rate, $qn_price * $this->converted_rate ); } } else if (($discounted_price > 0.0000000001 && ($discounted_price < $actual_price)) || $discounted_price == 0) { $item_price = wc_format_sale_price($actual_price * $this->converted_rate, $discounted_price * $this->converted_rate); } else if ($discounted_price > $actual_price) { $item_price = wc_format_sale_price($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')) { // $variations = $product->get_available_variations(); // if($variations) { // $wdp_max_price = $product->get_variation_price( 'max', true ); // $wdp_min_price = $product->get_variation_price( 'min', true ); // $variation_prices = []; // $regular_prices = []; // foreach($variations as $variation) { // array_push($variation_prices, $variation['display_price']); // } // $min_price = min($variation_prices); // $max_price = max($variation_prices); // if( $min_price == $wdp_min_price && $max_price == $wdp_max_price ) { // return $item_price; // } else { // if ( $min_price !== $max_price ) { // // $item_price = wc_format_price_range( $min_price * $this->converted_rate, $max_price * $this->converted_rate ); // $item_price = '
'.wc_format_price_range( $wdp_min_price, $wdp_max_price ).' '.wc_format_price_range( $min_price , $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 . ' |