Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ( ! defined( 'ABSPATH' ) ) {
- exit; // Exit if accessed directly
- }
- if (!function_exists('write_log')) {
- function write_log ( $log ) {
- if ( true === WP_DEBUG ) {
- if ( is_array( $log ) || is_object( $log ) ) {
- error_log( print_r( $log, true ) );
- } else {
- error_log( $log );
- }
- }
- }
- }
- if ( !class_exists( 'SBS_Ninja_Order' ) ) {
- write_log("SBS_Ninja_Order class doesn't exist; creating it.");
- class SBS_Ninja_Order
- {
- private static $wooActive=false;
- private static $field_key_filter = array(
- 'subscription_package',
- 'setup_package',
- 'addon_package'
- );
- protected $formData;
- function __construct() {
- //This class requires that woocommerce is installed
- add_action('plugins_loaded','SBS_Ninja_Order::setWooActiveFlag',5);
- add_action('ninja_forms_after_submission',array($this,'processNinjaSubmission') );
- add_filter('woocommerce_checkout_fields', array($this,'fillBilling')) ;
- write_log("Iniitalized SBS_Ninja_Order");
- }
- public static function setWooActiveFlag(){
- static::$wooActive = class_exists('WooCommerce');
- }
- public function processNinjaSubmission($form_data){
- write_log("Processing a form submission");
- write_log($form_data);
- $this->formData= $form_data; //Cache the form data in the object
- //set_transient('form_data')
- $this->purgeCart();
- $this->calculateCart($form_data);
- //Save the ninja form info to billing
- //$this->fillBilling();
- }
- public function calculateCart($form_data){
- write_log("Calculating the cart");
- if (static::$wooActive){
- global $woocommerce;
- write_log("Woocommerce is active.");
- write_log("Comparing against the following keys :");
- write_log(static::$field_key_filter );
- foreach( $form_data[ 'fields' ] as $field ) { // Field settigns, including the field key and value.
- static::addNFValueToCartIfQualified($field);
- }
- }else{
- write_log("Halting cart processing. Woocommerce not active.");
- }
- }
- private static function addNFValueToCartIfQualified($field){
- write_log("Processing field with key " . $field['key']);
- if (in_array( $field['key'] , static::$field_key_filter ) ) {
- $field_values= static::makeArray( $field['value'] ) ;
- write_log("Field " . $field['key'] . ' is flagged with value(s) ');
- write_log($field_values);
- static::addArrayofIDsToCart($field_values);
- }
- }
- private static function addArrayofIDsToCart($arrOfIDs){
- global $woocommerce;
- //The field is flagged for adding a product to the cart.
- //In this case, the field value will be the product ID
- foreach ($arrOfIDs as $field_value)
- {
- write_log("Adding following product ID to cart: ");
- write_log($field_value);
- //Add the field ID to the woocommerce cart
- $woocommerce->cart->add_to_cart($field_value);
- }
- }
- private static function makeArray($val){
- if (is_array($val)) return $val;
- return array($val);
- }
- public function purgeCart(){
- if (static::$wooActive){
- global $woocommerce;
- $woocommerce->cart->empty_cart();
- }else{
- write_log("Halting cart processing. Woocommerce not active.");
- }
- }
- public function fillBilling ($fields = array() ) {
- //write_log('Attempting to process value "' . $value . '" currently containing "' . $index . '".');
- //write_log($value);
- //write_log($index);
- $formData=$this->formData;
- $billing_fields=$fields['billing'];
- //write_log("Processing the billing fields with the cached form data: ");
- //write_log($formData);
- write_log("Attempting to process fields");
- write_log($billing_fields);
- write_log("Attempting to use formdata");
- write_log($formData);
- $formFieldToWCFieldXRef=array(
- 'first_name' => 'billing_first_name',
- 'last_name' => 'billing_last_name',
- 'company_name' => 'billing_company',
- 'address_1' => 'billing_address_1',
- 'city' => 'billing_city',
- 'state' => 'billing_state',
- 'zip' => 'billing_postcode',
- 'country' => 'billing_country',
- 'email' => 'billing_email',
- 'phone_number' => 'billing_phone',
- );
- foreach($formFieldToWCFieldXRef as $form_billing_field=>$wc_billing_field){
- write_log("Billing field name is " . $form_billing_field);
- $newValue=$formData['fields'][$form_billing_field]['value'];
- write_log("changing " . $wc_billing_field . " to " . $newValue);
- $billing_fields[$wc_billing_field]['default']=$newValue;
- }
- return $fields;
- }
- }
- }
- if (! $GLOBALS['sbs_ninja-to-woo'])
- {
- write_log("Instantiating ninja forms");
- $GLOBALS['sbs_ninja-to-woo'] = new SBS_Ninja_Order();
- }
Add Comment
Please, Sign In to add comment