/**
  * Process form data supplied via the checkout form.
  *
  * @access public
  * @since 1.1.0
  * @modified 1.7.1
  * @return Raise errors and adjust values if necessary
  */
 public function wcpgsk_checkout_process()
 {
     global $woocommerce;
     global $wcpgsk_session;
     if (function_exists('WC')) {
         WC()->session->set('post', $_POST);
     } else {
         $wcpgsk_session->post = $_POST;
     }
     //$wcpgsk_session->post = $_POST;
     $options = get_option('wcpgsk_settings');
     //do_action('wcpgsk_checkout_process_action');
     if (!is_user_logged_in()) {
         if (isset($options['checkoutform']['billingemailvalidator']) && $options['checkoutform']['billingemailvalidator'] == 1) {
             if ($_POST['billing_email'] && $_POST['billing_email_validator'] && strtolower($_POST['billing_email']) != strtolower($_POST['billing_email_validator'])) {
                 wcpgsk_add_error('<strong>' . __('Email addresses do not match', WCPGSK_DOMAIN) . ': ' . $_POST['billing_email'] . ' : ' . (empty($_POST['billing_email_validator']) ? __('Missing validation email', WCPGSK_DOMAIN) : $_POST['billing_email_validator']) . '</strong>');
             } elseif ($_POST['billing_email'] && !$_POST['billing_email_validator']) {
                 wcpgsk_add_error('<strong>' . __('You have to supply a validation email for: ', WCPGSK_DOMAIN) . $_POST['billing_email'] . '</strong>');
             }
         }
     }
     //Just communicate the required state to WC plus the label in billing context based on user choice and locale configuration
     //WC will take care of the error handling
     if (isset($_POST['billing_country']) && $_POST['billing_country']) {
         //Respect WC handling if desired
         $jsfields = $this->wcpgsk_get_country_locale_field_selectors();
         $locale = get_option('wcpgsk_locale', array());
         $wc_locale = $this->wcpgsk_get_country_locale();
         $countrycode = $_POST['billing_country'];
         foreach ($jsfields as $wc_field_handle => $wc_field_config) {
             if (isset($options['checkoutform']['default_' . $wc_field_handle]) && $options['checkoutform']['default_' . $wc_field_handle]) {
                 $default_required = isset($locale['default']['required_' . $wc_field_handle]) ? $locale['default']['required_' . $wc_field_handle] : '0';
                 $wc_localerequired = isset($wc_locale[$countrycode]) && isset($wc_locale[$countrycode][$wc_field_handle]) && isset($wc_locale[$countrycode][$wc_field_handle]['required']) ? $wc_locale[$countrycode][$wc_field_handle]['required'] ? '1' : '0' : (isset($wc_locale['default'][$wc_field_handle]['required']) && $wc_locale['default'][$wc_field_handle]['required'] ? '1' : $default_required);
                 $locale[$countrycode]['required_' . $wc_field_handle] = isset($locale[$countrycode]['required_' . $wc_field_handle]) ? $locale[$countrycode]['required_' . $wc_field_handle] : $wc_localerequired;
                 $required_field = $locale[$countrycode]['required_' . $wc_field_handle] ? true : false;
                 $default_label = isset($locale['default']['label_' . $wc_field_handle]) ? $locale['default']['label_' . $wc_field_handle] : '';
                 $wc_localelabel = isset($wc_locale[$countrycode]) && isset($wc_locale[$countrycode][$wc_field_handle]) && isset($wc_locale[$countrycode][$wc_field_handle]['label']) ? $wc_locale[$countrycode][$wc_field_handle]['label'] : (isset($wc_locale['default'][$wc_field_handle]['label']) ? $wc_locale['default'][$wc_field_handle]['label'] : $default_label);
                 $locale[$countrycode]['label_' . $wc_field_handle] = isset($locale[$countrycode]['label_' . $wc_field_handle]) ? $locale[$countrycode]['label_' . $wc_field_handle] : $wc_localelabel;
                 $required_label = $locale[$countrycode]['label_' . $wc_field_handle];
                 $woocommerce->checkout->checkout_fields['billing']['billing_' . $wc_field_handle]['required'] = $required_field;
                 $woocommerce->checkout->checkout_fields['billing']['billing_' . $wc_field_handle]['label'] = __($required_label, WCPGSK_DOMAIN);
             }
         }
     }
     //Just communicate the required state to WC plus the label in shipping context based on user choice and locale configuration
     //WC will take care of the error handling
     if (isset($_POST['shipping_country']) && $_POST['shipping_country']) {
         //Respect WC handling if desired
         $jsfields = $this->wcpgsk_get_country_locale_field_selectors();
         $locale = get_option('wcpgsk_locale', array());
         $wc_locale = $this->wcpgsk_get_country_locale();
         $countrycode = $_POST['shipping_country'];
         foreach ($jsfields as $wc_field_handle => $wc_field_config) {
             if (isset($options['checkoutform']['default_' . $wc_field_handle]) && $options['checkoutform']['default_' . $wc_field_handle]) {
                 $default_required = isset($locale['default']['required_' . $wc_field_handle]) ? $locale['default']['required_' . $wc_field_handle] : '0';
                 $wc_localerequired = isset($wc_locale[$countrycode]) && isset($wc_locale[$countrycode][$wc_field_handle]) && isset($wc_locale[$countrycode][$wc_field_handle]['required']) ? $wc_locale[$countrycode][$wc_field_handle]['required'] ? '1' : '0' : (isset($wc_locale['default'][$wc_field_handle]['required']) && $wc_locale['default'][$wc_field_handle]['required'] ? '1' : $default_required);
                 $locale[$countrycode]['required_' . $wc_field_handle] = isset($locale[$countrycode]['required_' . $wc_field_handle]) ? $locale[$countrycode]['required_' . $wc_field_handle] : $wc_localerequired;
                 $required_field = $locale[$countrycode]['required_' . $wc_field_handle] == '1' ? true : false;
                 $default_label = isset($locale['default']['label_' . $wc_field_handle]) ? $locale['default']['label_' . $wc_field_handle] : '';
                 $wc_localelabel = isset($wc_locale[$countrycode]) && isset($wc_locale[$countrycode][$wc_field_handle]) && isset($wc_locale[$countrycode][$wc_field_handle]['label']) ? $wc_locale[$countrycode][$wc_field_handle]['label'] : (isset($wc_locale['default'][$wc_field_handle]['label']) ? $wc_locale['default'][$wc_field_handle]['label'] : $default_label);
                 $locale[$countrycode]['label_' . $wc_field_handle] = isset($locale[$countrycode]['label_' . $wc_field_handle]) ? $locale[$countrycode]['label_' . $wc_field_handle] : $wc_localelabel;
                 $required_label = $locale[$countrycode]['label_' . $wc_field_handle];
                 $woocommerce->checkout->checkout_fields['shipping']['shipping_' . $wc_field_handle]['required'] = $required_field;
                 $woocommerce->checkout->checkout_fields['shipping']['shipping_' . $wc_field_handle]['label'] = __($required_label, WCPGSK_DOMAIN);
             }
         }
     }
     $combine = array();
     foreach ($_POST as $key => $val) {
         if (strpos($key, '_wcpgsk_repeater') !== false) {
             $testkey = str_replace('_wcpgsk_repeater', '', $key);
             if ($_POST[$key] != $_POST[$testkey]) {
                 $captured_value = $_POST[$testkey];
                 if (isset($options['woofields']['settings_' . $testkey])) {
                     $params = $this->explodeParameters($options['woofields']['settings_' . $testkey]);
                     if (isset($params) && isset($params['validate']) && !empty($params['validate']) && $params['validate'] == 'password') {
                         $captured_value = '*******';
                     }
                 }
                 $forLabel = '';
                 if (isset($options['woofields']['label_' . $testkey]) && !empty($options['woofields']['label_' . $testkey])) {
                     $forLabel = __($options['woofields']['label_' . $testkey], WCPGSK_DOMAIN);
                 }
                 wcpgsk_add_error('<strong>' . sprintf(__('You have to validate the value <em style="color:red">%s</em> for %s correctly! Please check your input.', WCPGSK_DOMAIN), $captured_value, $forLabel) . '</strong>');
             }
             unset($_POST[$key]);
         } elseif (isset($options['woofields']['billing'][$key]['custom_' . $key]) && $options['woofields']['billing'][$key]['custom_' . $key] || isset($options['woofields']['shipping'][$key]['custom_' . $key]) && $options['woofields']['shipping'][$key]['custom_' . $key]) {
             if ($options['woofields']['type_' . $key] == 'date' && !empty($_POST[$key])) {
                 //transform back based on field setting
                 $params = $this->explodeParameters($options['woofields']['settings_' . $key]);
                 $_POST[$key] = str_replace('-', '/', $_POST[$key]);
                 if (isset($params['dateformat']) && !empty($params['dateformat'])) {
                     if ($params['dateformat'] == 'dd/mm/yy') {
                         $arrdate = explode('/', $_POST[$key]);
                         $_POST[$key] = $arrdate[2] . '/' . $arrdate[1] . '/' . $arrdate[0];
                     } elseif ($params['dateformat'] == 'mm/dd/yy') {
                         $arrdate = explode('/', $_POST[$key]);
                         $_POST[$key] = $arrdate[2] . '/' . $arrdate[0] . '/' . $arrdate[1];
                     }
                 }
                 if ($this->ValidateDate($_POST[$key])) {
                     if (isset($params) && isset($params['mindays']) && !empty($params['mindays']) && (ctype_digit(strval($params['mindays'])) || is_numeric(strval($params['mindays'])))) {
                         $forLabel = '';
                         $daydiff = $this->datediffdays($_POST[$key]);
                         if ($params['mindays'] < 0) {
                             if ($daydiff < $params['mindays']) {
                                 if (isset($options['woofields']['label_' . $key]) && !empty($options['woofields']['label_' . $key])) {
                                     $forLabel = __($options['woofields']['label_' . $key], WCPGSK_DOMAIN);
                                 }
                                 $mindate = date('Y/m/d', strtotime(date("Y-m-d") . ' - ' . (absint($params['mindays']) - 1) . ' days'));
                                 wcpgsk_add_error('<strong>' . sprintf(__('Date value for <em style="color:red">%s</em> has to be set at least to <em>%s</em>!', WCPGSK_DOMAIN), $forLabel, $mindate) . '</strong>');
                             }
                         } else {
                             if ($daydiff < $params['mindays']) {
                                 if (isset($options['woofields']['label_' . $key]) && !empty($options['woofields']['label_' . $key])) {
                                     $forLabel = __($options['woofields']['label_' . $key], WCPGSK_DOMAIN);
                                 }
                                 $mindate = date('Y/m/d', strtotime(date("Y-m-d") . ' + ' . $params['mindays'] . ' days'));
                                 wcpgsk_add_error('<strong>' . sprintf(__('Date value for <em style="color:red">%s</em> has to be set at least to <em>%s</em>!', WCPGSK_DOMAIN), $forLabel, $mindate) . '</strong>');
                             }
                         }
                     } elseif (isset($params) && isset($params['mindays']) && !empty($params['mindays'])) {
                         $forLabel = '';
                         $mindays = $params['mindays'];
                         if (isset($params['dateformat']) && !empty($params['dateformat'])) {
                             if ($params['dateformat'] == 'dd/mm/yy') {
                                 $arrdate = explode('/', $params['mindays']);
                                 $mindays = $arrdate[2] . '/' . $arrdate[1] . '/' . $arrdate[0];
                             } elseif ($params['dateformat'] == 'mm/dd/yy') {
                                 $arrdate = explode('/', $params['mindays']);
                                 $mindays = $arrdate[2] . '/' . $arrdate[0] . '/' . $arrdate[1];
                             }
                         }
                         if ($_POST[$key] < $mindays) {
                             if (isset($options['woofields']['label_' . $key]) && !empty($options['woofields']['label_' . $key])) {
                                 $forLabel = __($options['woofields']['label_' . $key], WCPGSK_DOMAIN);
                             }
                             wcpgsk_add_error('<strong>' . sprintf(__('Date value for <em style="color:red">%s</em> has to be set at least to <em>%s</em>!', WCPGSK_DOMAIN), $forLabel, $params['mindays']) . '</strong>');
                         }
                     }
                     if (isset($params) && isset($params['maxdays']) && !empty($params['maxdays']) && (ctype_digit(strval($params['maxdays'])) || is_numeric(strval($params['maxdays'])))) {
                         $forLabel = '';
                         $daydiff = $this->datediffdays($_POST[$key]);
                         if ($params['maxdays'] < 0) {
                             if ($daydiff > $params['maxdays']) {
                                 if (isset($options['woofields']['label_' . $key]) && !empty($options['woofields']['label_' . $key])) {
                                     $forLabel = __($options['woofields']['label_' . $key], WCPGSK_DOMAIN);
                                 }
                                 $maxdate = date('Y/m/d', strtotime(date("Y-m-d") . ' - ' . absint($params['maxdays'] - 1) . ' days'));
                                 wcpgsk_add_error('<strong>' . sprintf(__('Date value for <em style="color:red">%s</em> has to be priorrrr to <em>%s</em>!', WCPGSK_DOMAIN), $forLabel, $maxdate) . '</strong>');
                             }
                         } else {
                             if ($daydiff > $params['maxdays']) {
                                 if (isset($options['woofields']['label_' . $key]) && !empty($options['woofields']['label_' . $key])) {
                                     $forLabel = __($options['woofields']['label_' . $key], WCPGSK_DOMAIN);
                                 }
                                 $maxdate = date('Y/m/d', strtotime(date("Y-m-d") . ' + ' . ($params['maxdays'] + 1) . ' days'));
                                 wcpgsk_add_error('<strong>' . sprintf(__('Date value for <em style="color:red">%s</em> has to be prior to <em>%s</em>!', WCPGSK_DOMAIN), $forLabel, $maxdate) . '</strong>');
                             }
                         }
                     } elseif (isset($params) && isset($params['maxdays']) && !empty($params['maxdays'])) {
                         $forLabel = '';
                         $maxdays = $params['maxdays'];
                         if (isset($params['dateformat']) && !empty($params['dateformat'])) {
                             if ($params['dateformat'] == 'dd/mm/yy') {
                                 $arrdate = explode('/', $params['maxdays']);
                                 $maxdays = $arrdate[2] . '/' . $arrdate[1] . '/' . $arrdate[0];
                             } elseif ($params['dateformat'] == 'mm/dd/yy') {
                                 $arrdate = explode('/', $params['maxdays']);
                                 $maxdays = $arrdate[2] . '/' . $arrdate[0] . '/' . $arrdate[1];
                             }
                         }
                         if ($_POST[$key] > $maxdays) {
                             if (isset($options['woofields']['label_' . $key]) && !empty($options['woofields']['label_' . $key])) {
                                 $forLabel = __($options['woofields']['label_' . $key], WCPGSK_DOMAIN);
                             }
                             wcpgsk_add_error('<strong>' . sprintf(__('Date value for <em style="color:red">%s</em> has to be prior to <em>%s</em>!', WCPGSK_DOMAIN), $forLabel, $params['maxdays']) . '</strong>');
                         }
                     }
                 } else {
                     $forLabel = '';
                     if (isset($options['woofields']['label_' . $key]) && !empty($options['woofields']['label_' . $key])) {
                         $forLabel = __($options['woofields']['label_' . $key], WCPGSK_DOMAIN);
                     }
                     wcpgsk_add_error('<strong>' . sprintf(__('You have to supply a valid date for <em style="color:red">%s</em> using the format year/month/day, e.g. 2014/12/24', WCPGSK_DOMAIN), $forLabel) . '</strong>');
                 }
             } elseif ($options['woofields']['type_' . $key] == 'select') {
                 //if ( is_array($key) ) :
                 //endif;
             }
             $combine[$key] = array();
             if (is_array($_POST[$key])) {
                 foreach ($_POST[$key] as $value) {
                     $combine[$key][] = esc_attr($value);
                 }
             } else {
                 $combine[$key][] = esc_attr($val);
             }
         }
     }
     foreach ($combine as $key => $val) {
         $_POST[$key] = implode('|', $val);
     }
 }
Пример #2
0
function wcpgsk_check_cart_items()
{
    global $woocommerce;
    wcpgsk_clear_messages();
    //wcpgsk_clear_messages();
    $options = get_option('wcpgsk_settings');
    $qtycnt = 0;
    $prodcnt = 0;
    $do_cart_redirect = false;
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
        $product_id = $values['product_id'];
        $quantity = $values['quantity'];
        $qtycnt += $quantity;
        $prodcnt++;
        $product = get_product($product_id);
        $minqty = isset($options['cart']['minqty_' . $product->product_type]) && $options['cart']['minqty_' . $product->product_type] ? $options['cart']['minqty_' . $product->product_type] : 0;
        $maxqty = isset($options['cart']['maxqty_' . $product->product_type]) && $options['cart']['maxqty_' . $product->product_type] ? $options['cart']['maxqty_' . $product->product_type] : 0;
        $stepqty = isset($options['cart']['stepqty_' . $product->product_type]) && $options['cart']['stepqty_' . $product->product_type] ? $options['cart']['stepqty_' . $product->product_type] : 0;
        if (isset($options['cart']['minmaxstepproduct']) && $options['cart']['minmaxstepproduct'] == 1) {
            $maxval = get_post_meta($product_id, '_wcpgsk_maxqty', true);
            if (isset($maxval) && $maxval > 0) {
                $maxqty = $maxval;
            }
            $minval = get_post_meta($product_id, '_wcpgsk_minqty', true);
            if (isset($minval) && $minval > 0) {
                $minqty = $minval;
            }
            $stepval = get_post_meta($product_id, '_wcpgsk_stepqty', true);
            if (isset($stepval) && $stepval > 0) {
                $stepqty = $stepval;
            }
        }
        if ($minqty > 0 && $quantity < $minqty) {
            $woocommerce->cart->set_quantity($cart_item_key, $minqty);
            if (is_checkout()) {
                wcpgsk_add_error(sprintf(__('You have to buy a minimum quantity. We have set the required minimum of %s as quantity for you.', WCPGSK_DOMAIN), $minqty));
                $do_cart_redirect = true;
            } else {
                wcpgsk_clear_messages();
                wcpgsk_add_message(sprintf(__('You have to buy a minimum quantity. We have set the required minimum of %s as quantity for you.', WCPGSK_DOMAIN), $minqty));
            }
            wcpgsk_set_messages();
        } elseif ($maxqty > 0 && $quantity > $maxqty) {
            $woocommerce->cart->set_quantity($cart_item_key, $maxqty);
            if (is_checkout()) {
                wcpgsk_add_error(sprintf(__('You cannot buy more than the allowed quantity for this product. We have set the maximum of %s as quantity for you.', WCPGSK_DOMAIN), $maxqty));
                $do_cart_redirect = true;
            } else {
                wcpgsk_clear_messages();
                wcpgsk_add_message(sprintf(__('You cannot buy more than the allowed quantity for this product. We have set the maximum of %s as quantity for you.', WCPGSK_DOMAIN), $maxqty));
            }
            wcpgsk_set_messages();
        } elseif ($stepqty > 0 && $quantity % $stepqty > 0) {
            $remainder = $quantity % $stepqty;
            $newqty = $quantity - $remainder;
            if ($newqty > 0) {
                $woocommerce->cart->set_quantity($cart_item_key, $newqty);
            } else {
                $newqty = $stepqty;
                $woocommerce->cart->set_quantity($cart_item_key, $newqty);
            }
            if (is_checkout()) {
                wcpgsk_add_error(sprintf(__('You have to buy this product in multiples of %s. We have set the product quantity to the closest lower multiple available.', WCPGSK_DOMAIN), $stepqty));
                $do_cart_redirect = true;
            } else {
                wcpgsk_clear_messages();
                wcpgsk_add_message(sprintf(__('You have to buy this product in multiples of %s. We have set the product quantity to the closest lower multiple available.', WCPGSK_DOMAIN), $stepqty));
            }
            wcpgsk_set_messages();
        }
    }
    $maxqtycart = isset($options['cart']['maxqtycart']) ? $options['cart']['maxqtycart'] : 0;
    $minqtycart = isset($options['cart']['minqtycart']) ? $options['cart']['minqtycart'] : 0;
    if ($maxqtycart && is_numeric($maxqtycart) && $qtycnt > $maxqtycart) {
        if (is_checkout()) {
            wcpgsk_add_error(sprintf(__('The overall sum for all product quantities is restricted to %s in this shop. Your overall quantity sum: %s. Please buy less quantity at least for some products.', WCPGSK_DOMAIN), $maxqtycart, $qtycnt));
            $do_cart_redirect = true;
        } else {
            wcpgsk_clear_messages();
            wcpgsk_add_message(sprintf(__('The overall sum for all product quantities is restricted to %s in this shop. Your overall quantity sum: %s. Please buy less quantity at least for some products.', WCPGSK_DOMAIN), $maxqtycart, $qtycnt));
        }
        wcpgsk_set_messages();
    }
    if ($minqtycart && is_numeric($minqtycart) && $qtycnt < $minqtycart) {
        if (is_checkout()) {
            wcpgsk_add_error(sprintf(__('The required minimum sum for all product quantities is set to %s in this shop. Your overall quantity sum: %s. You have to add products to your cart or buy existing products in a higher quantity.', WCPGSK_DOMAIN), $minqtycart, $qtycnt));
            $do_cart_redirect = true;
        } else {
            wcpgsk_clear_messages();
            wcpgsk_add_message(sprintf(__('The required minimum sum for all product quantities is set to %s in this shop. Your overall quantity sum: %s. You have to add products to your cart or buy existing products in a higher quantity.', WCPGSK_DOMAIN), $minqtycart, $qtycnt));
        }
        wcpgsk_set_messages();
    }
    if ($do_cart_redirect) {
        wp_safe_redirect(get_permalink(woocommerce_get_page_id('cart')));
    }
}