class-quick-donation-process.php 9.37 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 15 16
		add_action( 'woocommerce_checkout_update_order_meta',array($this,'save_order_id_db'),1);

		add_action( 'wp_loaded',array($this,'on_wp_loaded'),20);
17 18 19 20 21 22 23
        add_filter( 'woocommerce_get_price', array($this,'get_price'),10,2);
    }
    
    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);
24
            add_filter( 'wc_quick_donation_cart_project_name', array($this,'change_donation_name'));
25 26 27 28
        } 
        $this->process_donation(); 
    }
    
Varun Sridharan's avatar
Varun Sridharan committed
29
    public function change_donation_name(){
30
        $post_id = $this->get_checkout_project_id(); 
Varun Sridharan's avatar
Varun Sridharan committed
31
        $title = get_the_title($post_id);
32 33 34
        $link = get_permalink($post_id);
        $link = sprintf('<a href="%s"> %s </a> ',$link,$title);
        return $link;        
Varun Sridharan's avatar
Varun Sridharan committed
35 36 37 38
    }
    
    
    
39 40 41
    
    public function remove_email_actions($email_class ){
            // New order emails
Varun Sridharan's avatar
Varun Sridharan committed
42
            $email = $email_class;
43
            remove_action('woocommerce_order_status_pending_to_processing_notification',
Varun Sridharan's avatar
Varun Sridharan committed
44
                            array($email->emails['WC_Email_New_Order'],'trigger'));
45
            remove_action('woocommerce_order_status_pending_to_completed_notification',
Varun Sridharan's avatar
Varun Sridharan committed
46
                            array($email->emails['WC_Email_New_Order'],'trigger'));
47
            remove_action('woocommerce_order_status_pending_to_on-hold_notification',
Varun Sridharan's avatar
Varun Sridharan committed
48
                            array($email->emails['WC_Email_New_Order'],'trigger'));
49
            remove_action('woocommerce_order_status_failed_to_processing_notification',
Varun Sridharan's avatar
Varun Sridharan committed
50
                            array($email->emails['WC_Email_New_Order'],'trigger'));
51
            remove_action('woocommerce_order_status_failed_to_completed_notification',
Varun Sridharan's avatar
Varun Sridharan committed
52
                            array($email->emails['WC_Email_New_Order'],'trigger'));
53
            remove_action('woocommerce_order_status_failed_to_on-hold_notification',
Varun Sridharan's avatar
Varun Sridharan committed
54
                            array($email->emails['WC_Email_New_Order'],'trigger'));
55 56 57

            // Processing order emails
            remove_action('woocommerce_order_status_pending_to_processing_notification',
Varun Sridharan's avatar
Varun Sridharan committed
58
                            array($email->emails['WC_Email_Customer_Processing_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_Customer_Processing_Order'],'trigger'));
61 62 63

            // Completed order emails
            remove_action('woocommerce_order_status_completed_notification',
Varun Sridharan's avatar
Varun Sridharan committed
64
                            array($email->emails['WC_Email_Customer_Completed_Order'],'trigger'));
65 66 67

        
            add_action('woocommerce_order_status_failed_to_processing_notification',
Varun Sridharan's avatar
Varun Sridharan committed
68
                            array($email->emails[WC_QD_DB.'new_donation_email'],'trigger'));
69
            add_action('woocommerce_order_status_failed_to_completed_notification',
Varun Sridharan's avatar
Varun Sridharan committed
70
                            array($email->emails[WC_QD_DB.'new_donation_email'],'trigger'));
71
            add_action('woocommerce_order_status_failed_to_on-hold_notification',
Varun Sridharan's avatar
Varun Sridharan committed
72
                            array($email->emails[WC_QD_DB.'new_donation_email'],'trigger'));       
73 74 75 76 77 78 79 80
        
        
        
        
    }
     
    public function process_donation(){
        if(isset($_POST['donation_add'])){
Varun Sridharan's avatar
Varun Sridharan committed
81 82 83
            if($this->check_donation_already_exist()){
                $message = WC_QD()->db()->get_message(WC_QD_DB.'donation_already_exist');
                wc_add_notice($message,'error');
84 85 86 87 88 89 90 91
                $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
92 93
            }
            
94 95 96 97 98 99 100 101 102 103 104 105 106 107
            global $woocommerce;
            $donateprice = isset($_POST['wc_qd_donate_project_price']) ? $_POST['wc_qd_donate_project_price'] : false;
			$projects = isset($_POST['wc_qd_donate_project_name']) && !empty($_POST['wc_qd_donate_project_name']) ? $_POST['wc_qd_donate_project_name'] : false;

            $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
108
			$woocommerce->session->is_donation_product = true;
109 110 111 112 113 114 115 116 117 118 119 120
            
            $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');
            }
            
        }
    }
    
121 122
    public function added_with_other_products(){
        global $woocommerce;
Varun Sridharan's avatar
Varun Sridharan committed
123 124
        $cart = $woocommerce->cart->get_cart();
        if(!empty($cart)){ return true;  }
125 126
        return false;
    }
Varun Sridharan's avatar
Varun Sridharan committed
127 128 129 130 131 132 133 134 135 136
    
    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;
    }
    
137 138 139
    public function check_donation_price_status($price){
    
        if(empty($price)){ 
Varun Sridharan's avatar
Varun Sridharan committed
140
            $message = WC_QD()->db()->get_message(WC_QD_DB.'empty_donation_msg');
141 142 143 144 145 146 147
            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
148
            $message = WC_QD()->db()->get_message($id,$search_replace);
149 150 151 152 153 154 155 156 157
            wc_add_notice($message,'error');
            return false;
        }
        
        return true;
    }
    
    
    public function check_min_max($project_id,$price){
Varun Sridharan's avatar
Varun Sridharan committed
158 159
        $min_required = $this->db()->min_project($project_id);
        $max_required = $this->db()->max_project($project_id);
160 161 162 163 164 165 166
        $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
167
                $message = WC_QD()->db()->get_message($id,$search_replace);
168 169 170 171 172 173 174 175 176 177 178
                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
179
                $message = WC_QD()->db()->get_message($id,$search_replace);
180 181 182 183 184 185 186
                wc_add_notice($message,'error');
                return false;
            }
        }
        return true;
    }
    
Varun Sridharan's avatar
Varun Sridharan committed
187
    public function redirect_cart($key = ''){
Varun Sridharan's avatar
Varun Sridharan committed
188
        if(empty($key)){$key = WC_QD_DB.'redirect_user';}
189
        if($this->is_donation_exists){
190
            $redirect = WC_QD()->settings()->get_option($key);
191 192 193 194 195 196 197 198 199 200 201
            $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
202
	
203 204 205 206 207 208 209 210 211
    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
212 213 214 215 216 217 218
		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
219 220 221 222 223 224 225
    }
    
    
    public function get_checkout_project_id(){
        global $woocommerce;
        $post_id = $woocommerce->session->projects;
        return $post_id;
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    }
    
	/**
	 * 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
245
?>