woocommerce add new shipping method
-
Hi,
At the moment there are only 3 shipping method nativelly avaible to woocommerce 2.6.x..
– Flat Rate
– Free Shipping
– CollectionI would like to create a snippet to add a new shipping method title ‘Call store for quote’
This will abe apply if the postcode is in the range.Could someone please give me some pointer as to how to achieve that please?
Fred
-
You can create plugin for this:
Create one folder inside wp-content/pluigins/ with name “woocommerce-call-store-for-quote”
Inside “woocommerce-call-store-for-quote” folder, create two files as displayed below:
1. call-store-for-quote.php
<?php /* Plugin Name: WooCommerce Call store for quote */ /** * Check if WooCommerce is active */ $active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) ); if ( in_array( 'woocommerce/woocommerce.php', $active_plugins) ) { add_filter( 'woocommerce_shipping_methods', 'add_call_store_for_quote' ); function add_call_store_for_quote( $methods ) { $methods[] = 'WC_Call_Store_For_Quote'; return $methods; } add_action( 'woocommerce_shipping_init', 'call_store_for_quote_init' ); function call_store_for_quote_init(){ require_once 'class-call-store-for-quote.php'; } } <strong>2. class-call-store-for-quote.php</strong> <?php class WC_Call_Store_For_Quote extends WC_Shipping_Method{ public function __construct(){ $this->id = 'call_store_for_quote'; $this->method_title = __( 'Call store for quote', 'woocommerce' ); // Load the settings. $this->init_form_fields(); $this->init_settings(); // Define user set variables $this->enabled = $this->get_option( 'enabled' ); $this->title = $this->get_option( 'title' ); add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); } public function init_form_fields(){ $this->form_fields = array( 'enabled' => array( 'title' => __( 'Enable/Disable', 'woocommerce' ), 'type' => 'checkbox', 'label' => __( 'Enable Call store for quote', 'woocommerce' ), 'default' => 'yes' ), 'title' => array( 'title' => __( 'Method Tittle', 'woocommerce' ), 'type' => 'text', 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ), 'default' => __( 'Call store for quote', 'woocommerce' ), ) ); } public function is_available( $package ){ foreach ( $package['contents'] as $item_id => $values ) { $_product = $values['data']; $weight = $_product->get_weight(); if($weight > 10){ return false; } } return true; } public function calculate_shipping($package = array()){ //get the total weight and dimensions $weight = 0; $dimensions = 0; foreach ( $package['contents'] as $item_id => $values ) { $_product = $values['data']; $weight = $weight + $_product->get_weight() * $values['quantity']; $dimensions = $dimensions + (($_product->length * $values['quantity']) * $_product->width * $_product->height); } //calculate the cost according to the table switch ($weight) { case ($weight < 1): switch ($dimensions) { case ($dimensions <= 1000): $cost = 3; break; case ($dimensions > 1000): $cost = 4; break; } break; case ($weight >= 1 && $weight < 3 ): switch ($dimensions) { case ($dimensions <= 3000): $cost = 10; break; } break; case ($weight >= 3 && $weight < 10): switch ($dimensions) { case ($dimensions <= 5000): $cost = 25; break; case ($dimensions > 5000): $cost = 50; break; } break; } // send the final rate to the user. $c= $this->add_rate( array( 'id' => $this->id, 'label' => $this->title, 'cost' => $cost )); } }
- This reply was modified 8 years ago by Steven Stern (sterndata). Reason: put code in backticks
@AddWeb Solution Pvt. Ltd
Thank you very much for your help:)I copied the files as instructed and I can now see a new link called ‘Call store for quote’ but how do I add the new shipping method to my shipping for my postcode?
Thank you in advance
FredOk…
I get it, your code work base on the sipping weigth.. and is displayed automaticly in the cart checkout ??Any change you could help me further based on postcode? Is it possible?
- The topic ‘woocommerce add new shipping method’ is closed to new replies.