commit 2003f39bad4a1e7585835714958d62a82086088e Author: Eric Fawcett Date: Wed Jun 16 15:54:36 2021 -0400 Initial Commit diff --git a/wc-shipping-carrier-account.php b/wc-shipping-carrier-account.php new file mode 100644 index 0000000..b493d08 --- /dev/null +++ b/wc-shipping-carrier-account.php @@ -0,0 +1,210 @@ +id = 'carrieracct'; + $this->method_title = __( 'CarrierAcct Shipping', 'carrieracct' ); + $this->method_description = __( 'Custom Shipping Method for CarrierAcct', 'carrieracct' ); + + $this->init(); + + $this->enabled = isset( $this->settings['enabled'] ) ? $this->settings['enabled'] : 'no'; + $this->title = isset( $this->settings['title'] ) ? $this->settings['title'] : __( 'CarrierAcct Shipping', 'carrieracct' ); + + } + + + /** + * Init your settings + * + * @access public + * @return void + */ + function init() + { + // Load the settings API + $this->init_form_fields(); + $this->init_settings(); + + // Save settings in admin if you have any defined + add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); + } + + + /** + * Define settings field for this shipping + * @return void + */ + function init_form_fields() + { + + $this->form_fields = array( + + 'enabled' => array( + 'title' => __( 'Enable', 'carrieracct' ), + 'type' => 'checkbox', + 'description' => __( 'Enable this shipping.', 'carrieracct' ), + 'default' => 'yes' + ), + + 'title' => array( + 'title' => __( 'Title', 'carrieracct' ), + 'type' => 'text', + 'description' => __( 'Title to be display on site', 'carrieracct' ), + 'default' => __( 'CarrierAcct Shipping', 'carrieracct' ) + ), + ); + + } + + + /** + * This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters. + * + * @access public + * @param mixed $package + * @return void + */ + public function calculate_shipping( $package = array( )) + { + + $rate = array( + 'id' => $this->id, + 'label' => $this->title, + 'cost' => '0', + 'calc_tax' => 'per_order', + 'package' => $package, + ); + + // Register the rate + $this->add_rate( $rate ); + + } + + + } + + + } + } + + + add_action( 'woocommerce_shipping_init', 'carrieracct_shipping_method' ); + + function add_carrieracct_shipping_method( $methods ) + { + $methods['carrieracct'] = 'CarrierAcct_Shipping_Method'; + return $methods; + } + + + add_filter( 'woocommerce_shipping_methods', 'add_carrieracct_shipping_method' ); + + // Add custom fields to a specific selected shipping method + add_action( 'woocommerce_after_shipping_rate', 'carrier_custom_fields', 20, 2 ); + function carrier_custom_fields( $method, $index ) + { + if + ( ! is_checkout()) return; // Only on checkout page + + $customer_carrier_method = 'carrieracct'; + + if + ( $method->id != $customer_carrier_method ) return; // Only display for "local_pickup" + + $chosen_method_id = WC()->session->chosen_shipping_methods[ $index ]; + + // If the chosen shipping method is 'legacy_local_pickup' we display + if + ($chosen_method_id == $customer_carrier_method ): + + echo '
'; + + woocommerce_form_field( 'carrier_name' , array( + 'type' => 'select', + 'class' => array('form-row-first carrier-name'), + 'required' => true, + 'options' => array( + '' => __('Select a carrier', carrieracct), + 'UPS' => __('UPS', carrieracct), + 'FedEx' => __('FedEx', carrieracct), + 'DHL' => __('DHL', carrieracct), + ) + ), WC()->checkout->get_value( 'carrier_name' )); + + woocommerce_form_field( 'carrier_number' , array( + 'type' => 'text', + 'class' => array('form-row-last carrier-number'), + 'required' => true, + 'placeholder' => 'Carrier Number', + ), WC()->checkout->get_value( 'carrier_number' )); + + echo '
'; + endif; + } + + + // Check custom fields validation + add_action('woocommerce_checkout_process', 'carrier_checkout_process'); + function carrier_checkout_process() + { + if + ( isset( $_POST['carrier_name'] ) && empty( $_POST['carrier_name'] ) ) + wc_add_notice( ( "Missing shipping carrier name." ), "error" ); + + if( isset( $_POST['carrier_number'] ) && empty( $_POST['carrier_number'] ) ) + wc_add_notice( ( "Missing shipping carrier account number." ), "error" ); + } + + + // Save custom fields to order meta data + add_action( 'woocommerce_checkout_update_order_meta', 'carrier_update_order_meta', 30, 1 ); + function carrier_update_order_meta( $order_id ) { + if( !empty( $_POST['carrier_name'] )) + update_post_meta( $order_id, '_carrier_name', sanitize_text_field( $_POST['carrier_name'] ) ); + + if( !empty( $_POST['carrier_number'] )) + update_post_meta( $order_id, '_carrier_number', sanitize_text_field( $_POST['carrier_number'] ) ); + } + + function carrier_display_in_admin( $order ){ ?> +

+

' . get_post_meta( $order->id, '_carrier_name', true ) . '
'; + echo '' . get_post_meta( $order->id, '_carrier_number', true ) . '

'; ?> +