Last active
December 29, 2021 22:18
-
-
Save victormattosvm/e4ef2d196f9bc137c0229d4dc62a65ca to your computer and use it in GitHub Desktop.
Compatibilidade do plugin Frenet com classes de entrega do Woocommerce
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 | |
| /** | |
| * COLOCAR NO FUNCTIONS.PHP DO TEMA | |
| * OU EM ALGUM PLUGIN | |
| */ | |
| /** | |
| * Add shipping class field to method settings | |
| * @param array $args Current method settings. | |
| * @return array | |
| */ | |
| function add_shipping_class_fields( $args ){ | |
| $new_field = array( | |
| 'shipping_class' => array( | |
| 'title' => __( 'Shipping Class', 'woocommerce' ), | |
| 'type' => 'multiselect', | |
| 'description' => __( 'If you want, select the authorized shipping class(es) for this method.', 'woocommerce' ), | |
| 'desc_tip' => true, | |
| 'default' => '', | |
| 'class' => 'wc-enhanced-select', | |
| 'options' => get_shipping_classes_options(), | |
| ) | |
| ); | |
| $args = array_insert_after( $args, 'zip_origin', $new_field ); | |
| return $args; | |
| } | |
| add_filter( 'woocommerce_settings_api_form_fields_frenet', 'add_shipping_class_fields' ); | |
| add_filter( 'woocommerce_shipping_instance_form_fields_frenet', 'add_shipping_class_fields' ); | |
| /** | |
| * Check if product in cart has selected shipping class | |
| * @param bool $is_available True by default. | |
| * @param array $package Package in WC Cart | |
| * @param WC_Shipping_Method $method Frenet class. | |
| * @return bool | |
| */ | |
| function has_selected_shipping_class( $is_available, $package, $method ) { | |
| $instance_id = $method->instance_id; | |
| $option_key = 'woocommerce_frenet_' . $instance_id . '_settings'; | |
| $instance_settings = get_option( $option_key ); | |
| if( ! isset ( $instance_settings['shipping_class'] ) ){ | |
| return $is_available; | |
| } | |
| $shipping_classes = $instance_settings['shipping_class']; | |
| if( ! $shipping_classes || count( $shipping_classes ) <= 0 ) { | |
| return $is_available; | |
| } | |
| $is_available = false; | |
| foreach( $package['contents'] as $item_id => $values ) { | |
| $product = $values['data']; | |
| $qty = $values['quantity']; | |
| if( $qty > 0 && $product->needs_shipping() ) { | |
| if( in_array( (int) $product->get_shipping_class_id(), $shipping_classes ) ) { | |
| $is_available = true; | |
| break; | |
| } | |
| } | |
| } | |
| return $is_available; | |
| } | |
| add_filter( 'woocommerce_shipping_frenet_is_available', 'has_selected_shipping_class', 20, 3 ); | |
| /** | |
| * Get all shipping classes | |
| * | |
| */ | |
| function get_shipping_classes_options() { | |
| $shipping_classes = WC()->shipping->get_shipping_classes(); | |
| $options = array(); | |
| if ( ! empty( $shipping_classes ) ) { | |
| $options += wp_list_pluck( $shipping_classes, 'name', 'term_id' ); | |
| } | |
| return $options; | |
| } | |
| /** | |
| * Helper to insert element after certain index in array | |
| * | |
| */ | |
| function array_insert_after(array $array, $key, array $new) | |
| { | |
| $keys = array_keys($array); | |
| $index = array_search($key, $keys); | |
| $pos = false === $index ? count($array) : $index + 1; | |
| return array_merge(array_slice($array, 0, $pos), $new, array_slice($array, $pos)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obrigada Victor!