conditions = $conditions; $this->check_condition(); } protected function check_condition() { /** * Check rule is subtotal */ foreach ($this->conditions as $condition) { /** * Check subtotal */ if ( in_array($condition['condition'], array('subtotal', 'subtotal_ex_tax')) && $condition['operator'] == '>=' && $condition['value'] && (!$this->value || $this->value > $condition['value']) ) { $this->_subtotal_type = $condition['condition']; $this->value = $condition['value']; $this->render = true; } /** * Check country */ if ( $condition['condition'] == 'country' && $condition['value'] ) { if (!$this->_check_country($condition['value'], $condition['operator'])) { $this->value = 0; $this->render = false; continue; } } /** * Check State */ if ( $condition['condition'] == 'state' && $condition['value'] ) { if (!$this->_check_state($condition['value'], $condition['operator'])) { $this->value = 0; $this->render = false; continue; } } /** * Check City */ if ( $condition['condition'] == 'city' && $condition['value'] ) { if (!$this->_check_city($condition['value'], $condition['operator'])) { $this->value = 0; $this->render = false; continue; } } /** * Check Zipcode */ if ( $condition['condition'] == 'zipcode' && $condition['value'] ) { if (!$this->_check_zipcode($condition['value'], $condition['operator'])) { $this->value = 0; $this->render = false; continue; } } /** * Check weight */ if ( $condition['condition'] == 'weight' && $condition['value'] ) { if (!$this->_check_weight($condition['value'], $condition['operator'])) { $this->value = 0; $this->render = false; continue; } } } if (!$this->value) { $this->render = false; } } /** * Check Customer Contry * * @param type $country * @param type $operator * @return bool */ protected function _check_country($country = '', $operator = '==') { $user_country = WC()->customer->get_shipping_country(); $match = false; if ('==' == $operator) : $match = ($user_country == $country); elseif ('!=' == $operator) : $match = ($user_country != $country); endif; return $match; } /** * Check Customer State * * @param type $state * @param type $operator * @return bool */ protected function _check_state($state = '', $operator = '==') { $user_state = WC()->customer->get_shipping_state(); $match = false; if ('==' == $operator) : $match = ($user_state == $state); elseif ('!=' == $operator) : $match = ($user_state != $state); endif; return $match; } /** * Check Customer City * * @param type $city * @param type $operator * @return bool */ protected function _check_city($city = '', $operator = '==') { $user_city = WC()->customer->get_shipping_city(); $match = false; if ('==' == $operator) : $match = ($user_city == $city); elseif ('!=' == $operator) : $match = ($user_city != $city); endif; return $match; } /** * Check Customer zipcode / postcode * * @param type $zipcode * @param type $operator * @return bool */ protected function _check_zipcode($zipcode = '', $operator = '==') { $user_zipcode = WC()->customer->get_shipping_postcode(); $match = false; if ('==' == $operator) : $match = ($user_zipcode == $zipcode); elseif ('!=' == $operator) : $match = ($user_zipcode != $zipcode); endif; return $match; } /** * Check total weight * * @param type $weight * @param type $operator * @return bool */ protected function _check_weight($weight = '', $operator = '<=') { $tt_weight = WC()->cart->get_cart_contents_weight(); $match = false; if ('==' == $operator) : $match = ($tt_weight == $weight); elseif ('!=' == $operator) : $match = ($tt_weight != $weight); elseif ('>=' == $operator) : $match = ($tt_weight >= $weight); elseif ('<=' == $operator) : $match = ($tt_weight <= $weight); endif; return $match; } /** * Output HTML * * @return string */ public function output_html() { $content = ''; $style = ''; $cookieName = 'nasa_curent_per_shipping'; if(isset($_COOKIE[$cookieName])) { $cookieValue = $_COOKIE[$cookieName]; $style .= ' style="width: ' . $cookieValue. '%;"'; } if ($this->value && $this->render) { if ($this->_subtotal_type == 'subtotal_ex_tax') { $subtotal_cart = method_exists(WC()->cart, 'get_subtotal') ? WC()->cart->get_subtotal() : WC()->cart->subtotal_ex_tax; } else { $subtotal_cart = WC()->cart->subtotal; } $spend = 0; $content_cond = ''; $content_desc = ''; /** * Check free shipping */ if ($subtotal_cart < $this->value) { $spend = $this->value - $subtotal_cart; $per = intval(($subtotal_cart/$this->value)*100); $allowed_html = array( 'strong' => array(), 'a' => array( 'class' => array(), 'href' => array(), 'title' => array() ), 'span' => array( 'class' => array() ), 'br' => array() ); $content_desc .= '
' . sprintf( wp_kses(__('Spend %s more to reach FREE SHIPPING! Continue Shopping', 'elessi-theme'), $allowed_html), wc_price($spend), esc_url(get_permalink(wc_get_page_id('shop'))) ) . '
'; } /** * Congratulations! You've got free shipping! */ else { $per = 100; $content_desc .= '
'; $content_desc .= ''; $content_desc .= esc_html__("Congratulations! You've got free shipping.", 'elessi-theme'); $content_desc .= '
'; } $class_cond = 'nasa-total-condition-wrap'; $content_cond .= '
' . '
' . '' . $per . '%' . '
' . '
'; $content .= '
'; $content .= $content_cond; $content .= '
'; $content .= $content_desc; } return $content; } }