Advertisement
emcniece

WooCommerce Price By Country Chosen dropdowns in admin

Feb 20th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 34.16 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WooCommerce Price by Country
  4. Plugin URI:  http://www.sweethomes.es
  5. Description: Allows you to set the prices of a product according to the user's country
  6. Version: 0.34
  7. Author: Sweet Homes
  8. Author URI: http://www.sweethomes.es
  9. Email: info@sweethomes.es
  10. */
  11.  
  12.  
  13. class woocommerce_price_by_country {
  14.  
  15.  
  16.     function __construct() {
  17.  
  18.         $this->idName = 'price_by_country';
  19.  
  20.         add_action( 'init', array( &$this, 'init' ), 9999999 );
  21.         add_filter( 'woocommerce_get_cart_item_from_session', array( &$this, 'get_item_from_session' ), -1, 1 );
  22.  
  23.         add_filter( 'woocommerce_get_price', array( &$this, 'maybe_return_price' ), 999, 2 );
  24.  
  25.         add_action( 'woocommerce_variable_product_bulk_edit_actions', array( &$this, 'bulk_edit' ) );
  26.  
  27.         add_action( 'woocommerce_product_after_variable_attributes', array( &$this, 'add_variable_attributes'), 1, 2 );
  28.  
  29.         add_action( 'woocommerce_product_options_pricing', array( &$this, 'add_simple_price' ), 1 );
  30.  
  31.         add_action( 'wp_enqueue_scripts', array( &$this, 'wpbc_enqueue_scripts' ), 1);
  32.  
  33.         /* NEW FUNCTION: Enable Chosen dropdowns in admin! */
  34.         add_action( 'admin_enqueue_scripts', array( &$this, 'wpbc_enqueue_scripts' ), 1);
  35.  
  36.         add_action( 'wp_footer', array( $this, 'wpbc_footer_script' ), 1000);
  37.  
  38.         if (class_exists('woocommerce_wpml')) {
  39.             add_action( 'admin_footer', array( &$this, 'wpbc_woo_multilingual_fix' ), 1000 );
  40.         }
  41.  
  42.     }
  43.  
  44.  
  45.     function init() {
  46.  
  47.         @session_start();
  48.  
  49.         add_action( 'woocommerce_process_product_meta_simple', array( &$this, 'process_product_meta' ), 1, 1 );
  50.  
  51.         add_action( 'woocommerce_process_product_meta_variable', array( &$this, 'process_product_meta_variable' ), 999, 1 );
  52.  
  53.         // Regular price displays, before variations are selected by a buyer
  54.         add_filter( 'woocommerce_grouped_price_html', array( &$this, 'maybe_return_wholesale_price' ), 1, 2 );
  55.         add_filter( 'woocommerce_variable_price_html', array( &$this, 'maybe_return_wholesale_price' ), 1, 2 );
  56.  
  57.         // Javscript related
  58.         add_filter( 'woocommerce_variation_sale_price_html', array( &$this, 'maybe_return_variation_price' ), 1, 2 );
  59.         add_filter( 'woocommerce_variation_price_html', array( &$this, 'maybe_return_variation_price' ), 1, 2 );
  60.         add_filter( 'woocommerce_variable_empty_price_html', array( &$this, 'maybe_return_variation_price_empty' ), 999, 2 );
  61.  
  62.         add_filter( 'woocommerce_product_is_visible', array( &$this, 'variation_is_visible' ), 99999, 2 );
  63.  
  64.         add_filter( 'woocommerce_available_variation', array( &$this, 'maybe_adjust_variations' ), 1, 3 );
  65.  
  66.         add_filter( 'woocommerce_is_purchasable', array( &$this, 'is_purchasable' ), 1, 2 );
  67.  
  68.         add_filter( 'woocommerce_sale_price_html', array( &$this, 'maybe_return_wholesale_price' ), 1, 2 );
  69.         add_filter( 'woocommerce_price_html', array( &$this, 'maybe_return_wholesale_price' ), 1, 2 );
  70.         add_filter( 'woocommerce_empty_price_html', array( &$this, 'maybe_return_wholesale_price' ), 1, 2 );
  71.  
  72.         add_filter( 'woocommerce_get_cart_item_from_session', array( &$this, 'get_item_from_session' ), 999, 1 );
  73.  
  74.  
  75.  
  76.         $this->settings = get_option( 'woocommerce_global_'. $this->idName . '_settings' );
  77.         $defaults = array ( 'wpbc_price_checkoutselector' => 'billing');
  78.  
  79.         if(empty($this->settings)){
  80.             $this->settings = wp_parse_args( $this->settings, $defaults );
  81.         }
  82.  
  83.  
  84.     }
  85.  
  86.  
  87.     /* USER-AGENTS Tnks to iamandrus ## http://stackoverflow.com/a/6524325 ##
  88.     ========================================================================== */
  89.     function check_user_agent ( $type = NULL ) {
  90.             $user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
  91.             if ( $type == 'bot' ) {
  92.                     // matches popular bots
  93.                     if ( preg_match ( "/googlebot|adsbot|yahooseeker|yahoobot|msnbot|watchmouse|pingdom\.com|feedfetcher-google/", $user_agent ) ) {
  94.                             return true;
  95.                             // watchmouse|pingdom\.com are "uptime services"
  96.                     }
  97.             } else if ( $type == 'browser' ) {
  98.                     // matches core browser types
  99.                     if ( preg_match ( "/mozilla\/|opera\//", $user_agent ) ) {
  100.                             return true;
  101.                     }
  102.             } else if ( $type == 'mobile' ) {
  103.                     // matches popular mobile devices that have small screens and/or touch inputs
  104.                     // mobile devices have regional trends; some of these will have varying popularity in Europe, Asia, and America
  105.                     // detailed demographics are unknown, and South America, the Pacific Islands, and Africa trends might not be represented, here
  106.                     if ( preg_match ( "/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {
  107.                             // these are the most common
  108.                             return true;
  109.                     } else if ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent ) ) {
  110.                             // these are less common, and might not be worth checking
  111.                             return true;
  112.                     }
  113.             }
  114.             return false;
  115.     }
  116.  
  117.  
  118.  
  119.     function wpbc_enqueue_scripts() {
  120.  
  121.         $assets_path = str_replace( array( 'http:', 'https:' ), '', plugins_url() ) . '/woocommerce/assets/';
  122.         wp_enqueue_script('google-jsapi', 'https://www.google.com/jsapi');
  123.         wp_enqueue_script('jquery-cookie', '//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.0/jquery.cookie.min.js', array( 'jquery' ), '1.0.0', false );
  124.  
  125.  
  126.         $ismobile = $this->check_user_agent('mobile');
  127.  
  128.         if($ismobile) {
  129.             if($this->settings['wpbc_price_checkoutselector'] == 'billing'):
  130.                 wp_enqueue_script('pbc-billing', plugin_dir_url( __FILE__ ) . 'comple/pbc-billing.js', array( 'jquery', 'wc-checkout', 'google-jsapi', 'jquery-cookie'), '1.0.0', false);
  131.             elseif($this->settings['wpbc_price_checkoutselector'] == 'shipping'):
  132.                 wp_enqueue_script('pbc-shipping', plugin_dir_url( __FILE__ ) . 'comple/pbc-shipping.js', array( 'jquery', 'wc-checkout', 'google-jsapi', 'jquery-cookie' ), '1.0.0', false);
  133.             endif;
  134.         } else {
  135.             if ( get_option( 'woocommerce_enable_chosen' ) == 'yes' ) {
  136.  
  137.                 wp_enqueue_script( 'base-chosen', $assets_path . 'js/chosen/chosen.jquery.min.js', array( 'jquery' ), '1.0.0', false );
  138.                 wp_enqueue_style( 'woocommerce_chosen_styles', $assets_path . 'css/chosen.css' );
  139.  
  140.                 if($this->settings['wpbc_price_checkoutselector'] == 'billing'):
  141.                     wp_enqueue_script('pbc-chosen', plugin_dir_url( __FILE__ ) . 'comple/pbc-chosen-billing.js', array( 'jquery', 'wc-checkout', 'google-jsapi', 'jquery-cookie',  'base-chosen' ), '1.0.0', false);
  142.                 elseif($this->settings['wpbc_price_checkoutselector'] == 'shipping'):
  143.                     wp_enqueue_script('pbc-chosen', plugin_dir_url( __FILE__ ) . 'comple/pbc-chosen-shipping.js', array( 'jquery', 'wc-checkout', 'google-jsapi', 'jquery-cookie',  'base-chosen'  ), '1.0.0', false);
  144.                 endif;
  145.  
  146.             } else {
  147.                 if($this->settings['wpbc_price_checkoutselector'] == 'billing'):
  148.                     wp_enqueue_script('pbc-billing', plugin_dir_url( __FILE__ ) . 'comple/pbc-billing.js', array( 'jquery', 'wc-checkout', 'google-jsapi', 'jquery-cookie'), '1.0.0', false);
  149.                 elseif($this->settings['wpbc_price_checkoutselector'] == 'shipping'):
  150.                     wp_enqueue_script('pbc-shipping', plugin_dir_url( __FILE__ ) . 'comple/pbc-shipping.js', array( 'jquery', 'wc-checkout', 'google-jsapi', 'jquery-cookie' ), '1.0.0', false);
  151.                 endif;
  152.             }
  153.  
  154.         }
  155.  
  156.  
  157.  
  158.     }
  159.  
  160.     function wpbc_woo_multilingual_fix(){
  161.  
  162.         // this function fix the translation of prices
  163.  
  164.         $inline_js = "<!-- WooCommerce price by country Woocomerce multilingual Fix -->\n<script type=\"text/javascript\">\n jQuery(document).ready(function($) { \n";
  165.  
  166.                 $inline_js .= "$('input[class^=\"group_level_\"]').each(function(){ \n";
  167.                     $inline_js .= "$(this).removeAttr('readonly');\n";
  168.                     $inline_js .= "$(this).parent().find('img').remove();\n";
  169.                 $inline_js .= "});\n";
  170.  
  171.         $inline_js .="\n});\n</script> \n";
  172.  
  173.         echo $inline_js;
  174.     }
  175.  
  176.  
  177.  
  178.     function wpbc_footer_script(){
  179.         echo '<pre>'.print_r("WTF", true).'</pre>';
  180.         $c = new WC_Countries();
  181.  
  182.         $basecountry = $c->get_base_country();
  183.         $inline_js = "<!-- WooCommerce price by country JavaScript-->\n<script type=\"text/javascript\">\n jQuery(document).ready(function($) { \n";
  184.         $inline_js .= "var country = $.cookie('country'); \n ";
  185.         $inline_js .= "\n if (country) { }else{ \n ";
  186.         $inline_js .= "\n if (google.loader.ClientLocation ) { \n
  187.                         var country_code = google.loader.ClientLocation.address.country_code; \n
  188.                             $.cookie('country', country_code, { expires: 7 , path: '/'  }); \n
  189.                             $('select#pbc_country_selector').val('+country_code+'); \n
  190.                             $('select#pbc_country_selector').trigger('chosen:updated') \n
  191.                         }else{ \n
  192.                             $.cookie('country', '".$basecountry."', { expires: 7 , path: '/'  }); \n
  193.                             $('select#pbc_country_selector').val('".$basecountry."'); \n
  194.                             $('select#pbc_country_selector').trigger('chosen:updated') \n
  195.                         } \n
  196.                     } \n";
  197.         $inline_js .="\n});\n</script> \n";
  198.  
  199.         echo $inline_js;
  200.     }
  201.  
  202.  
  203.  
  204.     function get_item_from_session( $item_data = '' ) {
  205.         global $woocommerce;
  206.  
  207.         if (isset($_COOKIE['country'])) {
  208.             $country = $_COOKIE['country'];
  209.         } else {
  210.             $country = "";
  211.         }
  212.         $countries = $this->get_countries();
  213.         if ( empty( $countries ) )
  214.             return;
  215.  
  216.         foreach( $this->get_countries() as $group => $element ) {
  217.  
  218.             if ( !in_array($country, $element['countries'])  )
  219.                 continue;
  220.  
  221.             $_product = get_product( $item_data['product_id'] );
  222.  
  223.             if ( isset( $item_data['variation_id'] ) && 'variable' == $_product->product_type ):
  224.                 $level_price = get_post_meta( $item_data['variation_id' ], '_' . $group . '_price', true );
  225.             elseif ( 'simple' == $_product->product_type || 'external' == $_product->product_type ):
  226.                 $level_price = get_post_meta( $item_data['product_id' ], '_' . $group . '_price', true );
  227.             else: // all other product types - possibly incompatible with custom product types added by other plugins\
  228.                 $level_price = get_post_meta( $item_data['product_id' ], '_' . $group . '_price', true );
  229.             endif;
  230.  
  231.             if ( $level_price ) {
  232.                 $item_data['data']->price = $level_price;
  233.                 $item_data['data']->regular_price = $level_price;
  234.             }
  235.  
  236.         }
  237.  
  238.         return $item_data;
  239.     }
  240.  
  241.  
  242.  
  243.  
  244.     function maybe_return_wholesale_price( $price, $_product ) {
  245.  
  246.         global $product;
  247.         global $woocommerce;
  248.  
  249.         if (isset($_COOKIE['country'])) {
  250.             $country = $_COOKIE['country'];
  251.         } else {
  252.             $country = "";
  253.         }
  254.  
  255.         $countries = $this->get_countries();
  256.         if ( empty( $countries ) )
  257.             return;
  258.  
  259.         foreach( $this->get_countries() as $group => $element ) {
  260.  
  261.             if ( !in_array($country, $element['countries'])  )
  262.                 continue;
  263.  
  264.             $vtype = 'variable';
  265.  
  266.             if ( $_product->is_type('grouped') ) {
  267.  
  268.                 $min_price = '';
  269.                 $max_price = '';
  270.  
  271.                 foreach ( $_product->get_children() as $child_id ) {
  272.  
  273.                     $child_price = get_post_meta( $child_id, '_' . $group . '_price', true );
  274.  
  275.                     if ( !$child_price )
  276.                         continue;
  277.  
  278.                     if ( $child_price < $min_price || $min_price == '' ) $min_price = $child_price;
  279.  
  280.                     if ( $child_price > $max_price || $max_price == '' ) $max_price = $child_price;
  281.  
  282.                 }
  283.  
  284.  
  285.                 $price = '<span class="from">' . __('From:', 'woocomerce-price-by-country') . ' </span>' . wc_price( $min_price );
  286.  
  287.             } elseif ( $_product->is_type( $vtype ) ) {
  288.  
  289.                 $wprice_min = get_post_meta( $_product->id, 'min_variation_' . $group . '_price', true );
  290.                 $wprice_max = get_post_meta( $_product->id, 'max_variation_' . $group . '_price', true );
  291.                 /*
  292.                 $wprice_min = $_product->get_variation_price( 'min', true ); // tnx Germán Oronoz Arbide <germanoronoz@gmail.com>
  293.                 $wprice_max = $_product->get_variation_price( 'max', true ); // tnx Germán Oronoz Arbide <germanoronoz@gmail.com>
  294.                 */
  295.  
  296.                 if ( $wprice_min !== $wprice_max ){
  297.                     $price = '<span class="from">' . __( 'From:', 'woocomerce-price-by-country') . $wprice_min . ' </span>';
  298.                 }
  299.  
  300.                 if ( !empty( $wprice_min ) && !empty( $wprice_max ) && $wprice_min == $wprice_max ){
  301.                     return $price;
  302.  
  303.                 } elseif ( !empty( $wprice_min ) ){
  304.                     $price = '<span class="from '.$group.'" >' . __( 'From:', 'woocomerce-price-by-country') . ' ' . wc_price( $wprice_min ) . ' </span>';
  305.  
  306.                 } else {
  307.  
  308.                     $wprice_min = get_post_meta( $_product->id, 'min_variation_' . $group . '_price', true );
  309.                     $wprice_max = get_post_meta( $_product->id, 'max_variation_' . $group . '_price', true );
  310.                     /*$min_price = $product->get_variation_price( 'min', true );
  311.                     $max_price = $product->get_variation_price( 'max', true );*/
  312.  
  313.                     if ($min_price != $max_price){
  314.                         $price = sprintf( __( '%1$s', 'woocommerce' ), wc_price( $min_price ) );
  315.                         $price2 = sprintf( __( '%1$s', 'woocommerce' ), wc_price( $max_price ) );
  316.                         return $price.' - '.$price2;
  317.                     } else {
  318.                         $price = sprintf( __( '%1$s', 'woocommerce' ), wc_price( $min_price ) );
  319.                         return $price;
  320.                     }
  321.  
  322.                     /*$wprice_min = $_product->get_variation_price( 'min', true ); // tnx Germán Oronoz Arbide <germanoronoz@gmail.com>
  323.                     $wprice_max = $_product->get_variation_price( 'max', true ); // tnx Germán Oronoz Arbide <germanoronoz@gmail.com>*/
  324.  
  325.                     /*$wprice_min = get_post_meta( $_product->id, '_min_variation_regular_price', true );
  326.                     $wprice_max = get_post_meta( $_product->id, '_max_variation_regular_price', true );*/
  327.  
  328.                     if ( $wprice_min !== $wprice_max ):
  329.                         $price = '<span class="from">' . __( 'From:', 'woocomerce-price-by-country') . $wprice_min . ' </span>';
  330.                     endif;
  331.  
  332.                     if (  !empty( $wprice_min ) && !empty( $wprice_max ) && $wprice_min == $wprice_max ):
  333.                         return $price;
  334.  
  335.                     elseif ( !empty( $wprice_min ) ):
  336.                         $price = '<span class="from">' . __( 'From:', 'woocomerce-price-by-country') . ' ' . wc_price( $wprice_min ) . ' </span>';
  337.  
  338.                     endif;
  339.  
  340.                 }
  341.  
  342.             } else {
  343.  
  344.                 $wprice_min = get_post_meta( $_product->id, '_' . $group . '_price', true );
  345.  
  346.                 if ( isset( $wprice_min ) && $wprice_min > 0 ){
  347.                     $price = wc_price( $wprice_min );
  348.  
  349.                 } elseif ( '' === $wprice_min ) {
  350.  
  351.                     $price = get_post_meta( $_product->id, '_price', true );
  352.                     if ( !empty( $price ) ){
  353.                         $price = wc_price( $price );
  354.                     }
  355.                 } elseif ( 0 == $wprice_min )
  356.                     $price = __( 'Free!', 'woocomerce-price-by-country' );
  357.  
  358.  
  359.             }
  360.  
  361.         }
  362.  
  363.  
  364.         //$price = '0000';
  365.  
  366.         return $price;
  367.     }
  368.  
  369.  
  370.  
  371.  
  372.     function is_purchasable( $purchasable, $_product ) {
  373.  
  374.         if (isset($_COOKIE['country'])) {
  375.             $country = $_COOKIE['country'];
  376.         } else {
  377.             $country = "";
  378.         }
  379.         $countries = $this->get_countries();
  380.  
  381.         if ( empty( $countries ) )
  382.             return $purchasable;
  383.  
  384.         foreach( $this->get_countries() as $group => $element ) {
  385.  
  386.             if ( !in_array($country, $element['countries'])  )
  387.                 continue;
  388.  
  389.             $is_variation = $_product->is_type( 'variation' );
  390.  
  391.             if ( !$is_variation )
  392.                 $is_variation = $_product->is_type( 'variable' );
  393.  
  394.             if ( $is_variation  ) {
  395.  
  396.                 // Variable products
  397.                 if ( !isset( $_product->variation_id ) )
  398.                     return $purchasable;
  399.  
  400.                 $price = get_post_meta( $_product->variation_id, 'min_variation_' . $group . '_price', true );
  401.  
  402.                 if ( !isset( $price ) )
  403.                     return $purchasable;
  404.  
  405.             } else {
  406.  
  407.                 // Simple products
  408.                 $price = get_post_meta( $_product->id, '_' . $group . '_price', false );
  409.  
  410.                 if ( !empty( $price ) )
  411.                     return true;
  412.                 else
  413.                     return $purchasable;
  414.  
  415.  
  416.             }
  417.         }
  418.  
  419.         return $purchasable;
  420.     }
  421.  
  422.  
  423.  
  424.  
  425.     function maybe_return_price( $price = '', $_product ) {
  426.  
  427.         if (isset($_COOKIE['country'])) {
  428.             $country = $_COOKIE['country'];
  429.         } else {
  430.             $country = "";
  431.         }
  432.  
  433.         $countries = $this->get_countries();
  434.         if ( empty( $countries ) )
  435.             return $price;
  436.  
  437.         foreach( $this->get_countries() as $group => $element ) {
  438.  
  439.             if ( !in_array($country, $element['countries'])  )
  440.                 continue;
  441.  
  442.  
  443.             if ( isset( $_product->variation_id ) ) {
  444.  
  445.                 //if ( isset( $_product->variation_id ) )
  446.                     $wholesale = get_post_meta( $_product->variation_id, '_' . $group . '_price', true );
  447.                 //else
  448.                 //  $wholesale = '';
  449.  
  450.                 if ( intval( $wholesale ) > 0 ) {
  451.  
  452.                     $customPrice = $wholesale;
  453.  
  454.                     //$_product->product_custom_fields[ '_' . $group . '_price' ] = array( $wholesale );
  455.                     //var_dump($_product->product_custom_fields[ '_' . $group . '_price']);
  456.  
  457.                     /*echo '<pre>';
  458.                     var_dump($group);
  459.                     var_dump($wholesale);
  460.                     var_dump($customPrice);
  461.                     echo '</pre>';*/
  462.                 }
  463.  
  464.                 //if ( isset( $_product->product_custom_fields[ '_' . $group . '_price' ] ) && is_array( $_product->product_custom_fields[ '_' . $group . '_price'] ) && $_product->product_custom_fields[ '_' . $group . '_price'][0] > 0 ) {
  465.  
  466.                 if(isset($customPrice) && $customPrice > 0){
  467.  
  468.                     $price = $customPrice;
  469.  
  470.                 } elseif ( $_product->price === '' ) {
  471.                     $price = '';
  472.  
  473.                 }elseif ($_product->price == 0 ) {
  474.                     $price = __( 'Free!', 'woocomerce-price-by-country' );
  475.  
  476.                 }
  477.                 return $price;
  478.  
  479.  
  480.             }
  481.  
  482.             $tier_price = get_post_meta( $_product->id, '_' . $group . '_price', true );
  483.  
  484.             if ( empty( $tier_price ) )
  485.                 return $price;
  486.             else
  487.                 return $tier_price;
  488.  
  489.         }
  490.  
  491.         //die();
  492.  
  493.         return $price;
  494.     }
  495.  
  496.  
  497.  
  498.  
  499.     function maybe_adjust_variations( $variation = '', $obj = '' , $variation_obj  = '') {
  500.  
  501.         if (isset($_COOKIE['country'])) {
  502.             $country = $_COOKIE['country'];
  503.         } else {
  504.             $country = "";
  505.         }
  506.  
  507.         foreach( $this->get_countries() as $group => $element ) {
  508.  
  509.             if ( !in_array($country, $element['countries'])  )
  510.                 continue;
  511.  
  512.  
  513.             $price = $this->maybe_return_variation_price( '', $variation_obj );
  514.  
  515.             $variation['price_html'] = '<span class="price">' . $price . '</span>';
  516.  
  517.         }
  518.  
  519.         return $variation;
  520.     }
  521.  
  522.  
  523.     // For WooCommerce 2.x flow, to ensure product is visible as long as a group price is set
  524.     function variation_is_visible( $visible, $vid ) {
  525.         global $product;
  526.  
  527.         if ( !isset( $product->children ) || count( $product->children ) <= 0 )
  528.             return $visible;
  529.  
  530.         $variation = new sw_pbc_dummy_variation();
  531.  
  532.         $variation->variation_id = $vid;
  533.  
  534.         $res = $this->maybe_return_variation_price( 'xxxxx', $variation );
  535.  
  536.         if ( !isset( $res ) || empty( $res ) || '' == $res )
  537.             $res = false;
  538.         else
  539.             $res = true;
  540.  
  541.         return $res;
  542.     }
  543.  
  544.  
  545.  
  546.  
  547.     function maybe_return_variation_price_empty( $price, $_product ) {
  548.         global $product;
  549.  
  550.         if (isset($_COOKIE['country'])) {
  551.             $country = $_COOKIE['country'];
  552.         } else {
  553.             $country = "";
  554.         }
  555.  
  556.         foreach( $this->get_countries() as $group => $element ) {
  557.  
  558.             if ( !in_array($country, $element['countries'])  )
  559.                 continue;
  560.  
  561.             $min_variation_wholesale_price = get_post_meta( $_product->id, 'min_variation_' . $group . '_price' , true );
  562.  
  563.             $max_variation_wholesale_price = get_post_meta( $_product->id, 'max_variation_' . $group . '_price', true );
  564.  
  565.             if ( $min_variation_wholesale_price !== $max_variation_wholesale_price ):
  566.                 $price = '<span class="from">' . __( 'From:', 'woocomerce-price-by-country') . ' ' .  wc_price( $min_variation_wholesale_price ) . ' </span>';
  567.             else:
  568.                 $price = '<span class="from">' . wc_price( $min_variation_wholesale_price ) . ' </span>';
  569.             endif;
  570.         }
  571.  
  572.         return $price;
  573.     }
  574.  
  575.  
  576.  
  577.  
  578.     // Handles getting prices for variable products
  579.     // Used by woocommerce_variable_add_to_cart() function to generate Javascript vars that are later
  580.     // automatically injected on the public facing side into a single product page.
  581.     // This price is then displayed when someone selected a variation in a dropdown
  582.     function maybe_return_variation_price( $price, $_product ) {
  583.         global $product; // parent product object - global
  584.  
  585.  
  586.         if (isset($_COOKIE['country'])) {
  587.             $country = $_COOKIE['country'];
  588.         } else {
  589.             $country = "";
  590.         }
  591.  
  592.         // Sometimes this hook runs when the price is empty but wholesale price is not,
  593.         // So check for that and handle returning a price for archive page view
  594.         // $attrs = $_product->get_attributes();
  595.  
  596.  
  597.         $is_variation = $_product->is_type( 'variation' );
  598.  
  599.         if ( !$is_variation )
  600.             $is_variation = $_product->is_type( 'variable' );
  601.  
  602.  
  603.         if ( !isset( $_product->variation_id ) && !$is_variation )
  604.                 return $price;
  605.  
  606.  
  607.         foreach( $this->get_countries() as $group => $element ) {
  608.  
  609.             // validacion de la cookie
  610.             if ( $is_variation && in_array($country, $element['countries']) ) {
  611.                 $price = wc_price( get_post_meta( $_product->variation_id, '_' . $group . '_price', true ) );
  612.                 return $price;
  613.             }
  614.         }
  615.  
  616.         foreach( $this->get_countries() as $group => $element ) {
  617.  
  618.             // validacion de la cookie
  619.             if ( in_array($country, $element['countries']) )  {
  620.  
  621.                     $wholesale = get_post_meta( $_product->variation_id, '_' . $group . '_price', true );
  622.  
  623.                     if ( intval( $wholesale ) > 0 )
  624.                         $product->product_custom_fields[ '_' . $group . '_price'] = array( $wholesale );
  625.  
  626.                     if ( is_array( $product->product_custom_fields[ '_' . $group . '_price' ] ) && $product->product_custom_fields[ '_' . $group . '_price'][0] > 0 ) {
  627.  
  628.                         $price = wc_price( $product->product_custom_fields[ '_' . $group . '_price'][0] );
  629.  
  630.                     } elseif ( $product->price === '' )
  631.  
  632.                         $price = '';
  633.  
  634.                     elseif ($product->price == 0 )
  635.  
  636.                         $price = __( 'Free!', 'woocomerce-price-by-country' );
  637.             }
  638.  
  639.         }
  640.  
  641.         return $price;
  642.     }
  643.  
  644.  
  645.  
  646.     function process_product_meta( $post_id, $post = '' ) {
  647.  
  648.         foreach( $this->get_countries() as $group => $element ) {
  649.  
  650.             if ( '' !==  stripslashes( $_POST[ $group . '_price'] ) )
  651.                 update_post_meta( $post_id, '_' . $group . '_price', stripslashes( $_POST[ $group . '_price' ] ) );
  652.             else
  653.                 delete_post_meta( $post_id, '_' . $group . '_price' );
  654.  
  655.         }
  656.     }
  657.  
  658.  
  659.     // process variable product meta
  660.     function process_product_meta_variable( $post_id ) {
  661.  
  662.         /*$this->get_roles();
  663.  
  664.         if ( empty( $this->roles ) )
  665.             return;
  666.             */
  667.  
  668.         $variable_post_ids = $_POST['variable_post_id'];
  669.  
  670.         if ( empty( $variable_post_ids ) )
  671.             return;
  672.  
  673.         foreach( $this->get_countries() as $group => $element ) {
  674.  
  675.             foreach( $variable_post_ids as $key => $id ) {
  676.  
  677.                 if ( empty( $id ) || absint( $id ) <= 0 )
  678.                     continue;
  679.  
  680.                 //if ( '' == $_POST[ $role .  '_price' ][ $key ] )
  681.                 //  continue;
  682.  
  683.                 update_post_meta( $id, '_' . $group  . '_price', $_POST[ $group .  '_price' ][ $key ] );
  684.  
  685.             }
  686.  
  687.         }
  688.  
  689.         $post_parent = $post_id;
  690.  
  691.         $children = get_posts( array(
  692.                     'post_parent'   => $post_parent,
  693.                     'posts_per_page'=> -1,
  694.                     'post_type'     => 'product_variation',
  695.                     'fields'        => 'ids'
  696.                 ) );
  697.  
  698.         $lowest_price = '';
  699.  
  700.         $highest_price = '';
  701.  
  702.         if ( $children ) {
  703.  
  704.             foreach( $this->get_countries() as $group => $element ) {
  705.  
  706.                 foreach ( $children as $child ) {
  707.  
  708.                     $child_price = get_post_meta( $child, '_' . $group . '_price', true );
  709.  
  710.  
  711.  
  712.                     if ( is_null( $child_price ) ) continue;
  713.  
  714.                     // Low price
  715.                     if ( !is_numeric( $lowest_price ) || $child_price < $lowest_price ) {
  716.                         $lowest_price = $child_price;
  717.                         /*var_dump('detro');
  718.                         var_dump($group);*/
  719.                     } else {
  720.                         $lowest_price = $child_price;
  721.                     }
  722.  
  723.                     // High price
  724.                     if ( $child_price > $highest_price )
  725.                         $highest_price = $child_price;
  726.  
  727.  
  728.                     /*
  729.                     // debug
  730.                     echo '<pre>';
  731.                     var_dump($group);
  732.                     var_dump($highest_price);
  733.                     var_dump($lowest_price);
  734.                     var_dump($child_price);
  735.                     echo '</pre>';  */
  736.  
  737.                 }
  738.  
  739.                 update_post_meta( $post_parent, '_' . $group . '_price', $lowest_price );
  740.  
  741.                 update_post_meta( $post_parent, 'min_variation_' . $group . '_price' , $lowest_price );
  742.  
  743.                 update_post_meta( $post_parent, 'max_variation_' . $group . '_price', $highest_price );
  744.  
  745.             }
  746.             // debug
  747.             //die();
  748.  
  749.         }
  750.  
  751.     }
  752.  
  753.  
  754.  
  755.  
  756.     function bulk_edit() {
  757.  
  758.         foreach( $this->get_countries() as $group => $element ) {
  759.         ?>
  760.             <option value="<?php echo $group ?>_price"><?php _e( $element['name'] . ' Price', 'woocomerce-price-by-country' ); ?></option>
  761.         <?php
  762.         }
  763.     }
  764.  
  765.  
  766.  
  767.  
  768.     function add_variable_attributes( $loop, $variation_data ) {
  769.  
  770.         foreach( $this->get_countries() as $group => $element ) {
  771.  
  772.             $wprice = get_post_meta( $variation_data['variation_post_id'], '_' . $group . '_price', true );
  773.  
  774.             if ( !$wprice )
  775.                 $wprice = '';
  776.  
  777.             ?>
  778.             <tr>
  779.                 <td>
  780.                     <div>
  781.                     <label><?php echo $element['name']; echo ' ('.get_woocommerce_currency_symbol().')'; ?> <a class="tips" data-tip="<?php _e( 'Enter the price for ', 'woocomerce-price-by-country' ); echo $element['name'] ?>" href="#">[?]</a></label>
  782.                     <input class="<?php echo $group ?>_price" type="number" size="99" name="<?php echo $group ?>_price[<?php echo $loop; ?>]" value="<?php echo $wprice ?>" step="any" min="0" placeholder="<?php _e( 'Set price ( optional )', 'woocomerce-price-by-country' ) ?>"/>
  783.                     </div>
  784.  
  785.                 </td>
  786.             </tr>
  787.             <?php
  788.         }
  789.     }
  790.  
  791.  
  792.  
  793.  
  794.     function add_simple_price() {
  795.         global $thepostid;
  796.  
  797.  
  798.  
  799.         function doer_of_stuff() {
  800.  
  801.  
  802.           return new WP_Error( 'broke',  __( '<strong>Woocommerce Price By Country</strong> - You need to setup some country groups before enter, you can add some <a target="_blank" href="'.admin_url( 'admin.php?page=wc-settings&tab=integration&section=price_by_country' ).'">here</a>', 'woocomerce-price-by-country' ));
  803.  
  804.         }
  805.  
  806.         $return = doer_of_stuff();
  807.  
  808.  
  809.         if($this->get_countries()){
  810.  
  811.             foreach( $this->get_countries() as $group => $element ) {
  812.  
  813.                 $wprice = get_post_meta( $thepostid, '_' . $group . '_price', true );
  814.  
  815.                 woocommerce_wp_text_input( array( 'id' => $group . '_price', 'class' => 'wc_input_price short', 'label' => __('Price for', 'woocomerce-price-by-country').' '.$element['name'] . ' (' . get_woocommerce_currency_symbol() . ')', 'description' => '', 'type' => 'number', 'custom_attributes' => array(
  816.                             'step'  => 'any',
  817.                             'min'   => '0'
  818.                         ), 'value' => $wprice ) );
  819.  
  820.             }
  821.         } elseif(is_wp_error( $return )){
  822.             $error_string = $return->get_error_message();
  823.             echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
  824.         }
  825.     }
  826.  
  827.  
  828.  
  829.  
  830.     function get_countries() {
  831.  
  832.         $settings = get_option( 'woocommerce_price_by_country_settings' );
  833.  
  834.  
  835.         if ( empty( $settings ) ):
  836.             return;
  837.         else:
  838.             return unserialize($settings);
  839.         endif;
  840.     }
  841.  
  842. }
  843.  
  844. // end Class
  845.  
  846.  
  847. function wpbc_no_woo_warning(){
  848.     ?>
  849.     <div class="message error"><p><?php printf(__('Woocomerce Price by Country is enabled but not effective. It requires <a href="%s">WooCommerce</a> in order to work.', 'woocomerce-price-by-country'),
  850.         'http://www.woothemes.com/woocommerce/'); ?></p></div>
  851.     <?php
  852. }
  853.  
  854.  
  855. if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
  856.     $wc_price_by_country = new woocommerce_price_by_country();
  857. } else {
  858.     add_action('admin_notices', 'wpbc_no_woo_warning');
  859.         return false;
  860. }
  861.  
  862. class sw_pbc_dummy_variation {
  863.  
  864.     function is_type() {
  865.         return true;
  866.     }
  867.  
  868. }
  869.  
  870. add_action( 'plugins_loaded', 'sw_pbc_init', 1 );
  871.  
  872. function sw_pbc_init() {
  873.  
  874.     require_once( dirname( __FILE__ ) . '/class-woocommerce-price-by-country-settings.php' );
  875.  
  876.     add_action( 'woocommerce_integrations', 'sw_pbc_pricing_init'  );
  877. }
  878.  
  879.  
  880. function sw_pbc_pricing_init( $integrations ) {
  881.  
  882.     $integrations[] = 'Woocomerce_Price_by_Country_Settings';
  883.  
  884.     return $integrations;
  885. }
  886.  
  887.  
  888. //////////////////////////////////// New
  889.  
  890. /* USER-AGENTS Tnks to iamandrus ## http://stackoverflow.com/a/6524325 ##
  891.     ========================================================================== */
  892.     function check_user_agent ( $type = NULL ) {
  893.             $user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
  894.             if ( $type == 'bot' ) {
  895.                     // matches popular bots
  896.                     if ( preg_match ( "/googlebot|adsbot|yahooseeker|yahoobot|msnbot|watchmouse|pingdom\.com|feedfetcher-google/", $user_agent ) ) {
  897.                             return true;
  898.                             // watchmouse|pingdom\.com are "uptime services"
  899.                     }
  900.             } else if ( $type == 'browser' ) {
  901.                     // matches core browser types
  902.                     if ( preg_match ( "/mozilla\/|opera\//", $user_agent ) ) {
  903.                             return true;
  904.                     }
  905.             } else if ( $type == 'mobile' ) {
  906.                     // matches popular mobile devices that have small screens and/or touch inputs
  907.                     // mobile devices have regional trends; some of these will have varying popularity in Europe, Asia, and America
  908.                     // detailed demographics are unknown, and South America, the Pacific Islands, and Africa trends might not be represented, here
  909.                     if ( preg_match ( "/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {
  910.                             // these are the most common
  911.                             return true;
  912.                     } else if ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent ) ) {
  913.                             // these are less common, and might not be worth checking
  914.                             return true;
  915.                     }
  916.             }
  917.             return false;
  918.     }
  919.  
  920.  
  921. function wpbc_footer_chosen_selector_script(){
  922.  
  923.         $inline_js1 = "<!-- WooCommerce price by country Selector JavaScript-->\n<script type=\"text/javascript\">\n jQuery(document).ready(function($) { \n";
  924.         $inline_js1 .= " $('select#pbc_country_selector').chosen( { search_contains: true } ); \n";
  925.         $inline_js1 .= " $('select#pbc_country_selector').val($.cookie('country')); \n";
  926.         $inline_js1 .= " $('select#pbc_country_selector').trigger('chosen:updated') \n";
  927.  
  928.         $inline_js1 .= " $('select#pbc_country_selector').on('change', function(evt, params) { \n";
  929.             $inline_js1 .= " var valueSel = $('#pbc_country_selector').val(); \n";
  930.             $inline_js1 .= " $.cookie('country', valueSel, { expires: 7 , path: '/' }); \n";
  931.             $inline_js1 .= " window.location.reload(true);";
  932.         $inline_js1 .= "}); \n";
  933.  
  934.     $inline_js1 .="\n});\n</script> \n";
  935.  
  936.     echo $inline_js1;
  937. }
  938.  
  939.  
  940. function wpbc_footer_selector_script(){
  941.  
  942.         $inline_js1 = "<!-- WooCommerce price by country Selector JavaScript-->\n<script type=\"text/javascript\">\n jQuery(document).ready(function($) { \n";
  943.  
  944.             $inline_js1 .= "$('select#pbc_country_selector').change(function(){ \n";
  945.                 $inline_js1 .= "var valueSel2 = $(this).val(); \n";
  946.                 $inline_js1 .= "$.cookie('country', valueSel2, { expires: 7 , path: '/' }); \n";
  947.                 $inline_js1 .= " window.location.reload(true);";
  948.             $inline_js1 .= "}); \n";
  949.     $inline_js1 .="\n});\n</script> \n";
  950.  
  951.     echo $inline_js1;
  952. }
  953.  
  954.  
  955.  
  956.  
  957.  
  958. add_action( 'get_pbc_country_dropdown', 'pbc_country_dropdown' );
  959. function pbc_country_dropdown() {
  960.  
  961.     global $woocommerce;
  962.  
  963.  
  964.     $alowedType = get_option( 'woocommerce_allowed_countries' );
  965.  
  966.  
  967.     $c = new WC_Countries();
  968.  
  969.     if($alowedType == 'specific'):
  970.  
  971.         $woocommerce_specific_allowed_countries = get_option('woocommerce_specific_allowed_countries');
  972.  
  973.         if($woocommerce_specific_allowed_countries):
  974.             $country_list = unserialize($woocommerce_specific_allowed_countries);
  975.         endif;
  976.     endif;
  977.  
  978.     //var_dump($c);
  979.  
  980.     if ( !isset( $s ) )
  981.         $s = array();
  982.  
  983.     $ismobile = check_user_agent('mobile');
  984.     if($ismobile) {
  985.         add_action( 'wp_footer','wpbc_footer_selector_script' , 1000);
  986.     } else {
  987.         if ( get_option( 'woocommerce_enable_chosen' ) == 'yes' ) {
  988.             add_action( 'wp_footer','wpbc_footer_chosen_selector_script' , 1000);
  989.         } else {
  990.             add_action( 'wp_footer','wpbc_footer_selector_script' , 1000);
  991.         }
  992.     }
  993.     $settings = get_option( 'woocommerce_global_price_by_country_settings' );
  994.     $extraClasses = $settings['wpbc_price_countrySelectorClass'];
  995.  
  996.     $output .='<select id="pbc_country_selector" name="pbc_country_selector" class="chosen_select '.$extraClasses.'">';
  997.  
  998.     if($alowedType == 'specific'):
  999.         foreach( $woocommerce_specific_allowed_countries as $k => $v ) {
  1000.             $output .='<option value="' . $v . '" ' . selected( $_COOKIE['country'], $v , false) . '>' . $c->countries[$v] . '</option> ';
  1001.         }
  1002.     else:
  1003.         foreach( $c->countries as $k => $v ) {
  1004.             $output .='<option value="' . $k . '" ' . selected( $_COOKIE['country'], $k , false) . '>' . $v . '</option> ';
  1005.         }
  1006.  
  1007.     endif;
  1008.  
  1009.  
  1010.     $output .='</select>';
  1011.  
  1012.     echo $output;
  1013. }
  1014.  
  1015. add_action( 'after_setup_theme', 'pbc_get_permited_countries' );
  1016.  
  1017.  
  1018. function pbc_get_permited_countries() {
  1019.  
  1020.     if (isset($_COOKIE['country'])) {
  1021.         $country = $_COOKIE['country'];
  1022.     } else {
  1023.         $country = "";
  1024.     }
  1025.  
  1026.     $settings = get_option( 'woocommerce_price_by_country_settings' );
  1027.  
  1028.     if($settings):
  1029.  
  1030.         $settingsArr = unserialize($settings);
  1031.  
  1032.         if(is_array($settingsArr)):
  1033.  
  1034.             foreach( $settingsArr  as $group => $element ):
  1035.  
  1036.                 if($element['countries']):
  1037.                     foreach($element['countries'] as $key => $countryL):
  1038.                         if($countryL == $country):
  1039.                             $inList = 'yes';
  1040.                         endif;
  1041.                     endforeach;
  1042.                 endif;
  1043.  
  1044.             endforeach;
  1045.         endif;
  1046.  
  1047.     endif;
  1048.     if (!isset($inList)) { $inList = "outside"; }
  1049.  
  1050.     $output = ($inList == 'yes') ? 'inside' : 'outside';
  1051.  
  1052.     return $output;
  1053.  
  1054. }
  1055.  
  1056.  
  1057. $countries = pbc_get_permited_countries();
  1058.  
  1059. if($countries == 'outside'){
  1060.     add_action('init','pbc_remove_loop_button');
  1061. }
  1062.  
  1063. function pbc_remove_loop_button(){
  1064.     remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
  1065.     remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
  1066.  
  1067.     function sw_custom_variation_price( $price, $product ) {
  1068.  
  1069.         $settingsGlobal = get_option( 'woocommerce_global_price_by_country_settings' );
  1070.         $target_product_types = array(
  1071.             'variable',
  1072.             'simple'
  1073.         );
  1074.  
  1075.         if ( in_array ( $product->product_type, $target_product_types ) ) {
  1076.             // if variable product return and empty string
  1077.             return $settingsGlobal['wpbc_outsideMesage'];
  1078.         } else {
  1079.             // return normal price
  1080.             return $price;
  1081.         }
  1082.     }
  1083.     add_filter('woocommerce_get_price_html', 'sw_custom_variation_price', 10, 2);
  1084.  
  1085. }
  1086.  
  1087.  
  1088. add_action( 'wp_print_scripts', 'pbc_dequeueScripts', 99 );
  1089.  
  1090. function pbc_dequeueScripts() {
  1091.  
  1092.     //first check that woo exists to prevent fatal errors
  1093.     if ( function_exists( 'is_woocommerce' ) ) {
  1094.         //dequeue scripts and styles
  1095.         if ( is_checkout() ) {
  1096.             wp_dequeue_script( 'wc-chosen' );
  1097.         }
  1098.     }
  1099.  
  1100. }
  1101.  
  1102.  
  1103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement