How to add custom shipping method to woocommerce cart?

Viewed 264

I have two shipping zones. For one of them, I need a custom shipping method. I added a new method. I can add it from the drop-down list in woocommerce->Shipping->Location. I want users to be able to choose by using a radio button in the cart section. At this moment this method is not working at all. This shipping method is implemented as a plugin.

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

    function costs_specified_init() {
        if ( ! class_exists( 'Shipping_Costs_Will_BE_Specified' ) ) {
            class Shipping_Costs_Will_BE_Specified extends WC_Shipping_Method {
                /**
                 * Constructor for your shipping class
                 *
                 * @access public
                 * @return void
                 */
                public function __construct() {
                    $this->id                 = 'specified_price'; // Id for your shipping method. Should be uunique.
                    $this->method_title       = __( 'Specified shipping price' );  // Title shown in admin
                    $this->method_description = __( 'Used to specify shipping price' ); // Description shown in admin

                    $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled
                    $this->title              = "Specified shipping price"; // This can be added as an setting but for this example its forced.
                    $this->supports             = array(
                        'shipping-zones',
                        'instance-settings',
                        'instance-settings-modal',
                    );

                    $this->init();
                }

                /**
                 * Init your settings
                 *
                 * @access public
                 * @return void
                 */
                function init() {
                    // Load the settings API
                    $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
                    $this->init_settings(); // This is part of the settings API. Loads settings you previously init.

                    // Save settings in admin if you have any defined
                    add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
                }

                /**
                 * calculate_shipping function.
                 *
                 * @access public
                 * @param mixed $package
                 * @return void
                 */
                public function calculate_shipping( $package = array() ) {
                    $rate = array(
                        'label' => $this->title,
                        'cost' => '0',
                        'calc_tax' => 'per_item'
                    );

                    // Register the rate
                    $this->add_rate( $rate );
                }
            }
        }
    }

    add_action( 'woocommerce_shipping_init', 'costs_specified_init' );

    function add_your_shipping_method( $methods ) {
        $methods['specified_price'] = 'Shipping_Costs_Will_BE_Specified';
        return $methods;
    }

    add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );
}
``
0 Answers
Related