116 lines
3.5 KiB
PHP
116 lines
3.5 KiB
PHP
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
if ( ! class_exists( 'WPDesk_WooCommerce_DPD' ) ) {
|
|
class WPDesk_WooCommerce_DPD {
|
|
|
|
static $instance = null;
|
|
|
|
private $plugin = null;
|
|
|
|
public static function get_instance( WPDesk_WooCommerce_DPD_Plugin $plugin ) {
|
|
if ( self::$instance == null ) {
|
|
self::$instance = new self( $plugin );
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public static function get_products() {
|
|
$ret = array(
|
|
'classic' => __( 'Classic', 'woocommerce-dpd' ),
|
|
);
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public static function get_package_types() {
|
|
return array(
|
|
'ENVELOPE' => __( 'Koperta', 'woocommerce-dpd' ),
|
|
'PACKAGE' => __( 'Paczka', 'woocommerce-dpd' ),
|
|
'PALLET' => __( 'Paleta', 'woocommerce-dpd' ),
|
|
);
|
|
}
|
|
|
|
public function __construct( WPDesk_WooCommerce_DPD_Plugin $plugin ) {
|
|
$this->plugin = $plugin;
|
|
$this->hooks();
|
|
}
|
|
|
|
public function hooks() {
|
|
add_filter( 'woocommerce_shipping_methods', array( $this, 'woocommerce_shipping_methods' ), 20, 1 );
|
|
|
|
add_action( 'woocommerce_order_status_changed', array( $this, 'woocommerce_order_status_changed' ), 10, 3 );
|
|
|
|
add_action( 'flexible_shipping_shipment_confirmed', array( $this, 'flexible_shipping_shipment_confirmed' ), 10, 2 );
|
|
|
|
add_action( 'flexible_shipping_add_shipping_options', array( $this, 'flexible_shipping_add_shipping_options' ) );
|
|
|
|
}
|
|
|
|
public function woocommerce_order_status_changed( $order_id, $old_status, $new_status ) {
|
|
$all_shipping_methods = WC()->shipping()->get_shipping_methods();
|
|
$dpd = $all_shipping_methods['dpd'];
|
|
$settings = $dpd->settings;
|
|
if ( isset( $settings['auto_create'] ) && $settings['auto_create'] == 'auto' ) {
|
|
if ( isset( $settings['order_status'] ) && 'wc-' . $new_status == $settings['order_status'] ) {
|
|
$order = wc_get_order( $order_id );
|
|
$shipments = fs_get_order_shipments( $order_id, 'dpd' );
|
|
foreach ( $shipments as $shipment ) {
|
|
try {
|
|
$shipment->api_create();
|
|
}
|
|
catch ( Exception $e ) {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function flexible_shipping_shipment_confirmed( WPDesk_Flexible_Shipping_Shipment $shipment ) {
|
|
if ( $shipment->get_meta( '_integration', '' ) != 'dpd' ) {
|
|
return;
|
|
}
|
|
$all_shipping_methods = WC()->shipping()->get_shipping_methods();
|
|
$shipping_method = $all_shipping_methods['dpd'];
|
|
if ( $shipping_method->get_option( 'complete_order', 'no' ) == 'yes' ) {
|
|
$order = $shipment->get_order();
|
|
$order->update_status( 'completed', __( 'Status zmieniony automatycznie po utworzeniu przesyłki - wtyczka DPD.', 'woocommerce-dpd' ) );
|
|
}
|
|
}
|
|
|
|
|
|
public function get_shipping_method() {
|
|
$shipping_methods = WC()->shipping()->shipping_methods;
|
|
if ( empty( $shipping_methods ) || !is_array( $shipping_methods ) || count( $shipping_methods ) == 0 ) {
|
|
$shipping_methods = WC()->shipping()->load_shipping_methods();
|
|
}
|
|
return $shipping_methods['dpd'];
|
|
}
|
|
|
|
public function get_label( $shipment_id ) {
|
|
|
|
$shipment = fs_get_shipment( $shipment_id );
|
|
|
|
return $shipment->get_label();
|
|
}
|
|
|
|
public function get_api() {
|
|
$shipping_method = $this->get_shipping_method();
|
|
return $shipping_method->get_api();
|
|
}
|
|
|
|
|
|
public function woocommerce_shipping_methods( $methods ) {
|
|
include_once( 'class-shipping-method.php' );
|
|
$methods['dpd'] = 'WPDesk_WooCommerce_DPD_Shipping_Method';
|
|
return $methods;
|
|
}
|
|
|
|
public function flexible_shipping_add_shipping_options( $options ) {
|
|
$options['dpd'] = 'DPD';
|
|
return $options;
|
|
}
|
|
|
|
}
|
|
}
|