MC4WP_WooCommerce_Integration

Class MC4WP_WooCommerce_Integration.

Defined (1)

The class is defined in the following location(s).

/includes/integrations/class-woocommerce.php  
  1. class MC4WP_WooCommerce_Integration extends MC4WP_Integration { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. protected $type = 'woocommerce_checkout'; 
  7.  
  8. /** 
  9. * Constructor 
  10. */ 
  11. public function __construct() { 
  12.  
  13. parent::__construct(); 
  14.  
  15. add_filter( 'woocommerce_checkout_fields', array( $this, 'add_checkout_field' ), 20 ); 
  16.  
  17. add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'save_woocommerce_checkout_checkbox_value' ) ); 
  18. add_action( 'woocommerce_checkout_order_processed', array( $this, 'subscribe_from_woocommerce_checkout' ) ); 
  19.  
  20. /** 
  21. * @return string 
  22. */ 
  23. public function get_position() { 
  24. $opts = $this->get_options(); 
  25. return $opts['woocommerce_position']; 
  26.  
  27. /** 
  28. * @param $fields 
  29. * @return mixed 
  30. */ 
  31. public function add_checkout_field( $fields ) { 
  32.  
  33. $default = ( $this->is_prechecked() ) ? 1 : 0; 
  34. $label = $this->get_label_text(); 
  35. $position = $this->get_position(); 
  36.  
  37. $fields[ $position ]['_mc4wp_subscribe_woocommerce_checkout'] = array( 
  38. 'type' => 'checkbox',  
  39. 'label' => $label,  
  40. 'default' => $default,  
  41. ); 
  42.  
  43. return $fields; 
  44.  
  45. /** 
  46. * @param int $order_id 
  47. */ 
  48. public function save_woocommerce_checkout_checkbox_value( $order_id ) 
  49. update_post_meta( $order_id, '_mc4wp_optin', $this->checkbox_was_checked() ); 
  50.  
  51. /** 
  52. * @param int $order_id 
  53. * @return boolean 
  54. */ 
  55. public function subscribe_from_woocommerce_checkout( $order_id ) { 
  56.  
  57. $do_optin = get_post_meta( $order_id, '_mc4wp_optin', true ); 
  58.  
  59. if( $do_optin ) { 
  60.  
  61. $order = new WC_Order( $order_id ); 
  62. $email = $order->billing_email; 
  63. $merge_vars = array( 
  64. 'NAME' => "{$order->billing_first_name} {$order->billing_last_name}",  
  65. 'FNAME' => $order->billing_first_name,  
  66. 'LNAME' => $order->billing_last_name,  
  67. ); 
  68.  
  69. return $this->subscribe( $email, $merge_vars, $this->type, $order_id ); 
  70.  
  71. return false; 
  72.