class-quick-donation-process.php 9.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?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();
        add_action( 'wp_loaded',array($this,'on_wp_loaded'),20);
        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);
            add_action( 'woocommerce_checkout_update_order_meta',array($this,'update_order_meta'));
            add_action( 'woocommerce_checkout_update_order_meta',  array($this,'save_order_id_db'));
24
            //add_action( 'woocommerce_email',array($this,'remove_email_actions'));
25
            add_filter( 'wc_quick_donation_cart_project_name', array($this,'change_donation_name'));
26 27 28 29
        } 
        $this->process_donation(); 
    }
    
Varun Sridharan's avatar
Varun Sridharan committed
30
    public function change_donation_name(){
31
        $post_id = $this->get_checkout_project_id(); 
Varun Sridharan's avatar
Varun Sridharan committed
32
        $title = get_the_title($post_id);
33 34 35
        $link = get_permalink($post_id);
        $link = sprintf('<a href="%s"> %s </a> ',$link,$title);
        return $link;        
Varun Sridharan's avatar
Varun Sridharan committed
36 37 38 39
    }
    
    
    
40 41 42
    
    public function remove_email_actions($email_class ){
            // New order emails
Varun Sridharan's avatar
Varun Sridharan committed
43
            $email = $email_class;
44
            remove_action('woocommerce_order_status_pending_to_processing_notification',
Varun Sridharan's avatar
Varun Sridharan committed
45
                            array($email->emails['WC_Email_New_Order'],'trigger'));
46
            remove_action('woocommerce_order_status_pending_to_completed_notification',
Varun Sridharan's avatar
Varun Sridharan committed
47
                            array($email->emails['WC_Email_New_Order'],'trigger'));
48
            remove_action('woocommerce_order_status_pending_to_on-hold_notification',
Varun Sridharan's avatar
Varun Sridharan committed
49
                            array($email->emails['WC_Email_New_Order'],'trigger'));
50
            remove_action('woocommerce_order_status_failed_to_processing_notification',
Varun Sridharan's avatar
Varun Sridharan committed
51
                            array($email->emails['WC_Email_New_Order'],'trigger'));
52
            remove_action('woocommerce_order_status_failed_to_completed_notification',
Varun Sridharan's avatar
Varun Sridharan committed
53
                            array($email->emails['WC_Email_New_Order'],'trigger'));
54
            remove_action('woocommerce_order_status_failed_to_on-hold_notification',
Varun Sridharan's avatar
Varun Sridharan committed
55
                            array($email->emails['WC_Email_New_Order'],'trigger'));
56 57 58

            // Processing order emails
            remove_action('woocommerce_order_status_pending_to_processing_notification',
Varun Sridharan's avatar
Varun Sridharan committed
59
                            array($email->emails['WC_Email_Customer_Processing_Order'],'trigger'));
60
            remove_action('woocommerce_order_status_pending_to_on-hold_notification',
Varun Sridharan's avatar
Varun Sridharan committed
61
                            array($email->emails['WC_Email_Customer_Processing_Order'],'trigger'));
62 63 64

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

        
            add_action('woocommerce_order_status_failed_to_processing_notification',
Varun Sridharan's avatar
Varun Sridharan committed
69
                            array($email->emails[WC_QD_DB.'new_donation_email'],'trigger'));
70
            add_action('woocommerce_order_status_failed_to_completed_notification',
Varun Sridharan's avatar
Varun Sridharan committed
71
                            array($email->emails[WC_QD_DB.'new_donation_email'],'trigger'));
72
            add_action('woocommerce_order_status_failed_to_on-hold_notification',
Varun Sridharan's avatar
Varun Sridharan committed
73
                            array($email->emails[WC_QD_DB.'new_donation_email'],'trigger'));       
74 75 76 77 78 79 80 81
        
        
        
        
    }
     
    public function process_donation(){
        if(isset($_POST['donation_add'])){
Varun Sridharan's avatar
Varun Sridharan committed
82 83 84
            if($this->check_donation_already_exist()){
                $message = WC_QD()->db()->get_message(WC_QD_DB.'donation_already_exist');
                wc_add_notice($message,'error');
85 86 87 88 89 90 91 92
                $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
93 94
            }
            
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
            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;
            
            $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 188
    public function redirect_cart($key = ''){
    	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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
            $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;
        }
    }

    public function update_order_meta($order_id){
        global $woocommerce;

        update_post_meta( $order_id,"_is_donation",true);
        update_post_meta( $order_id,"_project_details",$woocommerce->session->projects);
    }
    
    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;
        $project_id = intval($woocommerce->session->projects);
        $user_id = get_current_user_id();
        WC_QD()->db()->add_db_option($order_id,$project_id,$user_id);
Varun Sridharan's avatar
Varun Sridharan committed
221 222 223 224 225 226 227
    }
    
    
    public function get_checkout_project_id(){
        global $woocommerce;
        $post_id = $woocommerce->session->projects;
        return $post_id;
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    }
    
	/**
	 * 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
248
                       ?>