Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 136 additions & 4 deletions ceske-sluzby.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ function ceske_sluzby_kontrola_aktivniho_pluginu() {
add_action( 'woocommerce_shipping_init', 'ceske_sluzby_doprava_ulozenka_init' );
add_filter( 'woocommerce_shipping_methods', 'ceske_sluzby_doprava_ulozenka' );

add_action( 'woocommerce_shipping_init', 'ceske_sluzby_doprava_zasilkovna_init' );
add_filter( 'woocommerce_shipping_methods', 'ceske_sluzby_doprava_zasilkovna' );

add_action( 'woocommerce_shipping_init', 'ceske_sluzby_doprava_dpd_parcelshop_init' );
add_filter( 'woocommerce_shipping_methods', 'ceske_sluzby_doprava_dpd_parcelshop' );

Expand All @@ -206,6 +209,13 @@ function ceske_sluzby_kontrola_aktivniho_pluginu() {
add_action( 'woocommerce_email_after_order_table', 'ceske_sluzby_ulozenka_pobocka_email' );
add_action( 'woocommerce_order_details_after_order_table', 'ceske_sluzby_ulozenka_pobocka_email' );

add_action( 'woocommerce_review_order_after_shipping', 'ceske_sluzby_zasilkovna_zobrazit_pobocky' );
add_action( 'woocommerce_add_shipping_order_item', 'ceske_sluzby_zasilkovna_ulozeni_pobocky', 10, 2 );
add_action( 'woocommerce_checkout_process', 'ceske_sluzby_zasilkovna_overit_pobocku' );
add_action( 'woocommerce_admin_order_data_after_billing_address', 'ceske_sluzby_zasilkovna_objednavka_zobrazit_pobocku' );
add_action( 'woocommerce_email_after_order_table', 'ceske_sluzby_zasilkovna_pobocka_email' );
add_action( 'woocommerce_order_details_after_order_table', 'ceske_sluzby_zasilkovna_pobocka_email' );

add_action( 'woocommerce_review_order_after_shipping', 'ceske_sluzby_dpd_parcelshop_zobrazit_pobocky' );
add_action( 'woocommerce_add_shipping_order_item', 'ceske_sluzby_dpd_parcelshop_ulozeni_pobocky', 10, 2 );
add_action( 'woocommerce_checkout_process', 'ceske_sluzby_dpd_parcelshop_overit_pobocku' );
Expand All @@ -214,6 +224,7 @@ function ceske_sluzby_kontrola_aktivniho_pluginu() {
add_action( 'woocommerce_order_details_after_order_table', 'ceske_sluzby_dpd_parcelshop_pobocka_email' );

add_filter( 'woocommerce_pay4pay_cod_amount', 'ceske_sluzby_ulozenka_dobirka_pay4pay' );
add_filter( 'woocommerce_pay4pay_cod_amount', 'ceske_sluzby_zasilkovna_dobirka_pay4pay' );
add_filter( 'woocommerce_pay4pay_cod_amount', 'ceske_sluzby_dpd_parcelshop_dobirka_pay4pay' );

$aktivace_recenzi = get_option( 'wc_ceske_sluzby_heureka_recenze_obchodu-aktivace' );
Expand Down Expand Up @@ -264,6 +275,7 @@ function ceske_sluzby_kontrola_aktivniho_pluginu() {
function ceske_sluzby_doprava_ulozenka_init() {
if ( ! class_exists( 'WC_Shipping_Ceske_Sluzby_Ulozenka' ) ) {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-ceske-sluzby-ulozenka.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-ceske-sluzby-ulozenka-json-loader.php';
}
}

Expand All @@ -285,7 +297,7 @@ function ceske_sluzby_ulozenka_zobrazit_pobocky() {

if ( $settings['enabled'] == "yes" && ! empty ( $settings['ulozenka_id-obchodu'] ) ) {

$pobocky = new Ceske_Sluzby_Json_Loader();
$pobocky = new Ceske_Sluzby_Ulozenka_Json_Loader();
// http://docs.ulozenkav3.apiary.io/#pepravnsluby

$zeme = WC()->customer->get_shipping_country();
Expand All @@ -297,7 +309,7 @@ function ceske_sluzby_ulozenka_zobrazit_pobocky() {

<tr class="ulozenka">
<td>
<img src="https://www.ulozenka.cz/logo/ulozenka.png" width="140" border="0">
<img src="<?php echo $settings['ulozenka_logo']; ?>" width="140" border="0">
</td>
<td>
<font size="2">Uloženka - výběr pobočky:</font><br>
Expand Down Expand Up @@ -379,10 +391,130 @@ function ceske_sluzby_ulozenka_pobocka_email( $order ) {
}
}

function ceske_sluzby_doprava_zasilkovna_init() {
if ( ! class_exists( 'WC_Shipping_Ceske_Sluzby_Zasilkovna' ) ) {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-ceske-sluzby-zasilkovna.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-ceske-sluzby-zasilkovna-json-loader.php';
}
}

function ceske_sluzby_doprava_zasilkovna( $methods ) {
$methods[] = 'WC_Shipping_Ceske_Sluzby_Zasilkovna';
return $methods;
}

function ceske_sluzby_zasilkovna_zobrazit_pobocky() {
if ( is_ajax() ) {
// Do budoucna možná použít spíše woocommerce_checkout_update_order_review
parse_str( $_POST['post_data'] );
$available_shipping = WC()->shipping->load_shipping_methods();
$chosen_shipping_method = WC()->session->get( 'chosen_shipping_methods' );
$settings = array();

if ( $chosen_shipping_method[0] == "ceske_sluzby_zasilkovna" ) {
$settings = $available_shipping[ $chosen_shipping_method[0] ]->settings;

if ( $settings['enabled'] == "yes" && ! empty ( $settings['zasilkovna_api-klic'] ) ) {

$pobocky = new Ceske_Sluzby_Zasilkovna_Json_Loader();

$zeme = WC()->customer->get_shipping_country();
if ( $zeme == "CZ" ) { $zeme_code = "cz"; }
if ( $zeme == "SK" ) { $zeme_code = "sk"; }

$parametry = array();
?>

<tr class="zasilkovna">
<td>
<img src="<?php echo $settings['zasilkovna_logo']; ?>" width="160" border="0">
</td>
<td>
<font size="2">Zásilkovna - výběr pobočky:</font><br>
<div id="zasilkovna-branch-select-options">
<select name="zasilkovna_branches">
<option>Vyberte pobočku</option>

<?php
foreach ( $pobocky->load( $parametry )->data as $pobocka_id => $pobocka ) {
if ( ! empty ( $zasilkovna_branches ) && $zasilkovna_branches == $pobocka_id ) {
$selected = ' selected="selected"';
} else {
$selected = "";
}
if ( $pobocka->country == $zeme_code ) {
echo '<option value="' . $pobocka_id . '"' . $selected . '>' . $pobocka->name . '</option>';
}
} ?>

</div>
</td>
</tr>

<?php }
}
}
}

function ceske_sluzby_zasilkovna_ulozeni_pobocky( $order_id, $item_id ) {
if ( isset( $_POST["zasilkovna_branches"] ) ) {
if ( $_POST["zasilkovna_branches"] && $_POST["shipping_method"][0] == "ceske_sluzby_zasilkovna" ) {
wc_add_order_item_meta( $item_id, 'ceske_sluzby_zasilkovna_pobocka_nazev', esc_attr( $_POST['zasilkovna_branches'] ), true );
}
}
}

function ceske_sluzby_zasilkovna_overit_pobocku() {
if ( isset( $_POST["zasilkovna_branches"] ) ) {
if ( $_POST["zasilkovna_branches"] == "Vyberte pobočku" && $_POST["shipping_method"][0] == "ceske_sluzby_zasilkovna" ) {
wc_add_notice( 'Pokud chcete doručit zboží prostřednictvím Zásilkovny, zvolte prosím pobočku.', 'error' );
}
}
}

function ceske_sluzby_zasilkovna_objednavka_zobrazit_pobocku( $order ) {
if ( $order->has_shipping_method( 'ceske_sluzby_zasilkovna' ) ) {
foreach ( $order->get_shipping_methods() as $shipping_item_id => $shipping_item ) {
echo "<p><strong>Zásilkovna:</strong> " . $order->get_item_meta( $shipping_item_id, 'ceske_sluzby_zasilkovna_pobocka_nazev', true ) . "</p>";
}
}
}

function ceske_sluzby_zasilkovna_dobirka_pay4pay( $amount ) {
$available_shipping = WC()->shipping->load_shipping_methods();
$chosen_shipping_method = WC()->session->get( 'chosen_shipping_methods' );
if ( $chosen_shipping_method[0] == "ceske_sluzby_zasilkovna" ) {
$settings = $available_shipping[ $chosen_shipping_method[0] ]->settings;
$zeme = WC()->customer->get_shipping_country();
if ( $zeme == "CZ" ) {
if ( ! empty( $settings['zasilkovna_dobirka'] ) ) {
$amount = $settings['zasilkovna_dobirka'];
}
}
if ( $zeme == "SK" ) {
if ( ! empty( $settings['zasilkovna_dobirka-slovensko'] ) ) {
$amount = $settings['zasilkovna_dobirka-slovensko'];
}
}
if ( class_exists( 'WOOCS' ) ) {
$amount = apply_filters( 'woocs_exchange_value', $amount );
}
}
return $amount;
}

function ceske_sluzby_zasilkovna_pobocka_email( $order ) {
if ( $order->has_shipping_method( 'ceske_sluzby_zasilkovna' ) ) {
foreach ( $order->get_shipping_methods() as $shipping_item_id => $shipping_item ) {
echo "<p><strong>Zásilkovna:</strong> " . $order->get_item_meta( $shipping_item_id, 'ceske_sluzby_zasilkovna_pobocka_nazev', true ) . "</p>";
}
}
}

function ceske_sluzby_doprava_dpd_parcelshop_init() {
if ( ! class_exists( 'WC_Shipping_Ceske_Sluzby_DPD_ParcelShop' ) ) {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-ceske-sluzby-dpd-parcelshop.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-ceske-sluzby-json-loader.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-ceske-sluzby-ulozenka-json-loader.php';
}
}

Expand All @@ -403,7 +535,7 @@ function ceske_sluzby_dpd_parcelshop_zobrazit_pobocky() {

if ( $settings['enabled'] == "yes" ) {

$pobocky = new Ceske_Sluzby_Json_Loader();
$pobocky = new Ceske_Sluzby_Ulozenka_Json_Loader();

$zeme = WC()->customer->get_shipping_country();
if ( $zeme == "CZ" ) { $zeme_code = "CZE"; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?php
// http://pressing-matters.io/building-an-object-oriented-wordpress-plugin-xkcd-shortcode-part-5/
class Ceske_Sluzby_Json_Loader {
class Ceske_Sluzby_Ulozenka_Json_Loader {
function load( $params = null ) {
$url = $this->build( $params );
$result = $this->fetch( $url );
$body = $this->verify( $result );
return $this->parse( $body );
$json = get_transient( 'ceske_sluzby_ulozenka_pobocky' );
if (!$json) {
$url = $this->build( $params );
$result = $this->fetch( $url );
$body = $this->verify( $result );
$json = $this->parse( $body );
set_transient( 'ceske_sluzby_ulozenka_pobocky', $json, 24 * 60 * 60);
}
return $json;
}

function fetch( $url ) {
Expand Down
7 changes: 7 additions & 0 deletions includes/class-ceske-sluzby-ulozenka.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public function init_form_fields() {
'default' => '',
'css' => 'width: 100px;'
),
'ulozenka_logo' => array(
'title' => 'Logo',
'type' => 'text',
'description' => 'Zadejte URL na logo Uloženky',
'default' => 'https://www.ulozenka.cz/logo/ulozenka.png',
'css' => 'width: 500px;'
),
'ulozenka_zakladni-cena' => array(
'title' => 'Základní cena',
'type' => 'price',
Expand Down
59 changes: 59 additions & 0 deletions includes/class-ceske-sluzby-zasilkovna-json-loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
// http://pressing-matters.io/building-an-object-oriented-wordpress-plugin-xkcd-shortcode-part-5/
class Ceske_Sluzby_Zasilkovna_Json_Loader {
function load( $params = null ) {
$json = get_transient( 'ceske_sluzby_zasilkovna_pobocky' );
if ( !$json ) {
$url = $this->build( $params );
$result = $this->fetch( $url );
$body = $this->verify( $result );
$json = $this->parse( $body );
set_transient( 'ceske_sluzby_zasilkovna_pobocky', $json, 24 * 60 * 60);
}
return $json;
}

function fetch( $url ) {
return wp_remote_get( $url );
}

function verify( $result ) {
if ( is_wp_error( $result ) ) {
throw new Exception( 'Ceske_Sluzby_Json_Loader Failed: Nepodařilo se získat obsah z URL adresy.' );
}

$code = $result['response']['code'];
if ( $code != 200 ) {
throw new Exception( 'Ceske_Sluzby_Json_Loader Failed: Neplatná HTTP reakce ze serveru - ' . $code );
}

$body = wp_remote_retrieve_body( $result );
if ( $body == '' ) {
throw new Exception( 'Ceske_Sluzby_Json_Loader Failed: Obsah Json je prázdný.' );
}

return $body;
}

function parse( $body ) {
$json = json_decode( $body );
if ( json_last_error() !== JSON_ERROR_NONE ) {
throw new Exception( 'Ceske_Sluzby_Json_Loader Failed: Neplatný Json obsah získaný ze serveru - ' . json_last_error() );
}
return $json;
}

function build( $params ) {
$base_url = 'https://www.zasilkovna.cz/api/v3/';
$available_shipping = WC()->shipping->load_shipping_methods();
$settings = $available_shipping[ "ceske_sluzby_zasilkovna" ]->settings;

if ( ! empty( $settings['zasilkovna_api-klic'] ) ) {
$base_url .= $settings['zasilkovna_api-klic'];
$base_url .= '/branch.json';
return $base_url;
} else {
return null;
}
}
}
Loading