_version = $version; $this->_token = AWDP_TOKEN; $this->discount = $discount; add_action('init', array($this, 'register_awdp_discounts')); if ($this->awdp_check_woocommerce_active()) { add_filter('woocommerce_cart_calculate_fees', array( $this, 'cart_apply_discount'), 10, 1 ); // add_filter('woocommerce_cart_item_price', array($this, 'cart_price_view'), 1000, 3); add_filter('woocommerce_cart_item_price_html', array($this, 'cart_price_view'), 1000, 3); // Price add_filter('woocommerce_product_get_price', array($this, 'get_product_price'), 99, 2 ); add_filter('woocommerce_product_get_regular_price', array($this, 'get_product_price'), 99, 2 ); // add_filter('woocommerce_get_price_html', array($this, 'get_product_price_html'), 99, 2 ); // add_filter('woocommerce_product_get_regular_price', array($this, 'custom_price'), 99, 2 ); // Variations add_filter('woocommerce_product_variation_get_regular_price', array($this, 'get_product_price'), 99, 2 ); add_filter('woocommerce_product_variation_get_price', array($this, 'get_product_price'), 99, 2 ); // Variable (price range) add_filter('woocommerce_variation_prices_price', array($this, 'custom_variable_price'), 99, 3 ); add_filter('woocommerce_variation_prices_regular_price', array($this, 'custom_variable_price'), 99, 3 ); } } public function before_calculate_totals($cart_obj) { remove_action('woocommerce_before_calculate_totals', array($this, 'before_calculate_totals'), 10); remove_action('woocommerce_cart_loaded_from_session', array($this, 'before_calculate_totals'), 10); } /** * Apply the discount as fee * @param $cart_obj object */ public function cart_apply_discount($cart_obj) { // $this->discount->calculate_discount($cart_obj); $this->discount->show_discounts($cart_obj); } /** * Apply the discount as fee * @param $cart_obj object */ public function cart_price_view( $item_price, $cart_item ) { return $this->discount->cart_discount_items( $item_price, $cart_item ); } // AWPD Prodcut Price After Discount public function get_product_price( $price, $product ) { // var_dump($this->discount->awdp_product_price_after_discount( $price, $product )); return $this->discount->get_product_price( $price, $product ); } // Price HTML Display public function get_product_price_html( $price, $product ) { return $this->discount->get_product_price_html( $price, $product ); } // AWPD Prodcut Price After Discount (Variable Products) public function custom_variable_price( $price, $variation, $product ) { // Delete product cached price (if needed) // wc_delete_product_transients($variation->get_id()); return $price * 2; } /** * Check if woocommerce plugin is active */ public function awdp_check_woocommerce_active() { if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { return true; } if (is_multisite()) { $plugins = get_site_option('active_sitewide_plugins'); if (isset($plugins['woocommerce/woocommerce.php'])) return true; } return false; } // Apply discount to products in cart function review_minicart_subtotal($subtotal, $compound) { $n_subtotal = 0; $items = WC()->cart->get_cart_contents(); $awdp_pro_discount = $_SESSION["awdp_discounts"]; $awdp_sub_total = 0; // if (!is_cart() && !is_checkout()){ foreach ($items as $item => $values) { $price = $values['data']->get_price(); $quantity = $values['quantity']; $n_subtotal += $price * $quantity; $product_id = $values['product_id']; $key = array_search($product_id, $_SESSION["awdp_discount_ids"]); if ($key > -1) { $price = $awdp_pro_discount[$key]['discount'] * $quantity; $awdp_sub_total += $price; } else { $awdp_sub_total += $price; } } $subtotal = wc_price($awdp_sub_total); // } return $subtotal; } // Apply discount to cart // Total savings text function awdp_cart_price() { if (is_admin() && !defined('DOING_AJAX')) return; $chosen_payment_method = WC()->session->get('chosen_payment_method'); $awdp_sub_total = 0; foreach (WC()->cart->get_cart() as $cart_item) { // The corresponding product ID $product_id = $cart_item['product_id']; $quantity = $cart_item['quantity']; $tax_amount = array_sum(WC()->cart->get_taxes()); // Check product $awdp_pro_discount = $_SESSION["awdp_discounts"]; array_push($_SESSION["awdp_actualprice"], array('pid' => $product_id, 'price' => $cart_item['data']->get_price())); $key = array_search($product_id, $_SESSION["awdp_discount_ids"]); if ($key > -1) { if (doing_action('woocommerce_before_calculate_totals')) $price = $awdp_pro_discount[$key]['discount']; else $price = $awdp_pro_discount[$key]['discount'] * $quantity; // echo $price; $cart_item['data']->set_price(floatval($price)); $awdp_sub_total += $price; } else { //$awdp_sub_total += $price; } } $awdp_total = $awdp_sub_total + $tax_amount; WC()->cart->subtotal = $awdp_sub_total; WC()->cart->total = $awdp_total; WC()->session->set('refresh_totals', true); //wc_price($awdp_sub_total); // Checking payment gateway // if ($chosen_payment_method == 'cod') { // WC()->cart->total = WC()->cart->total - (WC()->cart->total * 10 / 100); // WC()->cart->subtotal = WC()->cart->subtotal - (WC()->cart->subtotal * 10 / 100); // } if (did_action('woocommerce_before_calculate_totals')) remove_action('woocommerce_before_calculate_totals', array($this, 'awdp_cart_price'), 99); if (did_action('woocommerce_before_cart_totals')) remove_action('woocommerce_before_cart_totals', array($this, 'awdp_cart_price'), 99); if (did_action('woocommerce_review_order_before_order_total')) remove_action('woocommerce_review_order_before_order_total', array($this, 'awdp_cart_price'), 99); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** * AWDP Custom post types */ public function register_awdp_discounts() { $post_type = AWDP_POST_TYPE; $labels = array( 'name' => 'Pricing Rules', 'singular_name' => 'Pricing Rule', 'name_admin_bar' => 'WCPA_Form', 'add_new' => _x('Add New Product Form', $post_type, 'woo-pricing-rules'), 'add_new_item' => sprintf(__('Add New %s', 'woo-pricing-rules'), 'Form'), 'edit_item' => sprintf(__('Edit %s', 'woo-pricing-rules'), 'Form'), 'new_item' => sprintf(__('New %s', 'woo-pricing-rules'), 'Form'), 'all_items' => sprintf(__('Product Rules', 'woo-pricing-rules'), 'Form'), 'view_item' => sprintf(__('View %s', 'woo-pricing-rules'), 'Form'), 'search_items' => sprintf(__('Search %s', 'woo-pricing-rules'), 'Form'), 'not_found' => sprintf(__('No %s Found', 'woo-pricing-rules'), 'Form'), 'not_found_in_trash' => sprintf(__('No %s Found In Trash', 'woo-pricing-rules'), 'Form'), 'parent_item_colon' => sprintf(__('Parent %s'), 'Form'), 'menu_name' => 'Custom Product Options' ); $args = array( 'labels' => apply_filters($post_type . '_labels', $labels), 'description' => '', 'public' => false, 'publicly_queryable' => false, 'exclude_from_search' => true, 'show_ui' => false, 'show_in_menu' => 'edit.php?post_type=product', 'show_in_nav_menus' => false, 'query_var' => false, 'can_export' => true, 'rewrite' => false, 'capability_type' => 'post', 'has_archive' => false, 'rest_base' => $post_type, 'hierarchical' => false, 'show_in_rest' => false, 'rest_controller_class' => 'WP_REST_Posts_Controller', 'supports' => array('title'), 'menu_position' => 5, 'menu_icon' => 'dashicons-admin-post', 'taxonomies' => array('product_cat') ); register_post_type($post_type, apply_filters($post_type . '_register_args', $args, $post_type)); // Product Lists $post_type = AWDP_PRODUCT_LIST; $labels = array( 'name' => 'Product Lists', 'singular_name' => 'Product List', 'name_admin_bar' => 'WCPA_Form', 'add_new' => _x('Add New Product List', $post_type, 'woo-pricing-rules'), 'add_new_item' => sprintf(__('Add New %s', 'woo-pricing-rules'), 'Form'), 'edit_item' => sprintf(__('Edit %s', 'woo-pricing-rules'), 'Form'), 'new_item' => sprintf(__('New %s', 'woo-pricing-rules'), 'Form'), 'all_items' => sprintf(__('Product Lists', 'woo-pricing-rules'), 'Form'), 'view_item' => sprintf(__('View %s', 'woo-pricing-rules'), 'Form'), 'search_items' => sprintf(__('Search %s', 'woo-pricing-rules'), 'Form'), 'not_found' => sprintf(__('No %s Found', 'woo-pricing-rules'), 'Form'), 'not_found_in_trash' => sprintf(__('No %s Found In Trash', 'woo-pricing-rules'), 'Form'), 'parent_item_colon' => sprintf(__('Parent %s'), 'Form'), 'menu_name' => 'Custom Product Options' ); $args = array( 'labels' => apply_filters($post_type . '_labels', $labels), 'description' => '', 'public' => false, 'publicly_queryable' => false, 'exclude_from_search' => true, 'show_ui' => false, 'show_in_menu' => 'edit.php?post_type=product', 'show_in_nav_menus' => false, 'query_var' => false, 'can_export' => true, 'rewrite' => false, 'capability_type' => 'post', 'has_archive' => false, 'rest_base' => $post_type, 'hierarchical' => false, 'show_in_rest' => false, 'rest_controller_class' => 'WP_REST_Posts_Controller', 'supports' => array('title'), 'menu_position' => 5, 'menu_icon' => 'dashicons-admin-post', 'taxonomies' => array('product_cat') ); register_post_type($post_type, apply_filters($post_type . '_register_args', $args, $post_type)); } }