default.php
1 year ago
generic.php
8 years ago
mappings.php
7 years ago
shopify.php
7 years ago
wordpress.php
8 years ago
shopify.php
91 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Shopify mappings |
| 4 | * |
| 5 | * @package WooCommerce\Admin\Importers |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Add Shopify mappings. |
| 14 | * |
| 15 | * @since 3.7.0 |
| 16 | * @param array $mappings Importer columns mappings. |
| 17 | * @param array $raw_headers Raw headers from CSV being imported. |
| 18 | * @return array |
| 19 | */ |
| 20 | function wc_importer_shopify_mappings( $mappings, $raw_headers ) { |
| 21 | // Only map if this is looks like a Shopify export. |
| 22 | if ( 0 !== count( array_diff( array( 'Title', 'Body (HTML)', 'Type', 'Variant SKU' ), $raw_headers ) ) ) { |
| 23 | return $mappings; |
| 24 | } |
| 25 | $shopify_mappings = array( |
| 26 | 'Variant SKU' => 'sku', |
| 27 | 'Title' => 'name', |
| 28 | 'Body (HTML)' => 'description', |
| 29 | 'Quantity' => 'stock_quantity', |
| 30 | 'Variant Inventory Qty' => 'stock_quantity', |
| 31 | 'Image Src' => 'images', |
| 32 | 'Variant Image' => 'images', |
| 33 | 'Variant SKU' => 'sku', |
| 34 | 'Variant Price' => 'sale_price', |
| 35 | 'Variant Compare At Price' => 'regular_price', |
| 36 | 'Type' => 'category_ids', |
| 37 | 'Tags' => 'tag_ids_spaces', |
| 38 | 'Variant Grams' => 'weight', |
| 39 | 'Variant Requires Shipping' => 'meta:shopify_requires_shipping', |
| 40 | 'Variant Taxable' => 'tax_status', |
| 41 | ); |
| 42 | return array_merge( $mappings, $shopify_mappings ); |
| 43 | } |
| 44 | add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'wc_importer_shopify_mappings', 10, 2 ); |
| 45 | |
| 46 | /** |
| 47 | * Add special wildcard Shopify mappings. |
| 48 | * |
| 49 | * @since 3.7.0 |
| 50 | * @param array $mappings Importer columns mappings. |
| 51 | * @param array $raw_headers Raw headers from CSV being imported. |
| 52 | * @return array |
| 53 | */ |
| 54 | function wc_importer_shopify_special_mappings( $mappings, $raw_headers ) { |
| 55 | // Only map if this is looks like a Shopify export. |
| 56 | if ( 0 !== count( array_diff( array( 'Title', 'Body (HTML)', 'Type', 'Variant SKU' ), $raw_headers ) ) ) { |
| 57 | return $mappings; |
| 58 | } |
| 59 | $shopify_mappings = array( |
| 60 | 'Option%d Name' => 'attributes:name', |
| 61 | 'Option%d Value' => 'attributes:value', |
| 62 | ); |
| 63 | return array_merge( $mappings, $shopify_mappings ); |
| 64 | } |
| 65 | add_filter( 'woocommerce_csv_product_import_mapping_special_columns', 'wc_importer_shopify_special_mappings', 10, 2 ); |
| 66 | |
| 67 | /** |
| 68 | * Expand special Shopify columns to WC format. |
| 69 | * |
| 70 | * @since 3.7.0 |
| 71 | * @param array $data Array of data. |
| 72 | * @return array Expanded data. |
| 73 | */ |
| 74 | function wc_importer_shopify_expand_data( $data ) { |
| 75 | if ( isset( $data['meta:shopify_requires_shipping'] ) ) { |
| 76 | $requires_shipping = wc_string_to_bool( $data['meta:shopify_requires_shipping'] ); |
| 77 | |
| 78 | if ( ! $requires_shipping ) { |
| 79 | if ( isset( $data['type'] ) ) { |
| 80 | $data['type'][] = 'virtual'; |
| 81 | } else { |
| 82 | $data['type'] = array( 'virtual' ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | unset( $data['meta:shopify_requires_shipping'] ); |
| 87 | } |
| 88 | return $data; |
| 89 | } |
| 90 | add_filter( 'woocommerce_product_importer_pre_expand_data', 'wc_importer_shopify_expand_data' ); |
| 91 |