-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwoo-central-do-frete.php
155 lines (134 loc) · 3.59 KB
/
woo-central-do-frete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Plugin Name: WooCommerce Central do Frete
* Plugin URI: https://github.com/buzzmage/woo-central-do-frete
* Description: Módulo de cotações de frete da Central do Frete para WooCommerce
* Author: Buzz e-Commerce
* Author URI: http://www.sitedabuzz.com.br/
* Version: 1.0.1
* License: GPLv2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WooCommerce_CentralDoFrete_Main' ) ) :
/**
* Central do Frete main class.
*/
class WooCommerce_CentralDoFrete_Main {
/**
* Plugin version.
* @var string
*/
const VERSION = '1.0.1';
/**
* Instance of this class.
* @var object
*/
protected static $instance = null;
/**
* Initialize the plugin
*/
private function __construct() {
$this->initialize();
}
/**
* Verify if all plugin dependencies are available
* @return bool
*/
function verifyRequirements() {
if ( ! class_exists( 'WC_Integration' ) ) {
add_action( 'admin_notices', array( $this, 'missingWooCommerceNotice' ) );
return false;
}
return true;
}
/**
* Show message when WooCommerce aren't installed
*/
function missingWooCommerceNotice() {
$class = 'notice notice-warning';
$message = __( 'Sorry. The Central do Frete plugin depends on WooCommerce. Please install WooCommerce.' );
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
}
/**
* Initialize plugin
*/
public function initialize() {
if ( $this->verifyRequirements() ) {
$this->loadDependencies();
$this->addActions();
}
}
/**
* Add this plugin to shipping methods - addCentralDoFreteMethod
* Add product custom attributes - addCustomShippingOptionToProductForm
* Add listener when save product with custom attributes - saveCustomField
*/
public function addActions() {
add_filter( 'woocommerce_shipping_methods', array( $this, 'addCentralDoFreteMethod' ) );
add_action( 'woocommerce_product_options_shipping', array(
'WooCommerce_CentralDoFrete_Method',
'addCustomShippingOptionToProductForm'
) );
add_action( 'woocommerce_process_product_meta', array(
'WooCommerce_CentralDoFrete_Method',
'saveCustomField'
) );
}
/**
* Load plugins classes
*/
public function loadDependencies() {
include( self::getPluginPath() . 'classes/WooCentralDoFrete.php' );
include( self::getPluginPath() . 'classes/Helper.php' );
}
/**
*
* Return an instance of this class.
* @return object A single instance of this class.
*/
public static function getInstance() {
// If the single instance hasn't been set, set it now.
if ( null === self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Get plugin path.
* @return string
*/
public static function getPluginPath() {
return plugin_dir_path( __FILE__ );
}
/**
* Add the Central do Frete to shipping methods.
*
* @param array $methods
*
* @return array
*/
function addCentralDoFreteMethod( $methods ) {
$methods['centraldofrete'] = 'WooCommerce_CentralDoFrete_Method';
return $methods;
}
/**
* Output a message or error
*
* @param string $message
* @param string $type
*/
public function debug( $message, $type = 'notice' ) {
if ( $this->debug && ! is_admin() ) {
if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '>=' ) ) {
wc_add_notice( $message, $type );
} else {
global $woocommerce;
$woocommerce->add_message( $message );
}
}
}
}
add_action( 'plugins_loaded', array( 'WooCommerce_CentralDoFrete_Main', 'getInstance' ) );
endif;