Weight.php
170 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Weight |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\Rule\Condition |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\TableRate\Rule\Condition; |
| 9 | |
| 10 | use FSVendor\WPDesk\Forms\Field; |
| 11 | use Psr\Log\LoggerInterface; |
| 12 | use WPDesk\FS\TableRate\Rule\ShippingContents\ShippingContents; |
| 13 | |
| 14 | /** |
| 15 | * Weight condition. |
| 16 | */ |
| 17 | if ( defined( 'FLEXIBLE_SHIPPING_PSR_NOT_PREFIXED' ) && FLEXIBLE_SHIPPING_PSR_NOT_PREFIXED ) { |
| 18 | class Weight extends AbstractCondition { |
| 19 | |
| 20 | const MIN = 'min'; |
| 21 | const MAX = 'max'; |
| 22 | |
| 23 | const CONDITION_ID = 'weight'; |
| 24 | |
| 25 | /** |
| 26 | * Weight constructor. |
| 27 | * |
| 28 | * @param int $priority . |
| 29 | */ |
| 30 | public function __construct( $priority = 10 ) { |
| 31 | $this->condition_id = self::CONDITION_ID; |
| 32 | $this->name = __( 'Weight', 'flexible-shipping' ); |
| 33 | $this->description = __( 'Shipping cost based on the weight of the cart or package', 'flexible-shipping' ); |
| 34 | $this->group = __( 'Product', 'flexible-shipping' ); |
| 35 | $this->priority = $priority; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param array $condition_settings . |
| 40 | * @param ShippingContents $contents . |
| 41 | * @param LoggerInterface $logger . |
| 42 | * |
| 43 | * @return bool |
| 44 | */ |
| 45 | public function is_condition_matched( array $condition_settings, ShippingContents $contents, LoggerInterface $logger ) { |
| 46 | $min = (float) ( isset( $condition_settings[ self::MIN ] ) && 0 !== strlen( $condition_settings[ self::MIN ] ) ? $condition_settings[ self::MIN ] : 0 ); |
| 47 | $max = (float) ( isset( $condition_settings[ self::MAX ] ) && 0 !== strlen( $condition_settings[ self::MAX ] ) ? $condition_settings[ self::MAX ] : INF ); |
| 48 | |
| 49 | $contents_weight = |
| 50 | /** |
| 51 | * Can modify contents weight passed to Weight condition. |
| 52 | * |
| 53 | * @param float $contents_weight Contents weight. |
| 54 | * |
| 55 | * @since 4.1.1 |
| 56 | */ |
| 57 | apply_filters( 'flexible-shipping/condition/contents_weight', $contents->get_contents_weight() ); |
| 58 | |
| 59 | $contents_weight = (float) $contents_weight; |
| 60 | |
| 61 | $condition_matched = $contents_weight >= $min && $contents_weight <= $max; |
| 62 | |
| 63 | $logger->debug( $this->format_for_log( $condition_settings, $condition_matched, $contents_weight ) ); |
| 64 | |
| 65 | return $condition_matched; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return Field[] |
| 70 | */ |
| 71 | public function get_fields() { |
| 72 | return [ |
| 73 | ( new Field\InputNumberField() ) |
| 74 | ->set_name( self::MIN ) |
| 75 | ->add_class( 'wc_input_decimal' ) |
| 76 | ->add_class( 'hs-beacon-search' ) |
| 77 | ->add_class( 'parameter_min' ) |
| 78 | ->add_data( 'beacon_search', __( 'weight is from', 'flexible-shipping' ) ) |
| 79 | ->set_placeholder( __( 'is from', 'flexible-shipping' ) ) |
| 80 | ->set_label( __( 'is from', 'flexible-shipping' ) ), |
| 81 | ( new Field\InputNumberField() ) |
| 82 | ->set_name( self::MAX ) |
| 83 | ->add_class( 'wc_input_decimal' ) |
| 84 | ->add_class( 'hs-beacon-search' ) |
| 85 | ->add_class( 'parameter_max' ) |
| 86 | ->add_data( 'beacon_search', __( 'weight to', 'flexible-shipping' ) ) |
| 87 | ->set_placeholder( __( 'to', 'flexible-shipping' ) ) |
| 88 | ->set_label( __( 'to', 'flexible-shipping' ) ) |
| 89 | ->add_data( 'suffix', get_option( 'woocommerce_weight_unit' ) ), |
| 90 | ]; |
| 91 | } |
| 92 | } |
| 93 | } else { |
| 94 | class Weight extends AbstractCondition { |
| 95 | |
| 96 | const MIN = 'min'; |
| 97 | const MAX = 'max'; |
| 98 | |
| 99 | const CONDITION_ID = 'weight'; |
| 100 | |
| 101 | /** |
| 102 | * Weight constructor. |
| 103 | * |
| 104 | * @param int $priority . |
| 105 | */ |
| 106 | public function __construct( $priority = 10 ) { |
| 107 | $this->condition_id = self::CONDITION_ID; |
| 108 | $this->name = __( 'Weight', 'flexible-shipping' ); |
| 109 | $this->description = __( 'Shipping cost based on the weight of the cart or package', 'flexible-shipping' ); |
| 110 | $this->group = __( 'Product', 'flexible-shipping' ); |
| 111 | $this->priority = $priority; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param array $condition_settings . |
| 116 | * @param ShippingContents $contents . |
| 117 | * @param LoggerInterface $logger . |
| 118 | * |
| 119 | * @return bool |
| 120 | */ |
| 121 | public function is_condition_matched( array $condition_settings, ShippingContents $contents, $logger ) { |
| 122 | $min = (float) ( isset( $condition_settings[ self::MIN ] ) && 0 !== strlen( $condition_settings[ self::MIN ] ) ? $condition_settings[ self::MIN ] : 0 ); |
| 123 | $max = (float) ( isset( $condition_settings[ self::MAX ] ) && 0 !== strlen( $condition_settings[ self::MAX ] ) ? $condition_settings[ self::MAX ] : INF ); |
| 124 | |
| 125 | $contents_weight = |
| 126 | /** |
| 127 | * Can modify contents weight passed to Weight condition. |
| 128 | * |
| 129 | * @param float $contents_weight Contents weight. |
| 130 | * |
| 131 | * @since 4.1.1 |
| 132 | */ |
| 133 | apply_filters( 'flexible-shipping/condition/contents_weight', $contents->get_contents_weight() ); |
| 134 | |
| 135 | $contents_weight = (float) $contents_weight; |
| 136 | |
| 137 | $condition_matched = $contents_weight >= $min && $contents_weight <= $max; |
| 138 | |
| 139 | $logger->debug( $this->format_for_log( $condition_settings, $condition_matched, $contents_weight ) ); |
| 140 | |
| 141 | return $condition_matched; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @return Field[] |
| 146 | */ |
| 147 | public function get_fields() { |
| 148 | return [ |
| 149 | ( new Field\InputNumberField() ) |
| 150 | ->set_name( self::MIN ) |
| 151 | ->add_class( 'wc_input_decimal' ) |
| 152 | ->add_class( 'hs-beacon-search' ) |
| 153 | ->add_class( 'parameter_min' ) |
| 154 | ->add_data( 'beacon_search', __( 'weight is from', 'flexible-shipping' ) ) |
| 155 | ->set_placeholder( __( 'is from', 'flexible-shipping' ) ) |
| 156 | ->set_label( __( 'is from', 'flexible-shipping' ) ), |
| 157 | ( new Field\InputNumberField() ) |
| 158 | ->set_name( self::MAX ) |
| 159 | ->add_class( 'wc_input_decimal' ) |
| 160 | ->add_class( 'hs-beacon-search' ) |
| 161 | ->add_class( 'parameter_max' ) |
| 162 | ->add_data( 'beacon_search', __( 'weight to', 'flexible-shipping' ) ) |
| 163 | ->set_placeholder( __( 'to', 'flexible-shipping' ) ) |
| 164 | ->set_label( __( 'to', 'flexible-shipping' ) ) |
| 165 | ->add_data( 'suffix', get_option( 'woocommerce_weight_unit' ) ), |
| 166 | ]; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 |