BlockEditing
3 days ago
Convert
3 days ago
Duplicate
3 days ago
Management
3 days ago
Timestamps
3 days ago
Tracker
3 days ago
views
3 days ago
CommonMethodSettings.php
3 days ago
FreeShippingCalculator.php
3 days ago
FreeShippingThresholdRuleValidator.php
3 days ago
MethodDescription.php
3 days ago
MethodLogo.php
3 days ago
MethodLogoCheckoutBlocksAssets.php
3 days ago
MethodLogoSettingsField.php
3 days ago
MethodSettings.php
3 days ago
MethodTitle.php
3 days ago
RateCalculator.php
3 days ago
RateCalculatorFactory.php
3 days ago
SettingsDisplayPreparer.php
3 days ago
SettingsProcessor.php
3 days ago
SingleMethodSettings.php
3 days ago
RateCalculatorFactory.php
77 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class RateCalculatorFactory |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\ShippingMethod |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\TableRate\ShippingMethod; |
| 9 | |
| 10 | use WC_Cart; |
| 11 | use WC_Shipping_Method; |
| 12 | use WPDesk\FS\TableRate\Rule\Condition\ConditionsFactory; |
| 13 | use WPDesk\FS\TableRate\Rule\Cost\RuleAdditionalCostFactory; |
| 14 | use WPDesk\FS\TableRate\Rule\Cost\RuleCostFieldsFactory; |
| 15 | use WPDesk\FS\TableRate\Rule\ShippingContents\DestinationAddressFactory; |
| 16 | use WPDesk\FS\TableRate\Rule\ShippingContents\ShippingContentsImplementation; |
| 17 | use WPDesk\FS\TableRate\Rule\SpecialAction\SpecialActionFactory; |
| 18 | |
| 19 | /** |
| 20 | * Can create rates calculator. |
| 21 | */ |
| 22 | class RateCalculatorFactory { |
| 23 | /** |
| 24 | * @param WC_Shipping_Method $shipping_method . |
| 25 | * @param array $package . |
| 26 | * |
| 27 | * @return RateCalculator |
| 28 | */ |
| 29 | public static function create_for_shipping_method( WC_Shipping_Method $shipping_method, array $package ) { |
| 30 | $shop_currency = get_option( 'woocommerce_currency' ); |
| 31 | $cart_currency = get_woocommerce_currency(); |
| 32 | |
| 33 | $available_conditions = ( new ConditionsFactory() )->get_conditions(); |
| 34 | $cost_fields = ( new RuleCostFieldsFactory() )->get_fields(); |
| 35 | $available_additional_costs = ( new RuleAdditionalCostFactory() )->get_additional_costs(); |
| 36 | $available_special_actions = ( new SpecialActionFactory() )->get_special_actions(); |
| 37 | $cost_rounding_precision = wc_get_price_decimals(); |
| 38 | $cart = WC()->cart; |
| 39 | $prices_includes_tax = self::prices_include_tax( $cart ); |
| 40 | |
| 41 | $cart_contents = new ShippingContentsImplementation( |
| 42 | apply_filters( 'flexible-shipping/cart/cart-contents', $cart->get_cart_contents() ), |
| 43 | $prices_includes_tax, |
| 44 | $cost_rounding_precision, |
| 45 | DestinationAddressFactory::create_from_package_destination( $package['destination'] ), |
| 46 | $cart_currency |
| 47 | ); |
| 48 | |
| 49 | $free_shipping_calculator = new FreeShippingCalculator(); |
| 50 | |
| 51 | return new RateCalculator( |
| 52 | $shipping_method, |
| 53 | $shop_currency, |
| 54 | $cart_currency, |
| 55 | $available_conditions, |
| 56 | $cost_fields, |
| 57 | $available_additional_costs, |
| 58 | $available_special_actions, |
| 59 | $cost_rounding_precision, |
| 60 | $prices_includes_tax, |
| 61 | $cart, |
| 62 | $cart_contents, |
| 63 | $package, |
| 64 | $free_shipping_calculator |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param WC_Cart $cart . |
| 70 | * |
| 71 | * @return bool |
| 72 | */ |
| 73 | private static function prices_include_tax( WC_Cart $cart ) { |
| 74 | return (bool) apply_filters( 'flexible_shipping_prices_include_tax', $cart->display_prices_including_tax() ); |
| 75 | } |
| 76 | } |
| 77 |