class-quick-donation-process.php 9.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
/**
 * functionality of the plugin.
 * @author  Varun Sridharan <varunsridharan23@gmail.com>
 */
if ( ! defined( 'WPINC' ) ) { die; }

class WooCommerce_Quick_Donation_Process extends WooCommerce_Quick_Donation  {

    public $is_donation_exists = false;
        
    function __construct(){
        parent::__construct();
Varun Sridharan's avatar
Varun Sridharan committed
14
		add_action( 'woocommerce_checkout_update_order_meta',array($this,'save_order_id_db'),1);
15 16
		
		add_action( 'pre_get_posts', array( $this, 'add_doantion_via_link' ) );
Varun Sridharan's avatar
Varun Sridharan committed
17
		add_action( 'wp_loaded',array($this,'on_wp_loaded'),20);
18 19 20
        add_filter( 'woocommerce_get_price', array($this,'get_price'),10,2);
    }
    
21 22 23 24 25 26 27 28 29 30 31
	public function add_doantion_via_link($wp_query){
		if(isset($wp_query->query_vars['donate-now'])){
			$get_string = WC_QD()->f()->encryptor('decrypt', $wp_query->query_vars['donate-now']);
			parse_str($get_string, $get_array); 
			$this->process_donation(true,$get_array['id'],$get_array['amount']);
			exit;
		}
		return $wp_query;
	}
	
	
32 33 34 35
    public function on_wp_loaded(){
        if($this->check_donation_exists_cart()){ 
            $this->is_donation_exists = true; 
            add_action('woocommerce_add_order_item_meta',array($this,'add_order_meta'),99,3);
36
            add_filter( 'wc_quick_donation_cart_project_name', array($this,'change_donation_name'));
37 38 39 40
        } 
        $this->process_donation(); 
    }
    
Varun Sridharan's avatar
Varun Sridharan committed
41
    public function change_donation_name(){
42
        $post_id = $this->get_checkout_project_id(); 
Varun Sridharan's avatar
Varun Sridharan committed
43
        $title = get_the_title($post_id);
44 45 46
        $link = get_permalink($post_id);
        $link = sprintf('<a href="%s"> %s </a> ',$link,$title);
        return $link;        
Varun Sridharan's avatar
Varun Sridharan committed
47 48 49 50
    }
    
    
    
51 52 53
    
    public function remove_email_actions($email_class ){
            // New order emails
Varun Sridharan's avatar
Varun Sridharan committed
54
            $email = $email_class;
55
            remove_action('woocommerce_order_status_pending_to_processing_notification',
Varun Sridharan's avatar
Varun Sridharan committed
56
                            array($email->emails['WC_Email_New_Order'],'trigger'));
57
            remove_action('woocommerce_order_status_pending_to_completed_notification',
Varun Sridharan's avatar
Varun Sridharan committed
58
                            array($email->emails['WC_Email_New_Order'],'trigger'));
59
            remove_action('woocommerce_order_status_pending_to_on-hold_notification',
Varun Sridharan's avatar
Varun Sridharan committed
60
                            array($email->emails['WC_Email_New_Order'],'trigger'));
61
            remove_action('woocommerce_order_status_failed_to_processing_notification',
Varun Sridharan's avatar
Varun Sridharan committed
62
                            array($email->emails['WC_Email_New_Order'],'trigger'));
63
            remove_action('woocommerce_order_status_failed_to_completed_notification',
Varun Sridharan's avatar
Varun Sridharan committed
64
                            array($email->emails['WC_Email_New_Order'],'trigger'));
65
            remove_action('woocommerce_order_status_failed_to_on-hold_notification',
Varun Sridharan's avatar
Varun Sridharan committed
66
                            array($email->emails['WC_Email_New_Order'],'trigger'));
67 68 69

            // Processing order emails
            remove_action('woocommerce_order_status_pending_to_processing_notification',
Varun Sridharan's avatar
Varun Sridharan committed
70
                            array($email->emails['WC_Email_Customer_Processing_Order'],'trigger'));
71
            remove_action('woocommerce_order_status_pending_to_on-hold_notification',
Varun Sridharan's avatar
Varun Sridharan committed
72
                            array($email->emails['WC_Email_Customer_Processing_Order'],'trigger'));
73 74 75

            // Completed order emails
            remove_action('woocommerce_order_status_completed_notification',
Varun Sridharan's avatar
Varun Sridharan committed
76
                            array($email->emails['WC_Email_Customer_Completed_Order'],'trigger'));
77 78 79

        
            add_action('woocommerce_order_status_failed_to_processing_notification',
Varun Sridharan's avatar
Varun Sridharan committed
80
                            array($email->emails[WC_QD_DB.'new_donation_email'],'trigger'));
81
            add_action('woocommerce_order_status_failed_to_completed_notification',
Varun Sridharan's avatar
Varun Sridharan committed
82
                            array($email->emails[WC_QD_DB.'new_donation_email'],'trigger'));
83
            add_action('woocommerce_order_status_failed_to_on-hold_notification',
Varun Sridharan's avatar
Varun Sridharan committed
84
                            array($email->emails[WC_QD_DB.'new_donation_email'],'trigger'));       
85 86 87 88
        
        
        
        
89 90 91 92
    } 
	
    public function process_donation($donate_add = false,$project_id = null,$amount=null){
        if(isset($_POST['donation_add']) || $donate_add === true){
Varun Sridharan's avatar
Varun Sridharan committed
93 94 95
            if($this->check_donation_already_exist()){
                $message = WC_QD()->db()->get_message(WC_QD_DB.'donation_already_exist');
                wc_add_notice($message,'error');
96 97 98 99 100 101 102 103
                $this->redirect_cart($key = WC_QD_DB.'already_exist_redirect_user');
                return false;
            }
            
            if($this->added_with_other_products()){
                $message = WC_QD()->db()->get_message(WC_QD_DB.'donation_with_other_products');
                wc_add_notice($message,'error');
                return false;
Varun Sridharan's avatar
Varun Sridharan committed
104 105
            }
            
106
            global $woocommerce;
107 108
            $donateprice = isset($_POST['wc_qd_donate_project_price']) ? $_POST['wc_qd_donate_project_price'] : $amount;
			$projects = isset($_POST['wc_qd_donate_project_name']) && !empty($_POST['wc_qd_donate_project_name']) ? $_POST['wc_qd_donate_project_name'] : $project_id;
109 110 111 112 113 114 115 116 117 118 119

            $check_donation_price = $this->check_donation_price_status($donateprice);
            if( ! $check_donation_price){return false;}

            $donate_price = floatval($donateprice);
            
            $price_check = $this->check_min_max($projects,$donate_price);
            if(!$price_check){return false;}
            
            $woocommerce->session->donation_price = $donate_price;
            $woocommerce->session->projects = $projects;
Varun Sridharan's avatar
Varun Sridharan committed
120
			$woocommerce->session->is_donation_product = true;
121 122 123 124 125 126 127 128 129 130 131 132
            
            $donation_added = $woocommerce->cart->add_to_cart(self::$donation_id);
            
            if($donation_added){
                $this->is_donation_exists = true;
                $this->redirect_cart();
                wc_add_notice('Success','success');
            }
            
        }
    }
    
133 134
    public function added_with_other_products(){
        global $woocommerce;
Varun Sridharan's avatar
Varun Sridharan committed
135 136
        $cart = $woocommerce->cart->get_cart();
        if(!empty($cart)){ return true;  }
137 138
        return false;
    }
Varun Sridharan's avatar
Varun Sridharan committed
139 140 141 142 143 144 145 146 147 148
    
    public function check_donation_already_exist(){
        global $woocommerce;
        foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            if( self::$donation_id == $_product->id ) { return true; }
        }  
        return false;
    }
    
149 150 151
    public function check_donation_price_status($price){
    
        if(empty($price)){ 
Varun Sridharan's avatar
Varun Sridharan committed
152
            $message = WC_QD()->db()->get_message(WC_QD_DB.'empty_donation_msg');
153 154 155 156 157 158 159
            wc_add_notice($message,'error');
            return false;
        }
        
        if(empty($price) || ! is_int($price) && $price == 0){
            $id = WC_QD_DB.'invalid_donation_msg';
            $search_replace = array('{donation_amount}' => $price);
Varun Sridharan's avatar
Varun Sridharan committed
160
            $message = WC_QD()->db()->get_message($id,$search_replace);
161 162 163 164 165 166 167 168 169
            wc_add_notice($message,'error');
            return false;
        }
        
        return true;
    }
    
    
    public function check_min_max($project_id,$price){
Varun Sridharan's avatar
Varun Sridharan committed
170 171
        $min_required = $this->db()->min_project($project_id);
        $max_required = $this->db()->max_project($project_id);
172 173 174 175 176 177 178
        $price = intval($price);
        
        if($min_required){
            $min_required = intval($min_required);
            if($price < $min_required){
                $id = WC_QD_DB.'min_rda_msg';
                $search_replace = array('{donation_amount}' => $price, '{min_amount}' => $min_required);
Varun Sridharan's avatar
Varun Sridharan committed
179
                $message = WC_QD()->db()->get_message($id,$search_replace);
180 181 182 183 184 185 186 187 188 189 190
                wc_add_notice($message,'error');
                return false;
            }
        }
        
        
        if($max_required){
            $max_required = intval($max_required);
            if($price > $max_required){
                $id = WC_QD_DB.'max_rda_msg';
                $search_replace = array('{donation_amount}' => $price, '{max_amount}' => $max_required);
Varun Sridharan's avatar
Varun Sridharan committed
191
                $message = WC_QD()->db()->get_message($id,$search_replace);
192 193 194 195 196 197 198
                wc_add_notice($message,'error');
                return false;
            }
        }
        return true;
    }
    
Varun Sridharan's avatar
Varun Sridharan committed
199
    public function redirect_cart($key = ''){
Varun Sridharan's avatar
Varun Sridharan committed
200
        if(empty($key)){$key = WC_QD_DB.'redirect_user';}
201
        if($this->is_donation_exists){
202
            $redirect = WC_QD()->settings()->get_option($key);
203 204 205 206 207 208 209 210 211 212 213
            $url = '';
            if($redirect == 'cart'){
               $url = WC()->cart->get_cart_url(); 
            } else if($redirect == 'checkout'){
                $url = WC()->cart->get_checkout_url(); 
            }            
            wp_safe_redirect($url);
            exit;
        }
    }

Varun Sridharan's avatar
Varun Sridharan committed
214
	
215 216 217 218 219 220 221 222 223
    public function add_order_meta($item_id, $values, $cart_item_key){
        global $woocommerce;
        wc_add_order_item_meta( $item_id, "_project_details",$woocommerce->session->projects);	
        wc_add_order_item_meta( $item_id, "_is_donation",true);
    }
    
    
    public function save_order_id_db($order_id){
        global $woocommerce;
Varun Sridharan's avatar
Varun Sridharan committed
224 225 226 227 228 229 230
		if($this->check_donation_exists_cart()){
			$project_id = intval($woocommerce->session->projects);
			$user_id = get_current_user_id(); 
			update_post_meta( $order_id,"_is_donation",true);
			update_post_meta( $order_id,"_project_details",$woocommerce->session->projects);
			WC_QD()->db()->add_db_option($order_id,$project_id,$user_id);
		}
Varun Sridharan's avatar
Varun Sridharan committed
231 232 233 234 235 236 237
    }
    
    
    public function get_checkout_project_id(){
        global $woocommerce;
        $post_id = $woocommerce->session->projects;
        return $post_id;
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    }
    
	/**
	 * Gets Donation Current Price
	 * @param  $price 
	 * @param  $product 
	 * @returns 0 | price
	 */
	public function get_price($price, $product){
		global $woocommerce;
        if($product->id == self::$donation_id){ 
            return isset($woocommerce->session->donation_price) ? floatval($woocommerce->session->donation_price) : 0; 
        } 
		return $price;
    }


}
                       
Varun Sridharan's avatar
Varun Sridharan committed
257
?>