• Resolved fitbox

    (@fitmealat)


    Im trying to add another very simple shipping method to the new WC 2.6
    As a base i took “local pickup” (from the core) and tried to implement as follows. Where is my mistake? What am i missing?

    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    
    	function bike_shipping_method_init() {
    		if ( ! class_exists( 'WC_Shipping_Bike' ) ) {
    			class WC_Shipping_Bike extends WC_Shipping_Method {
    
    	/**
    	 * Constructor.
    	 */
    	public function __construct( $instance_id = 0 ) {
    		$this->id                    = 'local_bike';
    		$this->instance_id 			 = absint( $instance_id );
    		$this->method_title          = __( 'Local Bike Delivery', 'woocommerce' );
    		$this->method_description    = __( 'rad ja? lol', 'woocommerce' );
    		$this->supports              = array(
    			'shipping-zones',
    			'instance-settings',
    			'instance-settings-modal',
    		);
    		$this->init();
    	}
    
    	/**
    	 * Initialize local pickup.
    	 */
    	public function init() {
    
    		// Load the settings.
    		$this->init_form_fields();
    		$this->init_settings();
    
    		// Define user set variables
    		$this->title		     = $this->get_option( 'title' );
    		$this->tax_status	     = $this->get_option( 'tax_status' );
    		$this->cost	             = $this->get_option( 'cost' );
    
    		// Actions
    		add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
    	}
    
    	/**
    	 * calculate_shipping function.
    	 * Calculate local pickup shipping.
    	 */
    	public function calculate_shipping( $package = array() ) {
    		$this->add_rate( array(
    			'label' 	 => $this->title,
    			'package'    => $package,
    			'cost'       => $this->cost,
    		) );
    	}
    
    	/**
    	 * Init form fields.
    	 */
    	public function init_form_fields() {
    		$this->instance_form_fields = array(
    			'title' => array(
    				'title'       => __( 'Title', 'woocommerce' ),
    				'type'        => 'text',
    				'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
    				'default'     => __( 'Local Pickup', 'woocommerce' ),
    				'desc_tip'    => true,
    			),
    			'tax_status' => array(
    				'title' 		=> __( 'Tax Status', 'woocommerce' ),
    				'type' 			=> 'select',
    				'class'         => 'wc-enhanced-select',
    				'default' 		=> 'taxable',
    				'options'		=> array(
    					'taxable' 	=> __( 'Taxable', 'woocommerce' ),
    					'none' 		=> _x( 'None', 'Tax status', 'woocommerce' )
    				)
    			),
    			'cost' => array(
    				'title' 		=> __( 'Cost', 'woocommerce' ),
    				'type' 			=> 'text',
    				'placeholder'	=> '0',
    				'description'	=> __( 'Optional cost for local pickup.', 'woocommerce' ),
    				'default'		=> '',
    				'desc_tip'		=> true
    			)
    		);
    	}
    }
    
    		//class ends here
    		}
    	}
    
    	add_action( 'woocommerce_shipping_init', 'bike_shipping_method_init' );
    
    	function add_your_shipping_method( $methods ) {
    		$methods[] = 'WC_Shipping_Bike';
    		return $methods;
    	}
    
    	add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );
    }

    method shows up in selector field as local pickup even tought title etc have been changed? why

    It shows correct description

    Errors on Page

    https://wordpress.org/plugins/woocommerce/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Thread Starter fitbox

    (@fitmealat)

    mike you are everywhere…! thanks for the pointer will try adapt.

    i think there is an error on line 17 in the example code located here:
    https://gist.github.com/mikejolley/3b37b9cc19a774665f31

    $this->method_title = __( ‘Test Method‘ );

    Parse error: syntax error, unexpected ” ); ‘ (T_CONSTANT_ENCAPSED_STRING)

    Thread Starter fitbox

    (@fitmealat)

    even with your code from git i get same errors:
    ERROR
    this is after i select the custom shipping method from the drop down when trying to add it to a zone. It shows up in the dropdown but is not added to the zone.

    perhaps my method of implementation is wrong?

    <?php
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly
    }
    /*
    Plugin Name: eigenes hipping
    Plugin URI: fitmeal.at
    Description: asdfsfd
    Version: 1.0
    Author: Philipp Protschka
    Author URI: fitmeal.at
    License: GPL
    */
    /**
     * Check if WooCommerce is active
     */
     /**
     * Check if WooCommerce is active
     */
    
    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    
    	function bike_shipping_method_init() {
    		if ( ! class_exists( 'WC_Shipping_Bike' ) ) {
    		class WC_Shipping_Test_Method extends WC_Shipping_Method {
    
    	/**
    	 * Constructor. The instance ID is passed to this.
    	 */
    	public function __construct( $instance_id = 0 ) {
    		$this->id                    = 'test_method';
    		$this->instance_id 			     = absint( $instance_id );
    		$this->method_title          = __( 'Test Method' );
    		$this->method_description    = __( 'Some shipping method.' );
    		$this->supports              = array(
    			'shipping-zones',
    			'instance-settings',
    		);
    
    	    	$this->instance_form_fields = array(
            		'enabled' => array(
            			'title' 		=> __( 'Enable/Disable' ),
            			'type' 			=> 'checkbox',
            			'label' 		=> __( 'Enable this shipping method' ),
            			'default' 		=> 'yes',
            		),
            		'title' => array(
            			'title' 		=> __( 'Method Title' ),
            			'type' 			=> 'text',
            			'description' 	=> __( 'This controls the title which the user sees during checkout.' ),
            			'default'		=> __( 'Test Method' ),
            			'desc_tip'		=> true
            		)
    		);
    		$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' ) );
    	}
    
    	/**
    	 * calculate_shipping function.
    	 * @param array $package (default: array())
    	 */
    	public function calculate_shipping( $package = array() ) {
    		$this->add_rate( array(
    			'id'    => $this->id . $this->instance_id,
    			'label' => $this->title,
    			'cost'  => 100,
    		) );
    	}
    }
    
    		//class ends here
    		}
    	}
    
    	add_action( 'woocommerce_shipping_init', 'bike_shipping_method_init' );
    
    	function add_your_shipping_method( $methods ) {
    		$methods[] = 'WC_Shipping_Test_Method';
    		return $methods;
    	}
    
    	add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );
    }
    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Thread Starter fitbox

    (@fitmealat)

    solved.

    since my class was already initialized with the code above the end part needs to be changed to:

    add_filter( ‘woocommerce_shipping_methods’, ‘register_test_method’ );
    function register_test_method( $methods ) {
    $methods[ ‘test_method’ ] = new WC_Shipping_Bike();
    return $methods;
    }

    final working code as plugin

    <?php
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly
    }
    /*
    Plugin Name: eigenes hipping
    Plugin URI: fitmeal.at
    Description: asdfsfd
    Version: 1.0
    Author: Philipp Protschka
    Author URI: fitmeal.at
    License: GPL
    */
    /**
     * Check if WooCommerce is active
     */
     /**
     * Check if WooCommerce is active
     */
    
    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    
    	function bike_shipping_method_init() {
    		if ( ! class_exists( 'WC_Shipping_Bike' ) ) {
    		class WC_Shipping_Bike extends WC_Shipping_Method {
    
    	/**
    	 * Constructor. The instance ID is passed to this.
    	 */
    	public function __construct( $instance_id = 0 ) {
    		$this->id                    = 'test_method';
    		$this->instance_id 			     = absint( $instance_id );
    		$this->method_title          = __( 'Test Method' );
    		$this->method_description    = __( 'Some shipping method.' );
    		$this->supports              = array(
    			'shipping-zones',
    			'instance-settings',
    		);
    
    	    	$this->instance_form_fields = array(
            		'enabled' => array(
            			'title' 		=> __( 'Enable/Disable' ),
            			'type' 			=> 'checkbox',
            			'label' 		=> __( 'Enable this shipping method' ),
            			'default' 		=> 'yes',
            		),
            		'title' => array(
            			'title' 		=> __( 'Method Title' ),
            			'type' 			=> 'text',
            			'description' 	=> __( 'This controls the title which the user sees during checkout.' ),
            			'default'		=> __( 'Test Method' ),
            			'desc_tip'		=> true
            		)
    		);
    		$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' ) );
    	}
    
    	/**
    	 * calculate_shipping function.
    	 * @param array $package (default: array())
    	 */
    	public function calculate_shipping( $package = array() ) {
    		$this->add_rate( array(
    			'id'    => $this->id . $this->instance_id,
    			'label' => $this->title,
    			'cost'  => 100,
    		) );
    	}
    }
    
    		//class ends here
    		}
    	}
    
    	add_action( 'woocommerce_shipping_init', 'bike_shipping_method_init' );
    
    	add_filter( 'woocommerce_shipping_methods', 'register_test_method' );
    	function register_test_method( $methods ) {
    		$methods[ 'test_method' ] = new WC_Shipping_Bike();
    		return $methods;
    	}
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Dummy Shipping Method for WC 2.6 with ZONE Support?’ is closed to new replies.