Guest User

Untitled

a guest
Nov 18th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. if ( ! defined( 'ABSPATH' ) ) {
  2. exit; // Exit if accessed directly
  3. }
  4.  
  5.  
  6. if (!function_exists('write_log')) {
  7. function write_log ( $log ) {
  8. if ( true === WP_DEBUG ) {
  9. if ( is_array( $log ) || is_object( $log ) ) {
  10. error_log( print_r( $log, true ) );
  11. } else {
  12. error_log( $log );
  13. }
  14. }
  15. }
  16. }
  17.  
  18. if ( !class_exists( 'SBS_Ninja_Order' ) ) {
  19. write_log("SBS_Ninja_Order class doesn't exist; creating it.");
  20.  
  21. class SBS_Ninja_Order
  22. {
  23. private static $wooActive=false;
  24.  
  25. private static $field_key_filter = array(
  26. 'subscription_package',
  27. 'setup_package',
  28. 'addon_package'
  29. );
  30.  
  31. protected $formData;
  32.  
  33. function __construct() {
  34. //This class requires that woocommerce is installed
  35. add_action('plugins_loaded','SBS_Ninja_Order::setWooActiveFlag',5);
  36. add_action('ninja_forms_after_submission',array($this,'processNinjaSubmission') );
  37. add_filter('woocommerce_checkout_fields', array($this,'fillBilling')) ;
  38. write_log("Iniitalized SBS_Ninja_Order");
  39. }
  40.  
  41. public static function setWooActiveFlag(){
  42. static::$wooActive = class_exists('WooCommerce');
  43. }
  44.  
  45.  
  46.  
  47. public function processNinjaSubmission($form_data){
  48. write_log("Processing a form submission");
  49. write_log($form_data);
  50.  
  51. $this->formData= $form_data; //Cache the form data in the object
  52. //set_transient('form_data')
  53.  
  54. $this->purgeCart();
  55. $this->calculateCart($form_data);
  56. //Save the ninja form info to billing
  57. //$this->fillBilling();
  58. }
  59.  
  60. public function calculateCart($form_data){
  61. write_log("Calculating the cart");
  62. if (static::$wooActive){
  63. global $woocommerce;
  64. write_log("Woocommerce is active.");
  65. write_log("Comparing against the following keys :");
  66. write_log(static::$field_key_filter );
  67. foreach( $form_data[ 'fields' ] as $field ) { // Field settigns, including the field key and value.
  68. static::addNFValueToCartIfQualified($field);
  69. }
  70. }else{
  71. write_log("Halting cart processing. Woocommerce not active.");
  72. }
  73. }
  74.  
  75. private static function addNFValueToCartIfQualified($field){
  76. write_log("Processing field with key " . $field['key']);
  77. if (in_array( $field['key'] , static::$field_key_filter ) ) {
  78. $field_values= static::makeArray( $field['value'] ) ;
  79. write_log("Field " . $field['key'] . ' is flagged with value(s) ');
  80. write_log($field_values);
  81. static::addArrayofIDsToCart($field_values);
  82. }
  83. }
  84. private static function addArrayofIDsToCart($arrOfIDs){
  85. global $woocommerce;
  86. //The field is flagged for adding a product to the cart.
  87. //In this case, the field value will be the product ID
  88. foreach ($arrOfIDs as $field_value)
  89. {
  90. write_log("Adding following product ID to cart: ");
  91. write_log($field_value);
  92. //Add the field ID to the woocommerce cart
  93. $woocommerce->cart->add_to_cart($field_value);
  94. }
  95. }
  96.  
  97.  
  98. private static function makeArray($val){
  99. if (is_array($val)) return $val;
  100. return array($val);
  101. }
  102.  
  103.  
  104. public function purgeCart(){
  105.  
  106. if (static::$wooActive){
  107. global $woocommerce;
  108. $woocommerce->cart->empty_cart();
  109. }else{
  110. write_log("Halting cart processing. Woocommerce not active.");
  111. }
  112. }
  113.  
  114.  
  115. public function fillBilling ($fields = array() ) {
  116. //write_log('Attempting to process value "' . $value . '" currently containing "' . $index . '".');
  117. //write_log($value);
  118. //write_log($index);
  119. $formData=$this->formData;
  120. $billing_fields=$fields['billing'];
  121. //write_log("Processing the billing fields with the cached form data: ");
  122. //write_log($formData);
  123.  
  124. write_log("Attempting to process fields");
  125. write_log($billing_fields);
  126. write_log("Attempting to use formdata");
  127. write_log($formData);
  128.  
  129. $formFieldToWCFieldXRef=array(
  130. 'first_name' => 'billing_first_name',
  131. 'last_name' => 'billing_last_name',
  132. 'company_name' => 'billing_company',
  133. 'address_1' => 'billing_address_1',
  134. 'city' => 'billing_city',
  135. 'state' => 'billing_state',
  136. 'zip' => 'billing_postcode',
  137. 'country' => 'billing_country',
  138. 'email' => 'billing_email',
  139. 'phone_number' => 'billing_phone',
  140. );
  141.  
  142.  
  143. foreach($formFieldToWCFieldXRef as $form_billing_field=>$wc_billing_field){
  144. write_log("Billing field name is " . $form_billing_field);
  145. $newValue=$formData['fields'][$form_billing_field]['value'];
  146.  
  147. write_log("changing " . $wc_billing_field . " to " . $newValue);
  148.  
  149. $billing_fields[$wc_billing_field]['default']=$newValue;
  150. }
  151.  
  152. return $fields;
  153.  
  154. }
  155.  
  156.  
  157. }
  158. }
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. if (! $GLOBALS['sbs_ninja-to-woo'])
  166. {
  167. write_log("Instantiating ninja forms");
  168. $GLOBALS['sbs_ninja-to-woo'] = new SBS_Ninja_Order();
  169. }
Add Comment
Please, Sign In to add comment