flexible-shipping
/
src
/
WPDesk
/
FS
/
TableRate
/
Rule
/
Cost
/
RuleAdditionalCostFieldsFactory.php
RuleAdditionalCostFactory.php
1 week ago
RuleAdditionalCostFieldsFactory.php
1 week ago
RuleCostFieldsFactory.php
1 week ago
RuleAdditionalCostFieldsFactory.php
107 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class RuleCostFactory |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\Rule\Cost |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\TableRate\Rule\Cost; |
| 9 | |
| 10 | |
| 11 | |
| 12 | use FSVendor\WPDesk\Forms\Field; |
| 13 | use FSVendor\WPDesk\Forms\FieldProvider; |
| 14 | use FSVendor\WPDesk\Forms\Renderer\JsonNormalizedRenderer; |
| 15 | |
| 16 | /** |
| 17 | * Can create additional costs fields. |
| 18 | */ |
| 19 | class RuleAdditionalCostFieldsFactory implements FieldProvider { |
| 20 | |
| 21 | const ADDITIONAL_COST = 'additional_cost'; |
| 22 | const PER_VALUE = 'per_value'; |
| 23 | const BASED_ON = 'based_on'; |
| 24 | |
| 25 | /** |
| 26 | * @var AdditionalCost[] |
| 27 | */ |
| 28 | private $available_additional_costs; |
| 29 | |
| 30 | /** |
| 31 | * RuleAdditionalCostFieldsFactory constructor. |
| 32 | * |
| 33 | * @param AdditionalCost[] $available_additional_costs . |
| 34 | */ |
| 35 | public function __construct( array $available_additional_costs ) { |
| 36 | $this->available_additional_costs = $available_additional_costs; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * @return Field[] |
| 42 | */ |
| 43 | public function get_fields() { |
| 44 | return apply_filters( 'flexible_shipping_rule_additional_cost_fields', $this->get_built_in_rule_additional_cost_fields() ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * . |
| 49 | * |
| 50 | * @return array |
| 51 | */ |
| 52 | public function get_normalized_cost_fields() { |
| 53 | $normalized_cost_fields = array(); |
| 54 | $renderer = new JsonNormalizedRenderer(); |
| 55 | |
| 56 | return $renderer->render_fields( $this, array() ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @return Field[] |
| 61 | */ |
| 62 | public function get_built_in_rule_additional_cost_fields() { |
| 63 | return array( |
| 64 | ( new Field\InputNumberField() ) |
| 65 | ->set_name( self::ADDITIONAL_COST ) |
| 66 | ->add_class( 'wc_input_decimal' ) |
| 67 | ->add_class( 'hs-beacon-search' ) |
| 68 | ->add_class( 'additional-cost-cost' ) |
| 69 | ->add_data( 'beacon_search', __( 'additional cost', 'flexible-shipping' ) ) |
| 70 | ->set_placeholder( __( 'additional cost', 'flexible-shipping' ) ) |
| 71 | ->set_label( __( 'additional cost is', 'flexible-shipping' ) ) |
| 72 | ->add_data( 'suffix', get_woocommerce_currency_symbol() ), |
| 73 | ( new Field\InputNumberField() ) |
| 74 | ->set_name( self::PER_VALUE ) |
| 75 | ->add_class( 'wc_input_decimal' ) |
| 76 | ->add_class( 'hs-beacon-search' ) |
| 77 | ->add_class( 'additional-cost-per' ) |
| 78 | ->add_data( 'beacon_search', __( 'additional cost per', 'flexible-shipping' ) ) |
| 79 | ->set_placeholder( __( 'per', 'flexible-shipping' ) ) |
| 80 | ->set_label( __( 'per', 'flexible-shipping' ) ), |
| 81 | ( new Field\SelectField() ) |
| 82 | ->set_name( self::BASED_ON ) |
| 83 | ->set_options( $this->get_based_on_options() ) |
| 84 | ->add_class( 'wc_input_decimal' ) |
| 85 | ->add_class( 'hs-beacon-search' ) |
| 86 | ->add_class( 'additional-cost-based-on' ) |
| 87 | ->add_data( 'beacon_search', __( 'additional cost per', 'flexible-shipping' ) ), |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @return array |
| 93 | */ |
| 94 | private function get_based_on_options() { |
| 95 | $options = array(); |
| 96 | foreach ( $this->available_additional_costs as $additional_cost ) { |
| 97 | $options[] = array( |
| 98 | 'value' => $additional_cost->get_based_on(), |
| 99 | 'label' => $additional_cost->get_name(), |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | return $options; |
| 104 | } |
| 105 | |
| 106 | } |
| 107 |