Exporter
1 week ago
Importer
1 week ago
Exporter.php
1 week ago
ExporterData.php
1 week ago
ImporterData.php
1 week ago
ShippingClassTrait.php
1 week ago
ShippingClassTrait.php
91 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Trait ImporterExporter |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\ImporterExporter |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\TableRate\ImporterExporter; |
| 9 | |
| 10 | use stdClass; |
| 11 | use WPDesk\FS\TableRate\ImporterExporter\Importer\Exception\ImportCSVException; |
| 12 | |
| 13 | /** |
| 14 | * Trait ShippingClassTrait |
| 15 | * |
| 16 | * @package WPDesk\FS\TableRate\ImporterExporter |
| 17 | */ |
| 18 | trait ShippingClassTrait { |
| 19 | /** |
| 20 | * Hashmap for shipping classes with name->term_id data. |
| 21 | * |
| 22 | * @var stdClass[] |
| 23 | */ |
| 24 | public $wc_shipping_classes_hashmap; |
| 25 | |
| 26 | /** |
| 27 | * Init action. |
| 28 | */ |
| 29 | public function init_shipping_class() { |
| 30 | $this->wc_shipping_classes_hashmap = $this->prepare_shipping_class_hashmap(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Prepares hashmap for fast checking the term_id of given shipment class. |
| 35 | * |
| 36 | * @return array |
| 37 | */ |
| 38 | public function prepare_shipping_class_hashmap() { |
| 39 | $shipping_classes = array(); |
| 40 | |
| 41 | foreach ( WC()->shipping()->get_shipping_classes() as $class ) { |
| 42 | $shipping_classes[ html_entity_decode( $class->name ) ] = (int) $class->term_id; |
| 43 | } |
| 44 | |
| 45 | return $shipping_classes; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Find and returns shipping class term id |
| 50 | * |
| 51 | * @param string $name Shipping class name to search. |
| 52 | * |
| 53 | * @return int|null Term id |
| 54 | */ |
| 55 | public function find_shipping_class_by_name( $name ) { |
| 56 | $name = html_entity_decode( $name ); |
| 57 | if ( isset( $this->wc_shipping_classes_hashmap[ $name ] ) ) { |
| 58 | return (int) $this->wc_shipping_classes_hashmap[ $name ]; |
| 59 | } |
| 60 | |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates a shipping class |
| 66 | * |
| 67 | * @param string $name Shipping class name. |
| 68 | * @param string $description Shipping class description. |
| 69 | * |
| 70 | * @return int Term id |
| 71 | * @throws ImportCSVException When can't create the class. |
| 72 | */ |
| 73 | public function create_shipping_class( $name, $description ) { |
| 74 | $term_id = wp_insert_term( $name, 'product_shipping_class', array( 'description' => $description ) ); |
| 75 | if ( is_wp_error( $term_id ) ) { |
| 76 | throw new ImportCSVException( |
| 77 | sprintf( |
| 78 | // Translators: rule shipping class and wp_error message. |
| 79 | __( 'Error while creating shipping class: %1$s, %2$s', 'flexible-shipping' ), |
| 80 | $name, |
| 81 | $term_id->get_error_message() |
| 82 | ) |
| 83 | ); |
| 84 | } |
| 85 | $term_id = (int) $term_id['term_id']; |
| 86 | $this->wc_shipping_classes_hashmap[ html_entity_decode( $name ) ] = $term_id; |
| 87 | |
| 88 | return $term_id; |
| 89 | } |
| 90 | } |
| 91 |