carlos__z

Costo de envío

May 19th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.20 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: smp
  5.  * Date: 10/09/18
  6.  * Time: 05:32 PM
  7.  */
  8.  
  9. function filters_by_cities_method() {
  10.     if ( ! class_exists( 'Filters_By_Cities_Method' ) ) {
  11.  
  12.         class Filters_By_Cities_Method extends WC_Shipping_Method
  13.         {
  14.  
  15.             /**
  16.              * Constructor for your shipping class
  17.              *
  18.              * @access public
  19.              * @return void
  20.              */
  21.             public function __construct($instance_id = 0)
  22.             {
  23.  
  24.                 parent::__construct($instance_id);
  25.  
  26.                 $this->id                 = 'filters_by_cities_shipping_method';
  27.                 $this->instance_id              = absint( $instance_id );
  28.                 $this->method_title       = __( 'Shipping filter By Cities', 'departamentos-y-ciudades-de-peru-para-woocommerce' );
  29.                 $this->method_description = __( 'Allows adding rules by city', 'departamentos-y-ciudades-de-peru-para-woocommerce' );
  30.  
  31.                 $this->supports = array(
  32.                     'settings',
  33.                     'shipping-zones',
  34.                     'instance-settings'
  35.                 );
  36.  
  37.                 $this->init();
  38.             }
  39.  
  40.             /**
  41.              * Init your settings
  42.              *
  43.              * @access public
  44.              * @return void
  45.              */
  46.             function init() {
  47.                 // Load the settings API
  48.                 $this->instance_form_fields = $this->define_instance_form_fields();
  49.                 $this->form_fields = $this->define_global_form_fields();
  50.                 $this->single_method = $this->get_option('single_method');
  51.                 $this->title = $this->get_option('title');
  52.                 $this->tax_status = $this->get_option( 'tax_status' );
  53.                 $this->cost = $this->get_option( 'cost' );
  54.                 $this->cities = $this->get_option( 'cities' );
  55.                 $this->type = $this->get_option( 'type', 'class' );
  56.  
  57.  
  58.                 $this->init_form_fields();
  59.                 $this->init_settings();
  60.  
  61.                 // Save settings in admin if you have any defined
  62.                 add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
  63.             }
  64.  
  65.  
  66.             public function define_instance_form_fields()
  67.             {
  68.  
  69.                 return include 'settings-filter-by-cities.php';
  70.             }
  71.  
  72.             public function define_global_form_fields()
  73.             {
  74.  
  75.                 return array(
  76.                     'methods' => array(
  77.                         'type' => 'rules_shipping_methods',
  78.                     ),
  79.                 );
  80.             }
  81.  
  82.             /**
  83.              * generate_rules_shipping_methods_html function.
  84.              *
  85.              * @access public
  86.              * @return string
  87.              */
  88.             public function generate_rules_shipping_methods_html()
  89.             {
  90.                 ob_start();
  91.                 include_once 'admin/html/html-shipping-methods.php';
  92.                 return ob_get_clean();
  93.             }
  94.  
  95.             /**
  96.              * @access public
  97.              * @param mixed $package
  98.              * @return void
  99.              */
  100.             public function calculate_shipping( $package = array() )
  101.             {
  102.                 $rate = array(
  103.                     'id' => $this->id,
  104.                     'label'   => $this->title,
  105.                     'cost'      => 0,
  106.                     'package' => $package,
  107.                 );
  108.  
  109.                 $city_destination = $package['destination']['city'];
  110.                 // Calculate the costs.
  111.                 $has_costs = false; // True when a cost is set. False if all costs are blank strings.
  112.                 $cost      = $this->get_option( 'cost' );
  113.                 if ( '' !== $cost ) {
  114.                     $has_costs    = true;
  115.                     $rate['cost'] = $this->evaluate_cost(
  116.                         $cost, array(
  117.                             'qty'  => $this->get_package_item_qty( $package ),
  118.                             'cost' => $package['contents_cost'],
  119.                         )
  120.                     );
  121.                 }
  122.                 // Add shipping class costs.
  123.                 $shipping_classes = WC()->shipping->get_shipping_classes();
  124.                 if ( ! empty( $shipping_classes ) ) {
  125.                     $found_shipping_classes = $this->find_shipping_classes( $package );
  126.                     $highest_class_cost     = 0;
  127.                     foreach ( $found_shipping_classes as $shipping_class => $products ) {
  128.                         // Also handles BW compatibility when slugs were used instead of ids.
  129.                         $shipping_class_term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' );
  130.                         $class_cost_string   = $shipping_class_term && $shipping_class_term->term_id ? $this->get_option( 'class_cost_' . $shipping_class_term->term_id, $this->get_option( 'class_cost_' . $shipping_class, '' ) ) : $this->get_option( 'no_class_cost', '' );
  131.                         if ( '' === $class_cost_string ) {
  132.                             continue;
  133.                         }
  134.                         $has_costs  = true;
  135.                         $class_cost = $this->evaluate_cost(
  136.                             $class_cost_string, array(
  137.                                 'qty'  => array_sum( wp_list_pluck( $products, 'quantity' ) ),
  138.                                 'cost' => array_sum( wp_list_pluck( $products, 'line_total' ) ),
  139.                             )
  140.                         );
  141.                         if ( 'class' === $this->type ) {
  142.                             $rate['cost'] += $class_cost;
  143.                         } else {
  144.                             $highest_class_cost = $class_cost > $highest_class_cost ? $class_cost : $highest_class_cost;
  145.                         }
  146.                     }
  147.                     if ( 'order' === $this->type && $highest_class_cost ) {
  148.                         $rate['cost'] += $highest_class_cost;
  149.                     }
  150.                 }
  151.  
  152.                 if ( $has_costs ) {
  153.                     $this->add_rate( $rate );
  154.                 }
  155.             }
  156.  
  157.             /**
  158.              * See if the method is available.
  159.              *
  160.              * @param array $package Package information.
  161.              * @return bool
  162.              */
  163.             public function is_available( $package )
  164.             {
  165.                 $city_destination = $package['destination']['city'];
  166.  
  167.                 if (!empty($this->cities)){
  168.                     if (!in_array($city_destination, $this->cities))
  169.                         return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', false, $package, $this );
  170.                     if ($this->single_method === 'yes')
  171.                     add_filter( 'woocommerce_package_rates', array($this, 'unset_filters_by_cities_shipping_method_zones') , 10, 2 );
  172.                 }
  173.                 return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', true, $package, $this );
  174.             }
  175.  
  176.             public function showCitiesRegions()
  177.             {
  178.                 if (!isset($_REQUEST['instance_id']))
  179.                     return array();
  180.                 $ins = WC_Shipping_Zones::get_zone_by( 'instance_id', $_REQUEST['instance_id'] );
  181.                 $data = $ins->get_data();
  182.                 if (!isset($data['zone_locations']))
  183.                     return array();
  184.                 $zones = $data['zone_locations'];
  185.  
  186.                 $cities = array();
  187.  
  188.                 foreach ($zones as $zone){
  189.                     if (strpos($zone->code, ':') !== false){
  190.                         $place = explode(':', $zone->code );
  191.                         $states = WC_States_Places_peru::get_places( $place[0] );
  192.                         $cities =  array_merge($cities,$this->orderArray($states[$place[1]]));
  193.                     }
  194.                 }
  195.  
  196.                 return $cities;
  197.             }
  198.  
  199.             public function unset_filters_by_cities_shipping_method_zones($rates, $package)
  200.             {
  201.                 $all_free_rates = array();
  202.                 foreach ( $rates as $rate_id => $rate ) {
  203.                     if ( $this->id === $rate->method_id ) {
  204.                         $all_free_rates[ $rate_id ] = $rate;
  205.                         break;
  206.                     }
  207.                 }
  208.  
  209.                 if ( empty( $all_free_rates )) {
  210.                     return $rates;
  211.                 } else {
  212.                     return $all_free_rates;
  213.                 }
  214.             }
  215.  
  216.  
  217.             public function orderArray($array)
  218.             {
  219.                 $cities = array();
  220.  
  221.                 foreach ($array as $arr){
  222.                     $cities[$arr] = $arr;
  223.                 }
  224.  
  225.                 return $cities;
  226.             }
  227.  
  228.             /**
  229.              * Finds and returns shipping classes and the products with said class.
  230.              *
  231.              * @param mixed $package Package of items from cart.
  232.              * @return array
  233.              */
  234.             public function find_shipping_classes( $package )
  235.             {
  236.                 $found_shipping_classes = array();
  237.                 foreach ( $package['contents'] as $item_id => $values ) {
  238.                     if ( $values['data']->needs_shipping() ) {
  239.                         $found_class = $values['data']->get_shipping_class();
  240.                         if ( ! isset( $found_shipping_classes[ $found_class ] ) ) {
  241.                             $found_shipping_classes[ $found_class ] = array();
  242.                         }
  243.                         $found_shipping_classes[ $found_class ][ $item_id ] = $values;
  244.                     }
  245.                 }
  246.                 return $found_shipping_classes;
  247.             }
  248.  
  249.             /**
  250.              * Evaluate a cost from a sum/string.
  251.              *
  252.              * @param  string $sum Sum of shipping.
  253.              * @param  array  $args Args.
  254.              * @return string
  255.              */
  256.             protected function evaluate_cost( $sum, $args = array() )
  257.             {
  258.                 include_once WC()->plugin_path() . '/includes/libraries/class-wc-eval-math.php';
  259.                 // Allow 3rd parties to process shipping cost arguments.
  260.                 $args           = apply_filters( 'woocommerce_evaluate_shipping_cost_args', $args, $sum, $this );
  261.                 $locale         = localeconv();
  262.                 $decimals       = array( wc_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'], ',' );
  263.                 $this->fee_cost = $args['cost'];
  264.                 // Expand shortcodes.
  265.                 add_shortcode( 'fee', array( $this, 'fee' ) );
  266.                 $sum = do_shortcode(
  267.                     str_replace(
  268.                         array(
  269.                             '[qty]',
  270.                             '[cost]',
  271.                         ),
  272.                         array(
  273.                             $args['qty'],
  274.                             $args['cost'],
  275.                         ),
  276.                         $sum
  277.                     )
  278.                 );
  279.                 remove_shortcode( 'fee' );
  280.                 // Remove whitespace from string.
  281.                 $sum = preg_replace( '/\s+/', '', $sum );
  282.                 // Remove locale from string.
  283.                 $sum = str_replace( $decimals, '.', $sum );
  284.                 // Trim invalid start/end characters.
  285.                 $sum = rtrim( ltrim( $sum, "\t\n\r\0\x0B+*/" ), "\t\n\r\0\x0B+-*/" );
  286.                 // Do the math.
  287.                 return $sum ? WC_Eval_Math::evaluate( $sum ) : 0;
  288.             }
  289.  
  290.             /**
  291.              * Get items in package.
  292.              *
  293.              * @param  array $package Package of items from cart.
  294.              * @return int
  295.              */
  296.             public function get_package_item_qty( $package ) {
  297.                 $total_quantity = 0;
  298.                 foreach ( $package['contents'] as $item_id => $values ) {
  299.                     if ( $values['quantity'] > 0 && $values['data']->needs_shipping() ) {
  300.                         $total_quantity += $values['quantity'];
  301.                     }
  302.                 }
  303.                 return $total_quantity;
  304.             }
  305.  
  306.         }
  307.     }
  308. }
Add Comment
Please, Sign In to add comment