Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Your Shipping plugin
  4. Plugin URI: https://woocommerce.com/
  5. Description: Your shipping method plugin
  6. Version: 1.0.0
  7. Author: WooThemes
  8. Author URI: https://woocommerce.com/
  9. */
  10. /**
  11. * Check if WooCommerce is active
  12. */
  13.  
  14. if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
  15. function your_shipping_method_init() {
  16. if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
  17. class WC_Your_Shipping_Method extends WC_Shipping_Method {
  18. /**
  19. * Constructor for your shipping class
  20. *
  21. * @access public
  22. * @return void
  23. */
  24. public function __construct() {
  25. $this->id = 'your_shipping_method'; // Id for your shipping method. Should be uunique.
  26. $this->method_title = __( 'Your Shipping Method' ); // Title shown in admin
  27. $this->method_description = __( 'Description of your shipping method' ); // Description shown in admin
  28. $this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
  29. $this->title = "My Shipping Method"; // This can be added as an setting but for this example its forced.
  30. $this->init();
  31. }
  32. /**
  33. * Init your settings
  34. *
  35. * @access public
  36. * @return void
  37. */
  38. function init() {
  39. // Load the settings API
  40. $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
  41. $this->init_settings(); // This is part of the settings API. Loads settings you previously init.
  42. // Save settings in admin if you have any defined
  43. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
  44. }
  45. /**
  46. * calculate_shipping function.
  47. *
  48. * @access public
  49. * @param mixed $package
  50. * @return void
  51. */
  52. public function calculate_shipping( $package ) {
  53. $rate = array(
  54. 'id' => $this->id,
  55. 'label' => $this->title,
  56. 'cost' => '10.99',
  57. 'calc_tax' => 'per_item'
  58. );
  59. // Register the rate
  60. $this->add_rate( $rate );
  61. }
  62. }
  63. }
  64. }
  65. add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' );
  66. function add_your_shipping_method( $methods ) {
  67. $methods['your_shipping_method'] = 'WC_Your_Shipping_Method';
  68. return $methods;
  69. }
  70. add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement