flexible-shipping
/
vendor_prefixed
/
wpdesk
/
wp-persistence
/
src
/
Adapter
/
WooCommerce
/
WooCommerceShippingInstanceContainer.php
flexible-shipping
/
vendor_prefixed
/
wpdesk
/
wp-persistence
/
src
/
Adapter
/
WooCommerce
Last commit date
WooCommerceSettingsContainer.php
1 week ago
WooCommerceShippingInstanceContainer.php
1 week ago
WooCommerceShippingInstanceContainer.php
45 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\Persistence\Adapter\WooCommerce; |
| 4 | |
| 5 | use FSVendor\WPDesk\Persistence\ElementNotExistsException; |
| 6 | use FSVendor\WPDesk\Persistence\PersistentContainer; |
| 7 | /** |
| 8 | * Can store data using WooCommerce shipping instance settings options. |
| 9 | * Use when want the abstract access to \WC_Shipping_Method. |
| 10 | * |
| 11 | * @package WPDesk\Persistence\WooCommerce |
| 12 | */ |
| 13 | final class WooCommerceShippingInstanceContainer implements PersistentContainer |
| 14 | { |
| 15 | /** @var \WC_Shipping_Method */ |
| 16 | private $method; |
| 17 | public function __construct(\WC_Shipping_Method $method) |
| 18 | { |
| 19 | $this->method = $method; |
| 20 | } |
| 21 | public function get($id) |
| 22 | { |
| 23 | if (!$this->has($id)) { |
| 24 | throw new ElementNotExistsException(sprintf('Element %s not exists!', $id)); |
| 25 | } |
| 26 | return $this->method->get_instance_option($id); |
| 27 | } |
| 28 | public function has($id) |
| 29 | { |
| 30 | return isset($this->method->instance_settings[$id]); |
| 31 | } |
| 32 | public function set($id, $value) |
| 33 | { |
| 34 | $this->method->instance_settings[$id] = $value; |
| 35 | /** @see \WC_Shipping_Method::process_admin_options */ |
| 36 | update_option($this->method->get_instance_option_key(), apply_filters('woocommerce_shipping_' . $this->method->id . '_instance_settings_values', $this->method->instance_settings, $this->method), 'yes'); |
| 37 | } |
| 38 | public function delete($id) |
| 39 | { |
| 40 | $form_fields = $this->method->get_instance_form_fields(); |
| 41 | $empty_value = isset($form_fields[$id]) ? $this->method->get_field_default($form_fields[$id]) : null; |
| 42 | $this->set($id, $empty_value); |
| 43 | } |
| 44 | } |
| 45 |