Created
May 27, 2015 15:08
-
-
Save niko-afv/3f453beb67399ca5c866 to your computer and use it in GitHub Desktop.
Functions.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| error_reporting(E_ALL ^ E_NOTICE);error_reporting(E_ALL ^ E_NOTICE); | |
| /** | |
| * Theme function | |
| * | |
| * @version $Id$ | |
| * @package wpbase | |
| * @author WPOpal Team <opalwordpress@gmail.com, support@wpopal.com> | |
| * @copyright Copyright (C) 2014 wpopal.com. All Rights Reserved. | |
| * @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html | |
| * | |
| * @website http://www.wpopal.com | |
| * @support http://www.wpopal.com/support/forum.html | |
| */ | |
| if( !defined("WPB_VC_VERSION") ){ | |
| define("WPB_VC_VERSION",'4.3.3'); | |
| } | |
| define( 'WPO_THEME_DIR', get_template_directory() ); | |
| define( 'WPO_THEME_SUB_DIR', WPO_THEME_DIR.'/sub/' ); | |
| define( 'WPO_THEME_CSS_DIR', WPO_THEME_DIR.'/css/' ); | |
| define( 'WPO_THEME_URI', get_template_directory_uri() ); | |
| define( 'WPO_THEME_NAME', 'shopping' ); | |
| define( 'WPO_THEME_VERSION', '1.0' ); | |
| define( 'WPO_WOOCOMMERCE_ACTIVED', in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ); | |
| if ( is_user_logged_in() ) { | |
| $user_id = get_current_user_id(); | |
| $oUser = new WP_User($user_id); | |
| //print_r($oUser->roles); | |
| }elseif(get_server_name() == WP_HOME_B2B){ | |
| //echo "entra 1"; | |
| if(current_page_url() != get_server_name() . '/my-account/') { | |
| echo "entra 2"; | |
| header('Location: ' . get_server_name() . '/my-account/'); | |
| } | |
| } | |
| //echo "sale"; | |
| //die; | |
| /* | |
| * Include list of files from Opal Framework. | |
| */ | |
| require_once( WPO_THEME_DIR . '/framework/loader.php' ); | |
| require_once( WPO_THEME_DIR . '/sub/theme.php' ); | |
| require_once( WPO_THEME_DIR . '/sub/startup.php' ); | |
| /* | |
| * Localization | |
| */ | |
| $lang = WPO_THEME_DIR . '/languages' ; | |
| load_theme_textdomain( TEXTDOMAIN, $lang ); | |
| /** | |
| * Create variant objects to modify and proccess actions of only theme. | |
| */ | |
| require_once( WPO_THEME_DIR . '/sub/pagebuilder.php' ); | |
| /* | |
| * Shortcodes | |
| */ | |
| require_once( WPO_THEME_DIR. '/shortcode.php' ); | |
| /* | |
| * Create & start up instance of framework in application. | |
| */ | |
| /// include list of functions to process logics of worpdress not support 3rd-plugins. | |
| require_once( WPO_THEME_DIR . '/sub/functions/theme.php' ); | |
| /// WooCommerce specified functions | |
| if( WPO_WOOCOMMERCE_ACTIVED ) { | |
| require_once( WPO_THEME_DIR . '/sub/functions/woocommerce.php' ); | |
| } | |
| // Add save percent next to sale item prices. | |
| //add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 ); | |
| //function woocommerce_custom_sales_price( $price, $product ) { | |
| // $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ); | |
| // return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' ); | |
| //} | |
| // Hook in | |
| add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); | |
| // Our hooked in function - $fields is passed via the filter! | |
| function custom_override_checkout_fields( $fields ) { | |
| unset($fields['billing']['billing_postcode']); | |
| unset($fields['billing']['billing_state']); | |
| $locales = get_posts(array( | |
| 'post_type' => 'local', | |
| 'posts_per_page' => -1 | |
| )); | |
| foreach ($locales as $local) $options[$local->post_title] = __($local->post_title, 'woocommerce'); | |
| /*$fields['billing']['pickup_store'] = array( | |
| 'label' => __('Tienda de retiro', 'woocommerce'), | |
| 'type' => 'select', | |
| 'required' => true, | |
| 'class' => array('form-row-wide'), | |
| 'clear' => true, | |
| 'options' => $options | |
| );*/ | |
| $fields['billing']['billing_accept_term'] = array( | |
| 'label' => __('Acepto la <a href="/privacidad">política de privacidad</a>', 'woocommerce'), | |
| 'type' => 'checkbox', | |
| 'required' => true, | |
| 'class' => array('form-row-wide'), | |
| 'clear' => true | |
| ); | |
| $fields['billing']['rut_billing'] = array( | |
| 'label' => __('Rut Facturación.'), | |
| 'type' => 'text', | |
| 'required' => false, | |
| 'class' => array('form-row-wide','company_billing_data'), | |
| 'clear' => true | |
| ); | |
| $fields['billing']['giro_billing'] = array( | |
| 'label' => __('Giro Facturación.'), | |
| 'type' => 'text', | |
| 'required' => false, | |
| 'class' => array('form-row-wide','company_billing_data'), | |
| 'clear' => true | |
| ); | |
| $fields['billing']['billing_company'] = array( | |
| 'label' => __('Nombre de la empresa'), | |
| 'type' => 'text', | |
| 'required' => false, | |
| 'class' => array('form-row-wide','company_billing_data'), | |
| 'clear' => true | |
| ); | |
| $fields['billing']['billing_invoide'] = array( | |
| 'label' => __('¿Realizar la compra con factura?.'), | |
| 'type' => 'checkbox', | |
| 'required' => false, | |
| 'class' => array('form-row-wide'), | |
| 'clear' => true | |
| ); | |
| global $wpdb; | |
| $consulta = "SELECT * FROM BLUEX_CIUDADES"; | |
| $resultado = $wpdb->get_results( $consulta ); | |
| $ciudades = array(); | |
| foreach ( $resultado as $fila ): | |
| $ciudades[$fila->ID] = __($fila->NOMBRE, 'woocommerce' ); | |
| endforeach; | |
| $fields['billing']['billing_ciudad'] = array( | |
| 'label' => __('Ciudad', 'woocommerce'), | |
| 'placeholder' => _x('', 'placeholder', 'woocommerce'), | |
| 'required' => false, | |
| 'clear' => true, | |
| 'type' => 'select', | |
| 'class' => array('city_drop'), | |
| 'options' => $ciudades | |
| ); | |
| $fields['billing']['billing_city'] = array( | |
| 'label' => __('Localidad', 'woocommerce'), | |
| 'placeholder' => _x('', 'placeholder', 'woocommerce'), | |
| 'required' => true, | |
| 'clear' => true, | |
| 'type' => 'select', | |
| 'class' => array('comuna_drop'), | |
| 'options' => array(0=>'Selecciones Localidad') | |
| ); | |
| //Order fields billing | |
| $ordered_fields = array(); | |
| $order = array( | |
| 'billing_first_name', 'billing_last_name', | |
| 'billing_address_1', | |
| 'billing_address_2', 'billing_ciudad', | |
| 'billing_city', 'billing_email', | |
| 'billing_phone', 'billing_invoide', | |
| 'rut_billing', 'giro_billing', | |
| 'billing_company', | |
| 'billing_accept_term' | |
| ); | |
| foreach ($order as $field) { | |
| $ordered_fields[$field] = $fields['billing'][$field]; | |
| } | |
| $fields['billing'] = $ordered_fields; | |
| return $fields; | |
| } | |
| add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); | |
| function my_custom_checkout_field_process() { | |
| // Comprobar si el campo ha sido completado, en caso contrario agregar un error. | |
| if ($_POST['billing_invoide']) { | |
| if(!$_POST['rut_billing']){ | |
| wc_add_notice( '<strong>Rut Facturación:</strong> ' . __( 'Es un campo requerdio si compras con factura.', 'woocommerce' ), 'error' ); | |
| }elseif(!esRut($_POST['rut_billing'])){ | |
| wc_add_notice( '<strong>Rut Facturación:</strong> ' . __( 'El Rut ingresado no es valido.', 'woocommerce' ), 'error' ); | |
| } | |
| if(!$_POST['giro_billing']) { | |
| wc_add_notice( '<strong>Giro Facturación:</strong> ' . __( 'Es un campo requerdio si compras con factura.', 'woocommerce' ), 'error' ); | |
| } | |
| if(!$_POST['billing_company']) { | |
| wc_add_notice( '<strong>Giro Facturación:</strong> ' . __( 'Es un campo requerdio si compras con factura.', 'woocommerce' ), 'error' ); | |
| } | |
| } | |
| } | |
| add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta'); | |
| function my_custom_checkout_field_update_order_meta( $order_id ) { | |
| if ($_POST['billing_invoide']) update_post_meta( $order_id, 'Boleta/Factura', esc_attr($_POST['billing_invoide'])); | |
| if ($_POST['rut_billing']) update_post_meta( $order_id, 'Rut Facturación', esc_attr($_POST['rut_billing'])); | |
| if ($_POST['giro_billing']) update_post_meta( $order_id, 'Giro Facturación', esc_attr($_POST['giro_billing'])); | |
| } | |
| add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys'); | |
| function my_custom_checkout_field_order_meta_keys( $keys ) { | |
| $keys[] = 'billing_invoide'; | |
| $keys[] = 'rut_billing'; | |
| $keys[] = 'giro_billing'; | |
| return $keys; | |
| } | |
| add_action( 'wp_ajax_load_comunas', 'loadComunas'); | |
| add_action( 'wp_ajax_nopriv_load_comunas', 'loadComunas'); | |
| function loadComunas(){ | |
| global $wpdb; | |
| $city_id = $_POST['city_id']; | |
| $consulta = "SELECT * FROM BLUEX_COMUNAS WHERE CIUDAD_ID = " . $city_id; | |
| $resultado = $wpdb->get_results( $consulta ); | |
| $select = ""; | |
| //print_r($consulta); | |
| foreach ( $resultado as $fila ): | |
| $select .= "<option value='".$fila->NOMBRE."'>".$fila->NOMBRE."</option>"; | |
| endforeach; | |
| $response = array(); | |
| $response['error'] = FALSE; | |
| $response['msg'] = "Todo Ok"; | |
| $response['data'] = $select; | |
| echo json_encode($response); | |
| exit; | |
| } | |
| function get_server_name(){ | |
| $pageURL = 'http'; | |
| if( isset($_SERVER["HTTPS"]) ) { | |
| if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} | |
| } | |
| $pageURL .= "://"; | |
| if ($_SERVER["SERVER_PORT"] != "80") { | |
| $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"]; | |
| } else { | |
| $pageURL .= $_SERVER["SERVER_NAME"]; | |
| } | |
| return $pageURL; | |
| } | |
| function current_page_url() { | |
| $pageURL = get_server_name(); | |
| $pageURL .= $_SERVER["REQUEST_URI"]; | |
| return $pageURL; | |
| } | |
| function esRut($r = false){ | |
| if((!$r) or (is_array($r))) | |
| return false; /* Hace falta el rut */ | |
| if(!$r = preg_replace('|[^0-9kK]|i', '', $r)) | |
| return false; /* Era código basura */ | |
| if(!((strlen($r) == 8) or (strlen($r) == 9))) | |
| return false; /* La cantidad de carácteres no es válida. */ | |
| $v = strtoupper(substr($r, -1)); | |
| if(!$r = substr($r, 0, -1)) | |
| return false; | |
| if(!((int)$r > 0)) | |
| return false; /* No es un valor numérico */ | |
| $x = 2; $s = 0; | |
| for($i = (strlen($r) - 1); $i >= 0; $i--){ | |
| if($x > 7) | |
| $x = 2; | |
| $s += ($r[$i] * $x); | |
| $x++; | |
| } | |
| $dv=11-($s % 11); | |
| if($dv == 10) | |
| $dv = 'K'; | |
| if($dv == 11) | |
| $dv = '0'; | |
| if($dv == $v) | |
| return number_format($r, 0, '', '.').'-'.$v; /* Formatea el RUT */ | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment