*/ class Wc_Fpp_Public { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Wc_Fpp_Loader as all of the hooks are defined * in that particular class. * * The Wc_Fpp_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wc-fpp-public.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_scripts() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Wc_Fpp_Loader as all of the hooks are defined * in that particular class. * * The Wc_Fpp_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_script( $this->plugin_name . 'jquery-mask', plugin_dir_url( __FILE__ ) . 'js/jquery.mask.min.js', array('jquery'), $this->version, true ); wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wc-fpp-public.js', array( 'jquery', $this->plugin_name . 'jquery-mask' ), $this->version, true ); } public function display_shipping_estimate_modal() { global $product; require('partials/shipping-estimate-modal.php'); } public function ajax_shipping_estimate() { $data = $_GET; $shipping_response = $this->get_product_shipping_estimate( $_GET ); $response_html = '
'; if( !is_array($shipping_response) ){ $response_html .= '

Nenhuma forma de entrega disponível.

'; } else { $response_html .= ''; } $response_html .= '
'; echo $response_html; wp_die(); } public function get_product_shipping_estimate( array $request ) { $product = wc_get_product( sanitize_text_field( $request['product_id'] ) ); if (!$product->needs_shipping() || get_option('woocommerce_calc_shipping') === 'no' ) return __('Não foi possível calcular a entrega deste produto', 'sage'); if( !$product->is_in_stock() ) return __('Não foi possível calcular a entrega deste produto, pois o mesmo não está disponível.', 'sage'); if( !WC_Validation::is_postcode( $request['postcode'], WC()->customer->get_shipping_country() ) ) return __('Por favor, insira um CEP válido.', 'sage'); $products = array($product); if ( WC()->customer->get_shipping_country() == 'BR' && class_exists('WC_Correios_Autofill_Addresses') ) { $address = WC_Correios_Autofill_Addresses::get_address( sanitize_text_field( $request['postcode'] ) ); $destination = [ 'country' => 'BR', 'state' => $address->state, 'postcode' => $address->postcode, 'city' => $address->city, 'address' => $address->address, 'address_2' => '', ]; } else if ( WC()->customer->get_shipping_country() ) { $destination = [ 'country' => WC()->customer->get_shipping_country(), 'state' => WC()->customer->get_shipping_state(), 'postcode' => sanitize_text_field( $request['postcode'] ), 'city' => WC()->customer->get_shipping_city(), 'address' => WC()->customer->get_shipping_address(), 'address_2' => WC()->customer->get_shipping_address_2(), ]; } else { $destination = wc_get_customer_default_location(); } $package = [ 'destination' => $destination, 'applied_coupons' => WC()->cart->applied_coupons, 'user' => ['ID' => get_current_user_id()], ]; foreach ($products as $data) { $cart_id = WC()->cart->generate_cart_id($data->id, $product->is_type('variable') ? $data->variation_id : 0); $price = $data->get_price_excluding_tax(); $tax = $data->get_price_including_tax() - $price; $package['contents'] = [ $cart_id => [ 'product_id' => $data->id, 'data' => $data, 'quantity' => sanitize_text_field( $request['quantity'] ), 'line_total' => $price, 'line_tax' => $tax, 'line_subtotal' => $price, 'line_subtotal_tax' => $tax, 'contents_cost' => $price, ] ]; if( class_exists('WC_Correios_Webservice') ): add_filter( 'woocommerce_correios_shipping_args', function( $array, $this_id, $this_instance_id, $this_package ) use( $price ){ $option_id = 'woocommerce_'.$this_id.'_'.$this_instance_id.'_settings'; $settings = get_option( $option_id ); if( 'yes' == $settings['declare_value'] ) { $array['nVlValorDeclarado'] = $price; } return $array; },10,4 ); endif; $methods = WC_Shipping::instance()->load_shipping_methods($package); foreach ($methods as $key => $method) { if( "free_shipping" == $method->id && 'yes' == $method->enabled ) { $GLOBALS['method'] = $method; $has_coupon = $has_met_min_amount = false; if ( in_array( $method->requires, array( 'coupon', 'either', 'both' ) ) ) { if ( $coupons = WC()->cart->get_coupons() ) { foreach ( $coupons as $code => $coupon ) { if ( $coupon->is_valid() && $coupon->get_free_shipping() ) { $has_coupon = true; break; } } } } if ( in_array( $method->requires, array( 'min_amount', 'either', 'both' ) ) ) { $_total = $price * $request['qty']; if ( $_total >= $method->min_amount ) { $has_met_min_amount = true; } } switch ( $method->requires ) { case 'min_amount' : $is_available = $has_met_min_amount; break; case 'coupon' : $is_available = $has_coupon; break; case 'both' : $is_available = $has_met_min_amount && $has_coupon; break; case 'either' : $is_available = $has_met_min_amount || $has_coupon; break; default : $is_available = false; break; } break; } } if( $is_available ) { $rates[] = (object) [ 'cost' => 0, 'label' => $method->method_title ]; } $package_rates = WC_Shipping::instance()->calculate_shipping_for_package($package); foreach ($package_rates['rates'] as $rate) { $meta = $rate->get_meta_data(); if( isset( $meta['_delivery_forecast'] ) ) $rate->set_label( $rate->get_label() . " (Entrega em " . $meta['_delivery_forecast'] . " dias úteis)" ); $rates[] = $rate; } // if( $rates ){ // dd($destination); // WC()->customer->set_shipping_location( $destination['country'], $destination['state'], $destination['postcode'], $destination['city'] ); // WC()->customer->set_billing_location( $destination['country'], $destination['state'], $destination['postcode'], $destination['city'] ); // } } return $rates; } }