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;
}
/*
* @change get_price to woocommerce_before_calculate_totals
* @since 4.0
* @fix compatibility and quantity discount issues
*/
public function wdpCalculateDiscount ( $cartObject ) {
if ( $cartObject ) {
$cartContents = $cartObject->cart_contents;
$result = [];
// $couponStatus = get_option('awdp_apply_coupon_discount') ? get_option('awdp_apply_coupon_discount') : false;
$couponStatus = false;
// Disable Discount if any other gets added to the cart
$disable_discount = get_option('awdp_disable_discount') ? get_option('awdp_disable_discount') : '';
$coupon = get_option('awdp_fee_label') ? get_option('awdp_fee_label') : 'Discount';
$coupon_code = apply_filters('woocommerce_coupon_code', $coupon);
if ( $disable_discount && !empty ( WC()->cart->get_applied_coupons() ) && !in_array ( $coupon_code, WC()->cart->get_applied_coupons() ) ) {
return $cartObject;
}
// Load discount rules
$this->load_rules();
$this->apply_wdp_coupon = true;
// Check if discount is active
if ( $this->discount_rules == null )
return $cartObject; // Exit if no rules
// CartContents Loop
foreach ( $cartContents as $cartContent ) {
$prod_ID = $cartContent['product_id'];
$product = wc_get_product ( $prod_ID );
// $product_slug = $product->get_data()['slug'];
$product_slug = $cartContent['data']->get_slug();
$quantity = $cartContent['quantity'];
$variationID = $cartContent['variation_id'];
$variations = $cartContent['variation'];
$disc_prod_ID = $variationID == 0 ? $prod_ID : $variationID;
// Changing Product Slug to cart key - addons compatibility
$cartKey = $cartContent['key'];
// Get Cart Price
$cartItemPrice = $cartContent['data']->get_price();
// Checking for addons price
$addonPrice = apply_filters('wcpa_cart_addon_data', false, $cartContent);
$dispPrice = $addonPrice ? ( $addonPrice['totalPrice'] - $addonPrice['excludeFromDiscount'] ) : $cartItemPrice;
// $product_price1 = apply_filters('advanced_woo_discount_rules_product_price_on_before_calculate_discount', $product_price, $product, $quantity, $cart_item, $calculate_discount_from);
/*
* ver @ 4.3.3
* Get conversion rate
* Remove conversion from the coupon total
*/
$this->converted_rate = $this->get_con_unit($product, $cartContent['data']->get_price());
foreach ( $this->discount_rules as $k => $rule ) {
// Disable Discount for Deposit Items
$depositCheck = get_post_meta($rule['id'], 'deposit_check', true) ? get_post_meta($rule['id'], 'deposit_check', true) : 0;
if ( array_key_exists ( 'awcdp_deposit', $cartContent ) && $cartContent['awcdp_deposit'] && $depositCheck ) {
continue;
}
// Get Product List
if ( !$this->get_items_to_apply_discount ( $product, $rule, $disc_prod_ID, false, $product_slug ) ) {
continue;
}
// Check if User if Logged-In
if ( ( intval ( $rule['discount_reg_customers'] ) === 1 && !is_user_logged_in() ) || ( intval ( $rule['discount_reg_customers'] ) === 1 && is_user_logged_in() && ( !empty ( array_filter ( $rule['discount_reg_user_roles'] ) ) && empty ( array_intersect ( $rule['discount_cur_user_roles'], $rule['discount_reg_user_roles'] ) ) ) ) ) {
continue;
}
// Validate Rules
if( 'cart_quantity' != $rule['type'] ) { // Skipping cart_quantity rule //
if ( !$this->validate_discount_rules( $product, $rule, ['product_price','cart_total_amount', 'cart_total_amount_all_prods', 'cart_items', 'cart_items_all_prods', 'cart_products', 'cart_products_list'], $cartContent ) ) {
continue;
}
}
// Discounts Default Values
if ( !isset ( $this->discounts[$rule['id']] ) ) {
$this->discounts[$rule['id']] = [ 'label' => $rule['label'], 'discount_type' => $rule['type'], 'discount_remainder' => -1, 'taxable' => false ];
}
// Saving Actual Price (Cart View - Coupon Disabled)
// if ( $couponStatus && array_key_exists ( $product_slug, $this->actual_price ) == false ) {
// $this->actual_price[$product_slug] = $cartItemPrice;
// }
// Get Price
// $price = !empty ($this->wdp_discounted_price) ? ( ( array_key_exists ( $cartKey, $this->wdp_discounted_price ) && $this->wdp_discounted_price[$cartKey] != '' ) ? wc_remove_number_precision ( $this->wdp_discounted_price[$cartKey] ) : $cartContent['data']->get_price() ) : $cartContent['data']->get_price();
$price = !empty ($this->wdp_discounted_price) ? ( ( array_key_exists ( $cartKey, $this->wdp_discounted_price ) && $this->wdp_discounted_price[$cartKey] != '' ) ? wc_remove_number_precision ( $this->wdp_discounted_price[$cartKey] ) : $cartItemPrice ) : $cartItemPrice;
$discVariable = $this->discounts[$rule['id']];
$prodLists = $this->product_lists;
// Disable Double Discount
if ( array_key_exists ( 'discounts', $this->discounts[$rule['id']] ) && array_key_exists ( $cartKey, $this->discounts[$rule['id']]['discounts'] ) ) {
continue;
}
// Discount Types
if ( 'percent_product_price' == $rule['type'] )
$result = call_user_func_array (
array ( new AWDP_typeProductPrice(), 'apply_discount_percent_product_price' ),
array ( $rule, $product, $price, $quantity, $discVariable, $cartContent, $disc_prod_ID, $prodLists, $dispPrice, $couponStatus )
);
else if ( 'fixed_product_price' == $rule['type'] )
$result = call_user_func_array (
array ( new AWDP_typeProductPrice(), 'apply_discount_fixed_product_price' ),
array ( $rule, $product, $price, $quantity, $discVariable, $cartContent, $disc_prod_ID, $prodLists, $dispPrice, $couponStatus )
);
else if ( 'percent_total_amount' == $rule['type'] )
$result = call_user_func_array (
array ( new AWDP_typeTotalAmount(), 'apply_discount_percent_total_amount' ),
array ( $rule, $product, $price, $quantity, $discVariable, $cartContent, $disc_prod_ID, $dispPrice, $couponStatus )
);
else if ( 'fixed_cart_amount' == $rule['type'] )
$result = call_user_func_array (
array ( new AWDP_typeTotalAmount(), 'apply_discount_fixed_price_total_amount' ),
array ( $rule, $product, $price, $quantity, $discVariable, $cartContent, $disc_prod_ID, $dispPrice, $couponStatus )
);
else if ( 'cart_quantity' == $rule['type'] )
$result = call_user_func_array (
array ( new AWDP_typeCartQuantity(), 'apply_discount_cart_quantity' ),
array ( $rule, $product, $price, $quantity, $discVariable, $prodLists, $cartContents, $cartContent, $disc_prod_ID, $dispPrice, $couponStatus )
);
if ( !empty($result) ) {
$this->discounts[$rule['id']] = $result['productDiscount'];
$this->discounted_products[] = $cartKey;
$this->wdp_discounted_price[$cartKey] = array_key_exists ( 'discountedprice', $result ) ? $result['discountedprice'] : ( array_key_exists ( $cartKey, $this->wdp_discounted_price ) ? $this->wdp_discounted_price[$cartKey] : '' );
// Set Cart Item Price
// if ( $couponStatus ) {
// $cartContent['data']->set_price(wc_remove_number_precision($this->wdp_discounted_price[$product_slug]));
// }
// Order Meta
$coupon = get_option('awdp_fee_label') ? get_option('awdp_fee_label') : 'Discount';
$coupon_code = apply_filters('woocommerce_coupon_code', $coupon);
$orderMetaData = [];
$orderMetaData['type'] = $rule['type'];
$orderMetaData['discount'] = $result['productDiscount'];
$orderMetaData['coupon'] = $coupon_code;
$orderMetaData['discountedPrice'] = array_key_exists ( 'discountedprice', $result ) ? $result['discountedprice'] : ( array_key_exists ( $cartKey, $this->wdp_discounted_price ) ? $this->wdp_discounted_price[$cartKey] : '' );
$this->wdp_order_meta[$cartKey][] = $orderMetaData;
}
}
}
}
}
// Show Pricing Table
public function show_pricing_table(){
// Load discount rules
$this->load_rules();
// Check if discount is active
if ( $this->discount_rules == null )
return ''; // Exit if no rules
// Load Product List
$this->set_product_list();
global $product;
$post_id = $product->get_id();
$product = wc_get_product( $post_id );
// Divi Theme Page Builder Loading issue - Fix
if(!$product){
return;
}
/*
* ver @ 4.3.3
* Tax Settings
*/
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$cartPrice = $product->get_sale_price() ? $product->get_sale_price() : $product->get_price();
$priceIncTax = ( 'incl' === $tax_display_mode ) ? wc_get_price_including_tax( $product, array ( 'price' => $cartPrice ) ) : wc_get_price_excluding_tax( $product, array ( 'price' => $cartPrice ) );
$rules = $this->discount_rules;
$price = $priceIncTax ? $priceIncTax : $cartPrice;
$prodLists = $this->product_lists;
$variations = $this->variations;
$discountedPrice = $this->discountProductPrice;
$discountProductMaxPrice = $this->discountProductMaxPrice;
$discountProductMinPrice = $this->discountProductMinPrice;
// if( $this->converted_rate == '' && $item->get_ID() != '' ) {
// $this->converted_rate = $this->get_con_unit($item, $price, true);
// }
if ( $price == '' || $price == 0 ) return '';
$pricing_table = call_user_func_array (
array ( new AWDP_viewPricingTable(), 'pricin_table' ),
array ( $rules, $product, $price, $post_id, $prodLists, $variations, $discountedPrice, $discountProductMaxPrice, $discountProductMinPrice )
);
echo $pricing_table;
}
// Price View HTML
public function get_product_price_html ( $item_price, $product )
{
if ( !$product ) return $item_price;
$updatedPrice = '';
$post_id = $product->get_id();
/*
* Set / Reset Attribute Value
* Support for Feed Plugin - Set sale price attribute key as 'acowdp_sale_price'
* ver @ 4.4.4
*/
if ( metadata_exists ( 'post', $post_id, AWDP_Feed_Attribute ) ) {
update_post_meta ( $post_id, AWDP_Feed_Attribute, '' );
} else {
add_post_meta ( $post_id, AWDP_Feed_Attribute, '' );
}
// End
if ( is_admin() && !wp_doing_ajax() )
return $item_price;
// Load discount rules
$this->load_rules();
// Check if discount is active
if ( $this->discount_rules == null )
return $item_price; // Exit if no rules
// Load Product List
$this->set_product_list();
/*
* ver @ 4.3.3
* Tax Settings
*/
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$cartPrice = $product->get_sale_price() ? $product->get_sale_price() : $product->get_price();
$priceIncTax = ( 'incl' === $tax_display_mode ) ? wc_get_price_including_tax( $product, array ( 'price' => $cartPrice ) ) : wc_get_price_excluding_tax( $product, array ( 'price' => $cartPrice ) );
$rules = $this->discount_rules;
$price = $priceIncTax ? $priceIncTax : $cartPrice;
$prodLists = $this->product_lists;
$variations = $this->variations;
$cartRules = $this->awdp_cart_rules;
// Display regular price instead of sale price @ ver 4.3.3
$displayPrcIncTax = ( 'incl' === $tax_display_mode ) ? wc_get_price_including_tax( $product, array('price' => $product->get_regular_price() ) ) : wc_get_price_excluding_tax( $product, array('price' => $product->get_regular_price() ) );
$addition_settings = get_option('awdp_addition_settings') ? get_option('awdp_addition_settings') : [];
// $use_regular = array_key_exists ( 'use_regular', $addition_settings ) ? $addition_settings['use_regular'] : false;
// $display_price = $use_regular ? ( $displayPrcIncTax ? $displayPrcIncTax : $product->get_regular_price() ) : '';
// $display_price = $displayPrcIncTax ? $displayPrcIncTax : $product->get_regular_price();
$display_price = '';
// if( $this->converted_rate == '' && $item->get_ID() != '' ) {
// $this->converted_rate = $this->get_con_unit($item, $price, true);
// }
if ( $price == '' || $price == 0 ) return $item_price;
$viewPrice = call_user_func_array (
array ( new AWDP_viewProductPrice(), 'product_price' ),
array ( $rules, $price, $post_id, $product, $prodLists, $cartRules, $item_price, $display_price )
);
if ( is_array ( $viewPrice ) ) {
$updatedPrice = $viewPrice['itemPrice'];
$this->discountProductPrice = $viewPrice['discountedPrice'];
$this->discountProductMaxPrice = $viewPrice['discountedMaxPrice'];
$this->discountProductMinPrice = $viewPrice['discountedMinPrice'];
}
return $updatedPrice ? $updatedPrice : $item_price;
}
// WCPA Get Variation Price
public function wdpWCPAVariationPrice ( )
{
// global $product;
// $product->get_id();
// Reset Query
wp_reset_query();
$post_id = get_the_ID();
$product = wc_get_product( $post_id );
if ( !$product ) return '';
/*
* ver @ 4.3.3
* Tax Settings
*/
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$cartPrice = $product->get_sale_price() ? $product->get_sale_price() : $product->get_price();
$priceIncTax = ( 'incl' === $tax_display_mode ) ? wc_get_price_including_tax( $product, array ( 'price' => $cartPrice ) ) : wc_get_price_excluding_tax( $product, array ( 'price' => $cartPrice ) );
$price = $priceIncTax ? $priceIncTax : $cartPrice;
if ( is_admin() )
return $price;
// Load discount rules
$this->load_rules();
// Check if discount is active
if ( $this->discount_rules == null )
return $price; // Exit if no rules
// Load Product List
$this->set_product_list();
$updatedPrice = '';
$product_slug = $product->get_data()['slug'];
$rules = $this->discount_rules;
$prodLists = $this->product_lists;
$variations = $this->variations;
$cartRules = $this->awdp_cart_rules;
$item_price = $price;
// if( $this->converted_rate == '' && $item->get_ID() != '' ) {
// $this->converted_rate = $this->get_con_unit($item, $price, true);
// }
$priceGroup = call_user_func_array (
array ( new AWDP_productGroup(), 'product_group' ),
array ( $rules, $price, $post_id, $product, $prodLists, $cartRules, $item_price )
);
if ( is_array ( $priceGroup ) ) {
return new WP_REST_Response($priceGroup, 200);
}
return $price;
}
// WCPA Discount Price For Frontend
public function wdpDynamicDiscount ()
{
$post_id = isset( $_GET['prodID'] ) ? $_GET['prodID'] : get_the_ID();
$variation_id = isset( $_GET['varID'] ) ? $_GET['varID'] : '';
$itemCount = isset( $_GET['proCount'] ) ? $_GET['proCount'] : 1;
$product = $variation_id ? wc_get_product ( $variation_id ) : wc_get_product ( $post_id );
if ( !$product ) return '';
update_option('itemCount', $itemCount);
/*
* ver @ 5.0.5
* Tax Settings
*/
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$pirce_include_tax = get_option( 'woocommerce_prices_include_tax' );
$cartPrice = $product->get_sale_price() ? $product->get_sale_price() : $product->get_price();
/*
* ver @ 5.0.9
* If more than 2 decimal points, round to 2 decimal points
*/
$decimal_points = get_option( 'woocommerce_price_num_decimals' ) ? get_option( 'woocommerce_price_num_decimals' ) : 2;
$priceIncTax = ( ( strlen ( strrchr ( wc_get_price_including_tax ( $product, array ( 'price' => $cartPrice ) ), '.' ) ) -1 ) > 2 ) ? round ( wc_get_price_including_tax ( $product, array ( 'price' => $cartPrice ) ), 2 ) : wc_get_price_including_tax ( $product, array ( 'price' => $cartPrice ) );
$priceExcTax = ( ( strlen ( strrchr ( wc_get_price_excluding_tax ( $product, array ( 'price' => $cartPrice ) ), '.' ) ) -1 ) > 2 ) ? round ( wc_get_price_excluding_tax ( $product, array ( 'price' => $cartPrice ) ), 2 ) : wc_get_price_excluding_tax ( $product, array ( 'price' => $cartPrice ) );
// $priceAfterTax = ( 'incl' === $tax_display_mode ) ? wc_get_price_including_tax ( $product, array ( 'price' => $cartPrice ) ) : wc_get_price_excluding_tax ( $product, array ( 'price' => $cartPrice ) );
$priceAfterTax = ( 'incl' === $tax_display_mode ) ? $priceIncTax : ( ( 'yes' === $pirce_include_tax && 'excl' === $tax_display_mode ) ? $priceExcTax : '' );
$price = $priceAfterTax ? $priceAfterTax : $cartPrice;
// Load discount rules
$this->load_rules();
// Check if discount is active
if ( $this->discount_rules == null )
return $price; // Exit if no rules
// Load Product List
$this->set_product_list();
$updatedPrice = '';
$product_slug = $product->get_data()['slug'];
$rules = $this->discount_rules;
$prodLists = $this->product_lists;
$variations = $this->variations;
$cartRules = $this->awdp_cart_rules;
$item_price = $price;
// if ( $discountedPrice ) {
// $product_id = $_REQUEST['product_id'];
// }
$priceGroup = call_user_func_array (
array ( new AWDP_productGroup(), 'product_price' ),
array ( $rules, $price, $post_id, $product, $prodLists, $cartRules, $item_price,$itemCount)
);
// if ( is_array ( $priceGroup ) ) {
// return new WP_REST_Response($priceGroup, 200);
// }
if ( is_array ( $priceGroup ) ) {
return json_encode($priceGroup);
}
return $price;
}
public function wcpaDiscount($response, $product)
{
$result = [];
if ( !$product ) return $response;
// Load discount rules
$this->load_rules();
// Check if discount is active
if ( $this->discount_rules == null ) return $response;
// Load Product List
$this->set_product_list();
$rules = $this->discount_rules;
$prodLists = $this->product_lists;
$variations = $this->variations;
$cartRules = $this->awdp_cart_rules;
// $item_price = $price;
$wcpaDisc = call_user_func_array (
array ( new AWDP_productGroup(), 'wcpa_discount' ),
array ( $rules, $product )
);
if ( !empty ($wcpaDisc) ) {
// Considering only single discount value as multiple values / group currently not supported with WCPA
foreach ( $wcpaDisc as $disc ) {
if ( $disc['type'] == 'percentage' ) {
$value = $disc['value'] ? $disc['value'] / 100 : 0;
$result['fixed'] = 0;
$result['percentage'] = $value;
} elseif ( $disc['type'] == 'fixed' ) {
$value = $disc['value'] ? $disc['value'] : 0;
$result['percentage'] = 0;
$result['fixed'] = $value;
}
}
}
return !empty($result) ? $result : $response;
}
// WCPA Price
public function wdpWCPAPrice($default, $product) {
// Load discount rules
$this->load_rules();
// Check if discount is active
if ( $this->discount_rules == null )
return $default; // Exit if no rules
// Load Product List
$this->set_product_list();
if ( !$product ) return $default;
$updatedPrice = '';
$result = [];
$product_slug = $product->get_data()['slug'];
$post_id = $product->get_id();
/*
* ver @ 4.3.3
* Tax Settings
*/
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$cartPrice = $product->get_sale_price() ? $product->get_sale_price() : $product->get_price();
$priceIncTax = ( 'incl' === $tax_display_mode ) ? wc_get_price_including_tax( $product, array ( 'price' => $cartPrice ) ) : wc_get_price_excluding_tax( $product, array ( 'price' => $cartPrice ) );
$rules = $this->discount_rules;
$price = $priceIncTax ? $priceIncTax : $cartPrice;
$prodLists = $this->product_lists;
$variations = $this->variations;
$cartRules = $this->awdp_cart_rules;
$item_price = $price;
// Display regular price instead of sale price @ ver 4.3.3
$displayPrcIncTax = ( 'incl' === $tax_display_mode ) ? wc_get_price_including_tax( $product, array('price' => $product->get_regular_price() ) ) : wc_get_price_excluding_tax( $product, array('price' => $product->get_regular_price() ) );
$addition_settings = get_option('awdp_addition_settings') ? get_option('awdp_addition_settings') : [];
// $use_regular = array_key_exists ( 'use_regular', $addition_settings ) ? $addition_settings['use_regular'] : false;
// $display_price = $use_regular ? ( $displayPrcIncTax ? $displayPrcIncTax : $product->get_regular_price() ) : '';
// $display_price = $displayPrcIncTax ? $displayPrcIncTax : $product->get_regular_price();
$display_price = '';
$viewPrice = call_user_func_array (
array ( new AWDP_viewProductPrice(), 'product_price' ),
array ( $rules, $price, $post_id, $product, $prodLists, $cartRules, $item_price, $display_price )
);
$itemCount = get_option('itemCount', 1);
$wcpaPrice = call_user_func_array (
array ( new AWDP_productGroup(), 'product_price' ),
array ( $rules, $price, $post_id, $product, $prodLists, $cartRules, $item_price,$itemCount)
);
update_option('itemCount', 1);
if ( is_array ( $viewPrice ) || is_array ( $wcpaPrice ) ) {
// $updatedPrice = $viewPrice['discountedPrice'];
$result['price'] = $viewPrice['discountedPrice'] ? $viewPrice['discountedPrice'] : $wcpaPrice['price'];
$result['originalPrice'] = $price;
return $result;
}
return $default;
}
// Cart Price View
public function cart_discount_items ( $item_price, $cart_item )
{
// Disable Discount if any other gets added to the cart
$disable_discount = get_option('awdp_disable_discount') ? get_option('awdp_disable_discount') : '';
$coupon = get_option('awdp_fee_label') ? get_option('awdp_fee_label') : 'Discount';
$coupon_code = apply_filters('woocommerce_coupon_code', $coupon);
if ( $disable_discount && !empty ( WC()->cart->get_applied_coupons() ) && !in_array ( $coupon_code, WC()->cart->get_applied_coupons() ) ) {
return $item_price;
}
// Load discount rules
$this->load_rules();
// Check if discount is active
if ( $this->discount_rules == null )
return $item_price; // Exit if no rules
// Load Product List
$this->set_product_list();
$post_id = $cart_item['product_id'];
$quantity = $cart_item['quantity'];
$product = wc_get_product( $post_id );
$rules = $this->discount_rules;
// $price = $product->get_sale_price() ? $product->get_sale_price() : $product->get_price(); // get cart price
// $price = $cart_item['data']->get_price(); // get cart price
/*
* ver @ 4.3.3
* Tax Settings
*/
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$cartPrice = $cart_item['data']->get_price();
// Checking for addons price
$addonPrice = apply_filters('wcpa_cart_addon_data', false, $cart_item);
$dispPrice = $addonPrice ? ( $addonPrice['totalPrice'] - $addonPrice['excludeFromDiscount'] ) : $cartPrice;
$priceIncTax = ( 'incl' === $tax_display_mode ) ? wc_get_price_including_tax( $product, array ( 'price' => $cartPrice ) ) : wc_get_price_excluding_tax( $product, array ( 'price' => $cartPrice ) );
$price = $priceIncTax ? $priceIncTax : $cartPrice; // commenting $discPrices - fix - cart price mismatch when multiples rules are applied
// $price = '';
// $product_slug = $product->get_data()['slug'];
$product_slug = $cart_item['data']->get_slug();
$prodLists = $this->product_lists;
$variations = $this->variations;
$cartContents = WC()->cart->get_cart();
// $couponStatus = get_option('awdp_apply_coupon_discount') ? get_option('awdp_apply_coupon_discount') : false;
$couponStatus = false;
// $disc_prod_ID = $post_id;
$variationID = $cart_item['variation_id'];
// $variations = $cart_item['variation'];
$disc_prod_ID = $variationID == 0 ? $post_id : $variationID;
// Changing Product Slug to cart key - addons compatibility
$cartKey = $cart_item['key'];
// if( $this->converted_rate == '' && $item->get_ID() != '' ) {
// $this->converted_rate = $this->get_con_unit($item, $price, true);
// }
/*
* ver @ 4.3.3
* Tax Settings
*/
$priceIncTax = ( 'incl' === $tax_display_mode ) ? wc_get_price_including_tax( $product ) : wc_get_price_excluding_tax( $product );
// Display regular price instead of sale price @ ver 4.3.3
$displayPrcIncTax = ( 'incl' === $tax_display_mode ) ? wc_get_price_including_tax( $product, array('price' => $product->get_regular_price() ) ) : wc_get_price_excluding_tax( $product, array('price' => $product->get_regular_price() ) );
$addition_settings = get_option('awdp_addition_settings') ? get_option('awdp_addition_settings') : [];
// $use_regular = array_key_exists ( 'use_regular', $addition_settings ) ? $addition_settings['use_regular'] : false;
// $display_price = $use_regular ? ( $displayPrcIncTax ? $displayPrcIncTax : $product->get_regular_price() ) : '';
// $display_price = $displayPrcIncTax ? $displayPrcIncTax : $product->get_regular_price();
$display_price = '';
foreach ( $this->discount_rules as $k => $rule ) {
// Disable Discount for Deposit Items
$depositCheck = get_post_meta($rule['id'], 'deposit_check', true) ? get_post_meta($rule['id'], 'deposit_check', true) : 0;
if ( array_key_exists ( 'awcdp_deposit', $cart_item ) && $cart_item['awcdp_deposit'] && $depositCheck ) {
continue;
}
// Get Product List
if ( !$this->get_items_to_apply_discount ( $product, $rule, $disc_prod_ID, false, $product_slug ) ) {
continue;
}
// Check if User if Logged-In
if ( ( intval ( $rule['discount_reg_customers'] ) === 1 && !is_user_logged_in() ) || ( intval ( $rule['discount_reg_customers'] ) === 1 && is_user_logged_in() && ( !empty ( array_filter ( $rule['discount_reg_user_roles'] ) ) && empty ( array_intersect ( $rule['discount_cur_user_roles'], $rule['discount_reg_user_roles'] ) ) ) ) ) {
continue;
}
// Validate Rules
if( 'cart_quantity' != $rule['type'] ) { // Skipping cart_quantity rule //
if ( !$this->validate_discount_rules( $product, $rule, ['product_price','cart_total_amount', 'cart_total_amount_all_prods', 'cart_items', 'cart_items_all_prods', 'cart_products', 'cart_products_list'], $cart_item ) ) {
continue;
}
}
// Discounts Default Values
if ( !isset ( $this->discounts[$rule['id']] ) ) {
$this->discounts[$rule['id']] = [ 'label' => $rule['label'], 'discount_type' => $rule['type'], 'discount_remainder' => -1, 'taxable' => false ];
}
/*
* Disable Double Discount
* @ver 4.1.6
* $product_slug added to awdp_discount_applied list is discount already applied
*/
if ( array_key_exists ( 'discounts', $this->discounts[$rule['id']] ) && array_key_exists ( $cartKey, $this->discounts[$rule['id']]['discounts'] ) ) {
$this->awdp_discount_applied[] = $product_slug;
continue;
}
// Get Price
// if ( $couponStatus && array_key_exists ( $product_slug, $this->actual_price ) ) {
// $price = $this->actual_price[$product_slug];
// } else {
// $price = !empty ($this->wdp_discounted_price) ? ( ( array_key_exists ( $cartKey, $this->wdp_discounted_price ) && $this->wdp_discounted_price[$cartKey] != '' ) ? wc_remove_number_precision ( $this->wdp_discounted_price[$cartKey] ) : $cart_item['data']->get_price() ) : $cart_item['data']->get_price();
$price = !empty ($this->wdp_discounted_price) ? ( ( array_key_exists ( $cartKey, $this->wdp_discounted_price ) && $this->wdp_discounted_price[$cartKey] != '' ) ? wc_remove_number_precision ( $this->wdp_discounted_price[$cartKey] ) : $cart_item['data']->get_price() ) : $cart_item['data']->get_price();
// }
$discVariable = $this->discounts[$rule['id']];
$prodLists = $this->product_lists;
// Discount Types
if ( 'percent_product_price' == $rule['type'] )
$result = call_user_func_array (
array ( new AWDP_typeProductPrice(), 'apply_discount_percent_product_price' ),
array ( $rule, $product, $price, $quantity, $discVariable, $cart_item, $disc_prod_ID, $prodLists, $dispPrice, $couponStatus )
);
else if ( 'fixed_product_price' == $rule['type'] )
$result = call_user_func_array (
array ( new AWDP_typeProductPrice(), 'apply_discount_fixed_product_price' ),
array ( $rule, $product, $price, $quantity, $discVariable, $cart_item, $disc_prod_ID, $prodLists, $dispPrice, $couponStatus )
);
else if ( 'percent_total_amount' == $rule['type'] )
$result = call_user_func_array (
array ( new AWDP_typeTotalAmount(), 'apply_discount_percent_total_amount' ),
array ( $rule, $product, $price, $quantity, $discVariable, $cart_item, $disc_prod_ID, $dispPrice, $couponStatus )
);
else if ( 'fixed_cart_amount' == $rule['type'] )
$result = call_user_func_array (
array ( new AWDP_typeTotalAmount(), 'apply_discount_fixed_price_total_amount' ),
array ( $rule, $product, $price, $quantity, $discVariable, $cart_item, $disc_prod_ID, $dispPrice, $couponStatus )
);
else if ( 'cart_quantity' == $rule['type'] )
$result = call_user_func_array (
array ( new AWDP_typeCartQuantity(), 'apply_discount_cart_quantity' ),
array ( $rule, $product, $price, $quantity, $discVariable, $prodLists, $cartContents, $cart_item, $disc_prod_ID, $dispPrice, $couponStatus )
);
if ( !empty($result) ) {
$this->discounts[$rule['id']] = $result['productDiscount'];
$this->discounted_products[] = $cartKey;
$this->wdp_discounted_price[$cartKey] = array_key_exists ( 'discountedprice', $result ) ? $result['discountedprice'] : ( array_key_exists ( $cartKey, $this->wdp_discounted_price ) ? $this->wdp_discounted_price[$cartKey] : '' );
}
}
$activeDiscounts = $this->discounts;
$viewPrice = call_user_func_array (
array ( new AWDP_viewCartPrice(), 'cart_price' ),
array ( $rules, $price, $cart_item, $prodLists, $item_price, $activeDiscounts, $product, $quantity, $display_price )
);
return $viewPrice;
}
// Sub Total Calculations
public function wdpCartLoop ( $wc, $cart_item, $cart_item_key )
{
// $couponStatus = get_option('awdp_apply_coupon_discount') ? get_option('awdp_apply_coupon_discount') : false;
// if ( $couponStatus ) {
// return $wc;
// }
// $wcpa_price = preg_match('/(.*?)<\/span>/s', $wc, $match);
$activeDiscounts = $this->discounts;
// $variationDiscounts = $this->variationDiscounts;
$decimalPoints = wc_get_price_decimals();
$product_id = $cart_item['product_id'];
$product = wc_get_product( $product_id );
/*
* ver @ 4.3.3
* Tax Settings
*/
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$cartPrice = $cart_item['data']->get_price();
// Checking for addons price
$addonPrice = apply_filters('wcpa_cart_addon_data', false, $cart_item);
$dispPrice = $addonPrice ? ( $addonPrice['totalPrice'] - $addonPrice['excludeFromDiscount'] ) : $cartPrice;
$priceIncTax = ( 'incl' === $tax_display_mode ) ? wc_get_price_including_tax( $product, array ( 'price' => $cartPrice ) ) : wc_get_price_excluding_tax( $product, array ( 'price' => $cartPrice ) );
$price = $priceIncTax ? $priceIncTax : $cartPrice;
$quantity = $cart_item['quantity'];
$discount = 0;
$prod_ID = $cart_item['data']->get_slug();
$cartKey = $cart_item['key'];
// if ( $price > 0 ) {
// if (WC()->cart->display_prices_including_tax()) {
// $price = $this->wdp_price_including_tax ( $product, $price, array(
// 'qty' => $quantity,
// 'price' => $price,
// ) );
// } else {
// $price = $this->wdp_price_excluding_tax ( $product, $price, array(
// 'qty' => $quantity,
// 'price' => $price
// ) );
// }
// }
if ( $activeDiscounts ) {
foreach ( $activeDiscounts as $discounts ) {
if ( array_key_exists ( 'discounts', $discounts ) ) {
if ( array_key_exists ( $cartKey, $discounts['discounts'] ) && $discounts['discounts'][$cartKey]['discount'] != '' && ( $discounts['discounts'][$cartKey]["displayoncart"] != false ) ) {
$discount += wc_remove_number_precision ( $discounts['discounts'][$cartKey]['discount'] );
}
}
}
$discount = ( 'incl' === $tax_display_mode ) ? round( wc_get_price_including_tax( $product, array ( 'price' => $discount ) ),$decimalPoints ) : round( wc_get_price_excluding_tax( $product, array ( 'price' => $discount ) ), $decimalPoints );
$discounted_price = ( $price - $discount ) * $quantity;
$price = round ( $discounted_price, $decimalPoints );
$product_subtotal = wc_price ( $price );
if ( $product->is_taxable() && get_option('woocommerce_tax_display_cart') == 'incl' ) {
if( !wc_prices_include_tax() && WC()->cart->get_subtotal_tax() > 0 ) {
$product_subtotal .= ' ' . WC()->countries->inc_tax_or_vat() . '';
}
}
return $product_subtotal;
} else {
return $wc;
}
}
// Show Offer Message
public function show_offer_message(){
// Load discount rules
$this->load_rules();
// Check if discount is active
if ( $this->discount_rules == null )
return ''; // Exit if no rules
Global $product;
// Load Product List
$this->set_product_list();
$productid = $product->get_id();
$result = '';
$offer_desc_config = get_option('awdp_disc_desc_config') ? get_option('awdp_disc_desc_config') : [];
$offer_rule = array_key_exists ( 'dismessage_rule', $offer_desc_config ) ? $offer_desc_config['dismessage_rule'] : '';
$list_products = [];
$all_prods = false;
$checkML = call_user_func ( array ( new AWDP_ML(), 'is_default_lan' ), '' );
$currentLang = !$checkML ? call_user_func ( array ( new AWDP_ML(), 'current_language' ), '' ) : '';
$langSettings = get_option('awdp_settings_lang_options') ? get_option('awdp_settings_lang_options') : [];
/*
* @ver 4.1.7
* Offer description for all active rules (earlier version displays description on all products)
*/
if ( $offer_rule != '' ) {
/*
* @ver 4.3.5
* Added support for custom product list - offer description
*/
$custom_prd_list = get_post_meta ( $offer_rule, 'discount_custom_pl', true );
if ( $custom_prd_list ) {
$list_products = $this->set_custom_list($offer_rule);
} else {
if ( $offer_rule == 'all_active' ) {
foreach ( $this->discount_rules as $k => $rule ) {
$ruleID = $rule['id'];
$prd_list = get_post_meta ( $ruleID, 'discount_product_list', true );
if ( '' == $prd_list || 0 == $prd_list ) {
$all_prods = true;
$offer_rule = '';
break;
}
$list_products = array_merge ( $list_products, $this->product_lists[$prd_list] );
}
array_values ( array_unique ( $list_products ) );
} else {
$prd_list = get_post_meta ( $offer_rule, 'discount_product_list', true );
if ( $prd_list ) {
$list_products = array_merge ( $list_products, $this->product_lists[$prd_list] );
} else { // Set to all_products when no product list is selected (pricing rule)
$offer_rule = '';
}
}
}
}
if ( $offer_rule == '' || ( !empty ( $list_products ) && in_array ( $productid, $list_products ) ) ) {
// $productlist = ( $offer_rule != '' && $offer_rule != 'all_active' ) ? ( get_post_meta ( $offer_rule, 'discount_product_list', true ) ) : '';
// // Check in product list
// if ( '' == $productlist || 0 == $productlist || $all_prods == true || ( !empty ( $list_products ) && in_array ( $productid, $list_products ) ) || ( isset ( $this->product_lists[$productlist] ) && in_array ( $productid, $this->product_lists[$productlist] ) ) ) {
/*
* ver @ 4.4.8
* WMPL Support offer description
*/
if ( !empty ($langSettings) && array_key_exists ( $currentLang, $langSettings ) ) {
$offer_desc = array_key_exists ( 'dismessage', $langSettings[$currentLang] ) ? $langSettings[$currentLang]['dismessage'] : ( array_key_exists ( 'dismessage', $offer_desc_config ) ? $offer_desc_config['dismessage'] : '' );
} else {
$offer_desc = array_key_exists ( 'dismessage', $offer_desc_config ) ? $offer_desc_config['dismessage'] : '';
}
// $offer_desc = array_key_exists ( 'dismessage', $offer_desc_config ) ? $offer_desc_config['dismessage'] : '';
$offer_fontsize = array_key_exists ( 'dismessage_fontsize', $offer_desc_config ) ? $offer_desc_config['dismessage_fontsize'] : 12;
$offer_paddding_lm = array_key_exists ( 'dismessage_paddding_lm', $offer_desc_config ) ? $offer_desc_config['dismessage_paddding_lm'] : 10;
$offer_paddding_tp = array_key_exists ( 'dismessage_paddding_tp', $offer_desc_config ) ? $offer_desc_config['dismessage_paddding_tp'] : 10;
$offer_radius = array_key_exists ( 'dismessage_radius', $offer_desc_config ) ? $offer_desc_config['dismessage_radius'] : 0;
$offer_color = array_key_exists ( 'dismessage_color', $offer_desc_config ) ? $offer_desc_config['dismessage_color'] : '';
$offer_background = array_key_exists ( 'dismessage_background', $offer_desc_config ) ? $offer_desc_config['dismessage_background'] : '';
$border_top_width = ( array_key_exists ( 'border_top_width', $offer_desc_config ) && $offer_desc_config['border_top_width'] != '' ) ? $offer_desc_config['border_top_width'].'px' : '0px';
$border_right_width = ( array_key_exists ( 'border_right_width', $offer_desc_config ) && $offer_desc_config['border_right_width'] != '' ) ? ' '.$offer_desc_config['border_right_width'].'px' : ' 0px';
$border_bottom_width = ( array_key_exists ( 'border_bottom_width', $offer_desc_config ) && $offer_desc_config['border_bottom_width'] != '' ) ? ' '.$offer_desc_config['border_bottom_width'].'px' : ' 0px';
$border_left_width = ( array_key_exists ( 'border_left_width', $offer_desc_config ) && $offer_desc_config['border_left_width'] != '' ) ? ' '.$offer_desc_config['border_left_width'].'px' : ' 0px';
$offer_border_color = array_key_exists ( 'offer_border_color', $offer_desc_config ) ? $offer_desc_config['offer_border_color'] : '';
$customStyle = 'font-size: '.$offer_fontsize.'px;padding: '.$offer_paddding_tp.'px '.$offer_paddding_lm.'px;border-radius: '.$offer_radius.'px;';
$customStyle .= $offer_color ? 'color: '.$offer_color.';' : '';
$customStyle .= $offer_background ? 'background: '.$offer_background.';' : '';
$customStyle .= $offer_border_color ? 'border-color: '.$offer_border_color.';' : '';
$customStyle .= ( $border_top_width || $border_right_width || $border_bottom_width || $border_left_width ) ? 'border-width: '.$border_top_width.$border_right_width.$border_bottom_width.$border_left_width.';' : '';
$result = '';
// }
}
echo $result;
}
// Cart Message
public function wdpCartMessage(){
global $woocommerce;
$coupon = get_option('awdp_fee_label') ? get_option('awdp_fee_label') : 'Discount';
$coupon_code = apply_filters('woocommerce_coupon_code', $coupon);
$customStyle = '';
$result = '';
$total = 0;
$checkML = call_user_func ( array ( new AWDP_ML(), 'is_default_lan' ), '' );
$currentLang = !$checkML ? call_user_func ( array ( new AWDP_ML(), 'current_language' ), '' ) : '';
$langSettings = get_option('awdp_settings_lang_options') ? get_option('awdp_settings_lang_options') : [];
if ( in_array ( $coupon_code, $woocommerce->cart->get_applied_coupons() ) ) {
if ( $this->discounts ) {
foreach ( $this->discounts as $ruleid => $discounts ) {
$discount_type = $discounts['discount_type'];
$qn_type = ( $discount_type == 'cart_quantity' ) ? get_post_meta ( $ruleid, 'discount_quantity_type', true ) : '';
if ( array_key_exists ( 'discounts', $discounts ) ) {
foreach ( $discounts['discounts'] as $key => $discount ) {
$disc_product_ID = $discount['productid'];
if ( $discount['discount'] != '' ) {
// Decimal Round
$decimal_val = $discount['discount'] - floor($discount['discount']);
$calc_discount = ( $decimal_val == 0 ) ? $discount['discount'] : ( ( $decimal_val > 0.5 ) ? ceil ( $discount['discount'] ) : floor ( $discount['discount'] ) );
if ( $discount_type == 'fixed_product_price' || $discount_type == 'percent_product_price' || ( $discount_type == 'cart_quantity' && $qn_type == 'type_product' ) ) {
$calc_discount = $calc_discount * $discount['quantity'];
}
$total = $total + ( wc_remove_number_precision ( $calc_discount ) );
}
}
}
}
}
// $coupons_obj = new WC_Coupon($coupon_code);
// $coupons_amount = $coupons_obj->get_amount();
$coupons_amount = $total;
$custom_message_settings = get_option('awdp_custom_msg_settings') ? get_option('awdp_custom_msg_settings') : [];
$custom_message_status = array_key_exists ( 'custom_message_status', $custom_message_settings ) ? $custom_message_settings['custom_message_status'] : false;
if ( $coupons_amount > 0 && $custom_message_status ) {
/*
* ver @ 4.4.8
* WMPL Support custom message
*/
if ( !empty ($langSettings) && array_key_exists ( $currentLang, $langSettings ) ) {
$custom_message = array_key_exists ( 'custom_message', $langSettings[$currentLang] ) ? $langSettings[$currentLang]['custom_message'] : ( array_key_exists ( 'custom_message', $custom_message_settings ) ? $custom_message_settings['custom_message'] : '' );
} else {
$custom_message = array_key_exists ( 'custom_message', $custom_message_settings ) ? $custom_message_settings['custom_message'] : '';
}
// $custom_message = array_key_exists ( 'custom_message', $custom_message_settings ) ? $custom_message_settings['custom_message'] : '';
$custom_message_linheight = array_key_exists ( 'custom_message_linheight', $custom_message_settings ) ? $custom_message_settings['custom_message_linheight'] : '';
$custom_message_fontsize = array_key_exists ( 'custom_message_fontsize', $custom_message_settings ) ? $custom_message_settings['custom_message_fontsize'] : '';
$custom_message_position = array_key_exists ( 'custom_message_position', $custom_message_settings ) ? $custom_message_settings['custom_message_position'] : '';
$custom_message_paddding_lm = array_key_exists ( 'custom_message_paddding_lm', $custom_message_settings ) ? $custom_message_settings['custom_message_paddding_lm'] : '';
$custom_message_paddding_tp = array_key_exists ( 'custom_message_paddding_tp', $custom_message_settings ) ? $custom_message_settings['custom_message_paddding_tp'] : '';
$custom_message_border_radius = array_key_exists ( 'custom_message_border_radius', $custom_message_settings ) ? $custom_message_settings['custom_message_border_radius'] : '';
$custom_message_border_top_width = array_key_exists ( 'custom_message_border_top_width', $custom_message_settings ) ? $custom_message_settings['custom_message_border_top_width'].'px ' : '';
$custom_message_border_right_width = array_key_exists ( 'custom_message_border_right_width', $custom_message_settings ) ? $custom_message_settings['custom_message_border_right_width'].'px ' : '';
$custom_message_border_bottom_width = array_key_exists ( 'custom_message_border_bottom_width', $custom_message_settings ) ? $custom_message_settings['custom_message_border_bottom_width'].'px ' : '';
$custom_message_border_left_width = array_key_exists ( 'custom_message_border_left_width', $custom_message_settings ) ? $custom_message_settings['custom_message_border_left_width'].'px' : '';
$custom_message_border_color = array_key_exists ( 'custom_message_border_color', $custom_message_settings ) ? $custom_message_settings['custom_message_border_color'] : '';
$custom_message_background = array_key_exists ( 'custom_message_background', $custom_message_settings ) ? $custom_message_settings['custom_message_background'] : '';
$custom_message_color = array_key_exists ( 'custom_message_color', $custom_message_settings ) ? $custom_message_settings['custom_message_color'] : '';
$customStyle .= 'font-size: '.$custom_message_fontsize.'px;padding: '.$custom_message_paddding_tp.'px '.$custom_message_paddding_lm.'px;border-radius: '.$custom_message_border_radius.'px;';
$customStyle .= $custom_message_color ? 'color: '.$custom_message_color.';' : '';
$customStyle .= $custom_message_background ? 'background: '.$custom_message_background.';' : '';
$customStyle .= $custom_message_border_color ? 'border-color: '.$custom_message_border_color.';' : '';
$customStyle .= $custom_message_position ? 'text-align: '.$custom_message_position.';' : '';
$customStyle .= ( $custom_message_border_top_width || $custom_message_border_right_width || $custom_message_border_bottom_width || $custom_message_border_left_width ) ? 'border-width: '.$custom_message_border_top_width.$custom_message_border_right_width.$custom_message_border_bottom_width.$custom_message_border_left_width.';' : '';
$message = $custom_message ? str_replace('[discount]', wc_price($coupons_amount), $custom_message ) : __("You'he saved ", "aco-woo-dynamic-pricing").wc_price($coupons_amount).__(" on this order", "aco-woo-dynamic-pricing");
$result = '' . $awdp_qn_label . ' ';
$tr_pr = '' . $awdp_pc_label . ' ';
if ( $value_display == 'discount_both' ) {
$tr_nw = '' . $awdp_nw_label . ' ';
}
}
foreach ( $DisData as $quantity_rule ) {
$dis_value = $quantity_rule->dis_value;
$dis_type = $quantity_rule->dis_type;
if ($dis_type == 'percentage') {
$discounted_new_price_bt = (float)$dis_value . $prcn_text;
} else if ($dis_type == 'fixed') {
$discounted_new_price_bt = wc_price((float)$dis_value) . $fxd_text_two;
}
if ($dis_type == 'percentage') {
$discount_pt = $var_price * ((float)$dis_value / 100);
$discount_pt = min($var_price, $discount_pt);
} else if ($dis_type == 'fixed') {
$discount_pt = $dis_value * $converted_rate;
}
$discounted_new_price = (($var_price - $discount_pt) > 0) ? wc_price ( ( $var_price - $discount_pt ) * $converted_rate ) : 0;
if ( $table_layout == 'horizontal' ) {
if ($quantity_rule->start_range == $quantity_rule->end_range) {
$tr_qn .= '' . $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_bt . ' ';
if ( $value_display == 'discount_both' ) {
$tr_nw .= '' . $discounted_new_price . ' ';
}
} else {
if ($quantity_rule->start_range == $quantity_rule->end_range) {
if ( $value_display == 'discount_value' ) {
$table .= ' ';
} else if ( $value_display == 'discount_both' ) {
$table .= '' . $quantity_rule->start_range . ' ' . $discounted_new_price_bt . ' ';
} else {
$table .= '' . $quantity_rule->start_range . ' ' . $discounted_new_price_bt . ' ' . $discounted_new_price . ' ';
}
} else if ($quantity_rule->end_range) {
if ( $value_display == 'discount_value' ) {
$table .= '' . $quantity_rule->start_range . ' ' . $discounted_new_price . ' ';
} else if ( $value_display == 'discount_both' ) {
$table .= '' . $quantity_rule->start_range . ' ' . $discounted_new_price_bt . ' ';
} else {
$table .= '' . $quantity_rule->start_range . ' - ' . $quantity_rule->end_range . ' ' . $discounted_new_price_bt . ' ' . $discounted_new_price . ' ';
}
} else {
if ( $value_display == 'discount_value' ) {
$table .= '' . $quantity_rule->start_range . ' - ' . $quantity_rule->end_range . ' ' . $discounted_new_price . ' ';
} else if ( $value_display == 'discount_both' ) {
$table .= '' . $quantity_rule->start_range . ' ' . $discounted_new_price_bt . ' ';
} else {
$table .= '' . $quantity_rule->start_range . ' + ' . $discounted_new_price_bt . ' ' . $discounted_new_price . ' ';
}
}
}
}
if ($table_layout == 'horizontal') {
$tr_qn .= '';
$tr_pr .= '';
if ( $value_display == 'discount_both' ) {
$tr_nw .= '';
$table .= $tr_qn . $tr_pr . $tr_nw;
} else {
$table .= $tr_qn . $tr_pr;
}
}
echo $table;
}
} else {
$variation_prices = $_POST['ProdVarPrice'] ? json_decode($_POST['ProdVarPrice']) : [];
if ( $DisData ) {
// if ( !empty ( $variation_prices ) ) {
// $price_to_discount_max = $variation_prices ? max($variation_prices) : 0;
// $price_to_discount_min = $variation_prices ? min($variation_prices) : 0;
// // Default value when quantity not in range
// $result['price'] = $price_to_discount_min ? $price_to_discount_min : 0;
// $result['total'] = $price_to_discount_min ? round ( ( $price_to_discount_min * $ProdQty ), wc_get_price_decimals() ) : 0;
// $result['currency'] = get_woocommerce_currency_symbol();
// foreach ( $DisData as $discount ) {
// if ( ( $discount->end_range != '' && $ProdQty >= $discount->start_range && $ProdQty <= $discount->end_range ) || ( $discount->end_range == '' && $ProdQty >= $discount->start_range ) || ( $discount->end_range != '' && $ProdQty > $discount->end_range && ( $discount->start_range != $discount->end_range ) ) ) {
// if ( $discount->dis_type == 'percentage' ) {
// $discount_max_value = $price_to_discount_max * ((float)$discount->dis_value / 100);
// $discount_min_value = $price_to_discount_min * ((float)$discount->dis_value / 100);
// // $discount_max_value = min($price_to_discount, $discount_pt);
// } else if ( $discount->dis_type == 'fixed' ) {
// $discount_max_value = wc_add_number_precision($discount->dis_value);
// $discount_min_value = wc_add_number_precision($discount->dis_value);
// }
// $result['price'] = $discounted_new_min_price ? round ( $discounted_new_min_price, wc_get_price_decimals() ) : 0;
// $result['total'] = $discounted_new_min_price ? round ( ( $discounted_new_min_price * $ProdQty ), wc_get_price_decimals() ) : 0;
// $result['currency'] = get_woocommerce_currency_symbol();
// $discounted_new_max_price = (($price_to_discount_max - $discount_max_value) > 0) ? wc_price ( wc_remove_number_precision ( $price_to_discount_max - $discount_max_value ) ) : 0;
// $discounted_new_min_price = (($price_to_discount_min - $discount_min_value) > 0) ? wc_price ( wc_remove_number_precision ( $price_to_discount_min - $discount_min_value ) ) : 0;
// // $result['price'] = wc_format_sale_price ( wc_price ( wc_remove_number_precision ( $price_to_discount_min ) ) . ' - ' . wc_price ( wc_remove_number_precision ( $price_to_discount_max ) ), $discounted_new_min_price . ' - ' . $discounted_new_max_price );
// continue;
// }
// }
// } else {
// Default value when quantity not in range
$result['price'] = $ProdPrice ? (float)$ProdPrice : 0;
$result['total'] = $ProdPrice ? round ( ( $ProdPrice * $ProdQty ), wc_get_price_decimals() ) : 0;
$result['currency'] = get_woocommerce_currency_symbol();
foreach ( $DisData as $discount ) {
if ( ( $discount->end_range != '' && $ProdQty >= $discount->start_range && $ProdQty <= $discount->end_range ) || ( $discount->end_range == '' && $ProdQty >= $discount->start_range ) || ( $discount->end_range != '' && $ProdQty > $discount->end_range && ( $discount->start_range != $discount->end_range ) ) ) {
// $discountprice = ( $discount->dis_type === 'fixed' ) ? ( $ProdPrice - $discount->dis_value ) : ( $ProdPrice - ( $ProdPrice * ( (float)$discount->dis_value / 100 ) ) );
// $result['price'] = $discountprice ? round ( $discountprice, wc_get_price_decimals() ) : 0;
// $result['total'] = $discountprice ? round ( ( $discountprice * $ProdQty ), wc_get_price_decimals() ) : 0;
// $result['currency'] = get_woocommerce_currency_symbol();
$rounded_discount = ( $discount->dis_type === 'fixed' ) ? ( $ProdPrice - $discount->dis_value ) : ( $ProdPrice - ( $ProdPrice * ( (float)$discount->dis_value / 100 ) ) );
$discountprice = $rounded_discount ? round ( $rounded_discount, wc_get_price_decimals() ) : 0;
$result['price'] = $discountprice;
$result['total'] = $discountprice ? round ( ( $discountprice * $ProdQty ), wc_get_price_decimals() ) : 0;
$result['currency'] = get_woocommerce_currency_symbol();
continue;
}
}
// }
}
// $price = wc_format_sale_price($ProdPrice * $converted_rate, $discountprice * $converted_rate);
echo !empty ( $result ) ? json_encode($result) : '';
}
die();
}
// Get variations
public function wdpGetVariations ( $productID, $list = false ) {
if ( $productID ) {
if ( ( !is_array ( $productID ) && array_key_exists ( $productID, $this->productvariations ) ) || ( $list && array_key_exists ( $list, $this->productvariations ) ) ) {
return $this->productvariations[$productID];
} else {
global $wpdb;
$productID = is_array ( $productID ) ? implode(',', $productID) : $productID;
$PLVariations = $wpdb->get_col("SELECT ID FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_parent IN ($productID) AND post_type = 'product_variation'");
if ( $PLVariations ) {
if ( !is_array ( $productID ) ) $this->productvariations[$productID] = $PLVariations;
else if ( $list ) $this->productvariations[$list] = $PLVariations;
return $PLVariations;
}
}
}
return false;
}
// // Save Order Meta
// public function wdpOrderMeta ( $item_id, $values, $cart_item_key ) {
// $wdpDiscount = $this->wdp_order_meta ? $this->wdp_order_meta : [];
// // $coupon = get_option('awdp_fee_label') ? get_option('awdp_fee_label') : 'Discount';
// // $coupon_code = apply_filters('woocommerce_coupon_code', $coupon);
// if ( array_key_exists ( $cart_item_key, $wdpDiscount ) ) {
// $_awdp_discounted_price = ( $wdpDiscount[$cart_item_key] ) ? $wdpDiscount[$cart_item_key] : [];
// wc_add_order_item_meta($item_id, '_awdp_discount_details', $_awdp_discounted_price);
// // wc_add_order_item_meta($item_id, '_awdp_coupon', $coupon_code);
// return;
// }
// }
// Save Order Meta
public function wdpOrderMeta($item_id, $values, $cart_item_key) {
$wdpDiscount = $this->wdp_order_meta ? $this->wdp_order_meta : [];
if (is_object($values) && isset($values->legacy_values)) {
$cart_values = $values->legacy_values;
if (isset($cart_values['key']) && array_key_exists($cart_values['key'], $wdpDiscount)) {
$_awdp_discounted_price = $wdpDiscount[$cart_values['key']] ? $wdpDiscount[$cart_values['key']] : [];
wc_add_order_item_meta($item_id, '_awdp_discount_details', $_awdp_discounted_price);
return;
}
}
}
// Display Order Meta
public function wdpDisplayOrderMeta( $item_id, $item, $product ) {
$wdp_discount_details = wc_get_order_item_meta ( $item_id, '_awdp_discount_details', true ) ? wc_get_order_item_meta ( $item_id, '_awdp_discount_details', true ) : false;
// $wdp_discount_label = wc_get_order_item_meta($item_id, '_awdp_coupon', true);
if ( $wdp_discount_details ) {
$prdID = $product->get_ID();
$wdp_meta = '' . $quantity_rule->start_range . ' + ' . $discounted_new_price . '
' : 'Discounted Price: '. wc_price ( round ( wc_remove_number_precision ( $wdp_discounted_price ), wc_get_price_decimals() ) ) .'
';
}
// $wdp_meta .= $wdp_discount_label ? 'Discount Label: '.$wdp_discount_label : '';
}
$wdp_meta .= '
'; $result .= ''.$coupon.': '.wc_price($coupons_amount).''; $result .= ( $shipping && ( strpos ( $shipping, __('Free', 'aco-woo-dynamic-pricing') ) === false) ) ? ''.__("Shipping", "aco-woo-dynamic-pricing").': '.$shipping.'' : ''; if ($taxes) { if($tax_total_display == 'single') { $single_tax = 0; foreach ($taxes as $key => $val) { $single_tax += $val->amount; } $tax_label = isset($taxes[array_key_first($taxes)]->label) ? $taxes[array_key_first($taxes)]->label : 'Tax'; $result .= ''.$tax_label.': '.wc_price($single_tax).''; } else { foreach ($taxes as $key => $val) { $result .= ''.$val->label.': '.wc_price($val->amount).''; } } } $result .= ( $total && array_key_exists ( 'total', $total ) ) ? ''.__("Total", "aco-woo-dynamic-pricing").': '.wc_price($total['total']).'' : ''; $result .= '
'; } echo $result; } // if ( in_array($coupon_code, $woocommerce->cart->get_applied_coupons()) ) { // $coupons_obj = new WC_Coupon($coupon_code); // $coupons_amount = $coupons_obj->get_amount(); // $shipping = $woocommerce->cart->get_cart_shipping_total(); // $taxes = $woocommerce->cart->get_tax_totals(); // // $cart_total = $woocommerce->cart->cart_contents_total; // $total = $woocommerce->cart->get_totals(); // if ( $coupons_amount ) { // $result = ''; // $result .= ''.$coupon.': '.wc_price($coupons_amount).''; // $result .= ( $shipping && ( strpos ( $shipping, __('Free', 'aco-woo-dynamic-pricing') ) === false) ) ? ''.__("Shipping", "aco-woo-dynamic-pricing").': '.$shipping.'' : ''; // if ($taxes) { // foreach ($taxes as $key => $val) { // $result .= ''.$val->label.': '.wc_price($val->amount).''; // } // } // $result .= ( $total && array_key_exists ( 'total', $total ) ) ? ''.__("Total", "aco-woo-dynamic-pricing").': '.wc_price($total['total']).'' : ''; // $result .= '
'; // } // echo $result; // } } // Array Search public function array_needle_search ( $needle, $haystack ) { $result = []; foreach ( $haystack as $key => $value ) { $current_key = $key; if ( is_array ( $value ) && in_array ( $needle, $value ) !== false ) { $result[] = $current_key; } } return $result; } // Products On Sale public function check_product_on_sale( $productID ) { if ( false == $this->products_on_sale ) { global $wpdb; $awdp_onsale_prods = $wpdb->get_results( " SELECT posts.ID as id, posts.post_parent as parent_id FROM {$wpdb->posts} AS posts INNER JOIN {$wpdb->wc_product_meta_lookup} AS lookup ON posts.ID = lookup.product_id INNER JOIN {$wpdb->postmeta} as meta ON posts.ID = meta.post_id WHERE posts.post_type IN ( 'product', 'product_variation' ) AND posts.post_status = 'publish' AND lookup.onsale = 1 AND meta.meta_key LIKE '_stock_status' AND meta.meta_value IN ( 'instock', 'onbackorder' ) AND posts.post_parent NOT IN ( SELECT ID FROM `$wpdb->posts` as posts WHERE posts.post_type = 'product' AND posts.post_parent = 0 AND posts.post_status != 'publish' ) GROUP BY posts.ID " ); $prods_onSale = wp_parse_id_list( array_merge( wp_list_pluck( $awdp_onsale_prods, 'id' ), array_diff( wp_list_pluck( $awdp_onsale_prods, 'parent_id' ), array( 0 ) ) ) ); $this->products_on_sale = $prods_onSale; } $onSaleIDs = $this->products_on_sale; return in_array ( $productID, $onSaleIDs ) ? true : false; } /** * Cloning is forbidden. * * @since 1.0.0 */ public function __clone() { _doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?'), $this->_version); } /** * Unserializing instances of this class is forbidden. * * @since 1.0.0 */ public function __wakeup() { _doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?'), $this->_version); } public function check_in_rule ( $selectedRule, $productID ) { $this->load_rules(); if ( $this->discount_rules == null ) return false; // Skip if no active rules $product = wc_get_product ( $productID ); $discount_index = array_search ( $selectedRule, array_column ( $this->discount_rules, 'id' ) ); if ( $discount_index !== false ) { $rules = $this->discount_rules; $rule = $rules[$discount_index]; // Get Product List if ( !$this->get_items_to_apply_discount ( $product, $rule ) ) { return false; } // Check if User if Logged-In if ( ( intval ( $rule['discount_reg_customers'] ) === 1 && !is_user_logged_in() ) || ( intval ( $rule['discount_reg_customers'] ) === 1 && is_user_logged_in() && ( !empty ( array_filter ( $rule['discount_reg_user_roles'] ) ) && empty ( array_intersect ( $rule['discount_cur_user_roles'], $rule['discount_reg_user_roles'] ) ) ) ) ) { return false; } return true; } return false; } protected function apply_discount_remainder($rule, $items_to_apply, $amount) { $total_discount = 0; foreach ($items_to_apply as $item) { for ($i = 0; $i < $item->quantity; $i++) { // Find out how much price is available to discount for the item. $discounted_price = $this->get_discounted_price_in_cents($item); // $price_to_discount = (false) ? $discounted_price : $item->price;// check if apply_ sequential $discount = min($discounted_price, 1); // Store totals. $total_discount += $discount; // Store code and discount amount per item. $this->discounts[$rule['id']]['discounts'][$item->key] += $discount; if ($total_discount >= $amount) { break 2; } } if ($total_discount >= $amount) { break; } } return $total_discount; } public function get_discounted_price_in_cents($item, $include_tax = true, $sequential = false) { $product_actual_price = $item->get_data()['price']; $excluding_tax = get_option('woocommerce_tax_display_shop'); if ($include_tax && $excluding_tax == 'incl') { $price = $this->wdp_price_including_tax ( $item, $product_actual_price, array ( 'price' => $product_actual_price, ) ); } else { $price = $this->wdp_price_excluding_tax ( $item, $product_actual_price, array ( 'price' => $product_actual_price, ) ); } // if($sequential) // return abs($price - wc_remove_number_precision($this->get_discount($item->get_id(), true))); // else return $price; } // Woocommerce functions function wdp_price_including_tax ( $product, $prodPrice, $args = array() ) { $args = wp_parse_args( $args, array( 'qty' => '', 'price' => '', ) ); $price = '' !== $args['price'] ? max( 0.0, (float) $args['price'] ) : $prodPrice; $qty = '' !== $args['qty'] ? max( 0.0, (float) $args['qty'] ) : 1; if ( '' === $price ) { return ''; } elseif ( empty( $qty ) ) { return 0.0; } $line_price = $price * $qty; $return_price = $line_price; if ( $product->is_taxable() ) { if ( ! wc_prices_include_tax() ) { $tax_rates = WC_Tax::get_rates( $product->get_tax_class() ); $taxes = WC_Tax::calc_tax( $line_price, $tax_rates, false ); if ( 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' ) ) { $taxes_total = array_sum( $taxes ); } else { $taxes_total = array_sum( array_map( 'wc_round_tax_total', $taxes ) ); } $return_price = round( $line_price + $taxes_total, wc_get_price_decimals() ); } else { $tax_rates = WC_Tax::get_rates( $product->get_tax_class() ); $base_tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) ); /** * If the customer is excempt from VAT, remove the taxes here. * Either remove the base or the user taxes depending on woocommerce_adjust_non_base_location_prices setting. */ if ( ! empty( WC()->customer ) && WC()->customer->get_is_vat_exempt() ) { // @codingStandardsIgnoreLine. $remove_taxes = apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ? WC_Tax::calc_tax( $line_price, $base_tax_rates, true ) : WC_Tax::calc_tax( $line_price, $tax_rates, true ); if ( 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' ) ) { $remove_taxes_total = array_sum( $remove_taxes ); } else { $remove_taxes_total = array_sum( array_map( 'wc_round_tax_total', $remove_taxes ) ); } $return_price = round( $line_price - $remove_taxes_total, wc_get_price_decimals() ); /** * The woocommerce_adjust_non_base_location_prices filter can stop base taxes being taken off when dealing with out of base locations. * e.g. If a product costs 10 including tax, all users will pay 10 regardless of location and taxes. * This feature is experimental @since 2.4.7 and may change in the future. Use at your risk. */ } elseif ( $tax_rates !== $base_tax_rates && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) { $base_taxes = WC_Tax::calc_tax( $line_price, $base_tax_rates, true ); $modded_taxes = WC_Tax::calc_tax( $line_price - array_sum( $base_taxes ), $tax_rates, false ); if ( 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' ) ) { $base_taxes_total = array_sum( $base_taxes ); $modded_taxes_total = array_sum( $modded_taxes ); } else { $base_taxes_total = array_sum( array_map( 'wc_round_tax_total', $base_taxes ) ); $modded_taxes_total = array_sum( array_map( 'wc_round_tax_total', $modded_taxes ) ); } $return_price = round( $line_price - $base_taxes_total + $modded_taxes_total, wc_get_price_decimals() ); } } } return apply_filters( 'woocommerce_get_price_including_tax', $return_price, $qty, $product ); } function wdp_price_excluding_tax ( $product, $prodPrice, $args = array() ) { $args = wp_parse_args( $args, array( 'qty' => '', 'price' => '', 'skipcheck' => '' ) ); $price = '' !== $args['price'] ? max( 0.0, (float) $args['price'] ) : $prodPrice; $qty = '' !== $args['qty'] ? max( 0.0, (float) $args['qty'] ) : 1; $skipcheck = '' !== $args['skipcheck'] ? true : false; if ( '' === $price ) { return ''; } elseif ( empty( $qty ) ) { return 0.0; } $line_price = $price * $qty; if ( ( $product->is_taxable() && wc_prices_include_tax() ) || $skipcheck ) { $tax_rates = WC_Tax::get_rates( $product->get_tax_class() ); $base_tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) ); $remove_taxes = apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ? WC_Tax::calc_tax( $line_price, $base_tax_rates, true ) : WC_Tax::calc_tax( $line_price, $tax_rates, true ); $return_price = $line_price - array_sum( $remove_taxes ); // Unrounded since we're dealing with tax inclusive prices. Matches logic in cart-totals class. @see adjust_non_base_location_price. } else { $return_price = $line_price; } return apply_filters( 'woocommerce_get_price_excluding_tax', $return_price, $qty, $product ); } }