Skip to content

Commit

Permalink
Merge pull request #50 from luizbills/1.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
luizbills authored Mar 8, 2023
2 parents 40d7206 + ea93578 commit 7da3dbf
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 24 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file.

## 1.9.0 - 2023-03-08

[Source code changes](https://github.com/luizbills/shipping-simulator-for-woocommerce/compare/1.8.0...1.9.0)

- Tweak: Change input mask for Brazilian postcodes from "99 999-999" to "99999-999" (removed a whitespace).
- Tweak: Improve the shipping calculation for better compatibility with other plugins.

## 1.8.0 - 2023-03-02

[Source code changes](https://github.com/luizbills/shipping-simulator-for-woocommerce/compare/1.7.0...1.8.0)
Expand Down
2 changes: 1 addition & 1 deletion classes/Integration/Brazil.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function update_package ( $package, $posted ) {
}

public function form_input_mask ( $mask ) {
return 'XX XXX-XXX';
return 'XXXXX-XXX';
}

public function add_cep_finder_link () {
Expand Down
59 changes: 46 additions & 13 deletions classes/Shipping_Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
final class Shipping_Package {
public $ready = false;

protected $contents;
protected $destination;
protected $package = null;
protected $destination = null;
protected $contents = [];
protected $original_cart_info = [];

public function __construct () {
$this->contents = [];
Expand Down Expand Up @@ -99,29 +101,39 @@ public function get_package () {
'has_variations' => $has_variations
]
);

$package['DOING_SHIPPING_SIMULATION'] = true;

return $package;
}

public function calculate_shipping () {
$wc_shipping = \WC_Shipping::instance();
$wc_shipping = WC()->shipping();

// save the current WC_Shipping->packages
$original_packages = $wc_shipping->packages;
// backup the current WC_Shipping::packages
$original_shipping_packages = $wc_shipping->packages;

// calculate
$package = $this->get_package();
$this->package = $this->get_package();
h::logger()->info( 'Calculating shipping rates for ...' );
$this->log_package( $package );
$result = $wc_shipping->calculate_shipping( [ $package ] );
$this->log_package( $this->package );

// sync the cart contents and totals
$this->modify_cart_info();

// calculate
$result = $wc_shipping->calculate_shipping( [ $this->package ] );

// restore the cart contents and totals
$this->restore_cart_info();

// restore the WC_Shipping->packages
$wc_shipping->packages = $original_packages;
// restore the WC_Shipping::packages
$wc_shipping->packages = $original_shipping_packages;

// get the results
$rates = h::get( $result[0]['rates'], [] );
h::logger()->info( 'Result: ' . wp_json_encode( array_keys( $rates ) ) );

// sort results in ASC order
if ( count( $rates ) > 1 ) {
uasort( $rates, function ( $a, $b ) {
return $a->get_cost() <=> $b->get_cost();
Expand All @@ -131,17 +143,38 @@ public function calculate_shipping () {
$rates = apply_filters(
'wc_shipping_simulator_package_rates',
$rates,
$package
$this->package
);

h::throw_if(
$package['has_variations'] && 0 === count( $rates ),
$this->package['has_variations'] && 0 === count( $rates ),
esc_attr__( 'Please select some product options before adding this product to your cart.', 'wc-shipping-simulator' )
);

return $rates;
}

public function modify_cart_info () {
$cart = WC()->cart;
$this->original_cart_info = [
'cart_contents' => $cart->get_cart_contents(),
'cart_contents_total' => $cart->get_cart_contents_total(),
'subtotal' => $cart->get_subtotal(),
];
$cart->set_cart_contents( $this->package['contents' ]);
$cart->set_cart_contents_total( $this->package['contents_cost'] );
$cart->set_subtotal( $this->package['contents_cost'] );
}

public function restore_cart_info () {
if ( 0 === count( $this->original_cart_info ) ) return;
$cart = WC()->cart;
foreach ( $this->original_cart_info as $prop => $value ) {
$cart->{'set_' . $prop}( $value );
unset( $this->original_cart_info[ $prop ] );
}
}

protected function log_package ( $package ) {
if ( Settings::debug_enabled() ) {
$i = 0;
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
},
"require-dev": {
"szepeviktor/phpstan-wordpress": "^1.1",
"php-stubs/woocommerce-stubs": "^7.4"
"php-stubs/woocommerce-stubs": "^7.4",
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion main.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Shipping Simulator for WooCommerce
Plugin URI: https://github.com/luizbills/shipping-simulator-for-woocommerce
Description: Allows your customers to calculate the shipping rates on the product page
Version: 1.8.0
Version: 1.9.0
Requires at least: 4.9
Requires PHP: 7.4
Author: Luiz Bills
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Contributors: luizbills
Donate link: https://luizpb.com/donate/
Tags: woocommerce, shipping simulator, simulador de frete, brazil, brasil, calculadora de frete, shipping calculator, product
Stable tag: 1.8.0
Stable tag: 1.9.0
Requires at least: 4.9
Requires PHP: 7.4
Tested up to: 6.1
Expand Down

0 comments on commit 7da3dbf

Please sign in to comment.