Skip to content

Commit

Permalink
Build v2.3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
jcollings committed Nov 16, 2024
1 parent 5d3928a commit f3999d7
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Requires Import WP: 2.11.6

**Version: 2.3.5-rc1**
**Version: 2.3.5**

![WooCommerce Product Importer](./assets/iwp-addon-woocommerce.png)

Expand Down Expand Up @@ -52,6 +52,11 @@ The WooCommerce Product Importer Addon can currently only be installed by downlo

## Changelog

### 2.3.5

- ADD - Add new field for "GTIN, UPC, EAN, or ISBN".
- FIX - stop empty local attributes being added.

### 2.3.4

- ADD - Add featured product field to advanced section.
Expand Down
117 changes: 113 additions & 4 deletions class/Importer/Template/ProductTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,23 @@ public function register()
])
]);

$attributes = apply_filters('iwp/woocommerce/xml_child_variation_attributes', []);
if (!empty($attributes)) {

$attribute_fields = [];
foreach ($attributes as $attribute) {
$attribute_fields[] = $this->register_field('Attribute ' . $attribute, 'attribute_' . $attribute);
}

$groups[] = $this->register_group('Variations', 'variations', array_merge([

$this->register_field('Stock', 'stock'),
$this->register_field('Price', 'price'),
$this->register_field('Sku', 'sku'),

], $attribute_fields), ['type' => 'repeatable', 'row_base' => true]);
}

$groups[] = $this->register_group('Shipping', 'shipping', [
$this->register_group('Product Dimensions', 'dimensions', [
$this->register_field('Weight', '_weight'),
Expand Down Expand Up @@ -548,8 +565,8 @@ public function post_process($post_id, ParsedData $data)
if (isset($wc_data['_sku'])) {
$product->set_sku($wc_data['_sku']);
}
if(isset($wc_data['_global_unique_id']) && method_exists($product, 'set_global_unique_id')){

if (isset($wc_data['_global_unique_id']) && method_exists($product, 'set_global_unique_id')) {
$product->set_global_unique_id($wc_data['_global_unique_id']);
}

Expand Down Expand Up @@ -707,11 +724,100 @@ public function post_process($post_id, ParsedData $data)
$this->process_product_gallery($product, $data);

$product->save();

// Needs to be after product save, due to fetching data.
if ($product->is_type('variable')) {
$this->process_inline_variations($product, $data);
}
} catch (\Exception $e) {
throw new MapperException($e->getMessage());
}
}

function process_inline_variations($product, $data)
{
$attributes = apply_filters('iwp/woocommerce/xml_child_variation_attributes', []);
if (empty($attributes)) {
return;
}

/**
* @var \ImportWP\Common\Importer\ParsedData $data
*/
$variations = $data->getData('variations.0');

$tmp_variations = [];
foreach ($variations as $variation_data) {

$sku = $variation_data['variations.0.sku'];
$stock = $variation_data['variations.0.stock'];
$price = $variation_data['variations.0.price'];

$formatted_attrs = [];
foreach ($attributes as $attr) {

// if starts with pa_ then we get the id
$name = $variation_data['variations.0.attribute_' . $attr];

if (empty($name)) {
continue;
}

if (strpos($attr, 'pa_') === 0) {
$attr_term = get_term_by('name', $name, $attr);
if ($attr_term) {
$name = $attr_term->slug;
}
}
$formatted_attrs['attribute_' . $attr] = $name;
}

$variation_id = false;

/**
* @var \WC_Product_Variable $product
*/
$available_variations = array_merge(
$product->get_available_variations(),
$tmp_variations
);

foreach ($available_variations as $existing_variation) {

if (
(!empty($sku) && $existing_variation['sku'] == $sku)
|| !array_diff_assoc($formatted_attrs, $existing_variation['attributes'])
) {
$variation_id = $existing_variation['variation_id'];
break;
}
}

if (!empty($variation_id)) {
$variation = wc_get_product_object('variation', $variation_id);
} else {
$variation = new \WC_Product_Variation();
$variation->set_attributes($formatted_attrs);
}

$variation->set_sku($sku);
$variation->set_parent_id($product->get_id());

if ("" !== $stock) {
$variation->set_manage_stock(true);
$variation->set_stock_quantity($stock);
}
$variation->set_regular_price($price);
$variation->save();

$tmp_variations[] = [
'variation_id' => $variation->get_id(),
'sku' => $variation->get_sku(),
'attributes' => $formatted_attrs
];
}
}

function get_product_object($post_id, $wc_data)
{
$found_type = false;
Expand Down Expand Up @@ -865,7 +971,7 @@ private function set_downloads(&$product, $data, $wc_data)
* @param \WC_Product_Variation $variation
* @param ParsedData $data
* @return void
* @throws Exception
* @throws \Exception
*/
public function set_variation_data(&$variation, ParsedData $data)
{
Expand Down Expand Up @@ -1184,6 +1290,9 @@ public function set_product_data(&$product, $data)
}
}

// remove duplicate attributes
$terms = array_values(array_unique($terms));

if ($attribute_id) {
if (isset($terms)) {

Expand Down Expand Up @@ -1219,7 +1328,7 @@ public function set_product_data(&$product, $data)
$attribute_object->set_variation($is_variation);
$attributes[] = $attribute_object;
}
} elseif (isset($terms)) {
} elseif (!empty($terms)) {
// Check for default attributes and set "is_variation".
// if (isset($attribute['default']) && !empty($attribute['default']) && in_array($attribute['default'], $terms, true)) {
// $default_attributes[sanitize_title($name)] = $attribute['default'];
Expand Down
2 changes: 1 addition & 1 deletion woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Plugin URI: https://www.importwp.com
* Description: Allow Import WP to import WooCommerce Products.
* Author: James Collings <[email protected]>
* Version: 2.3.5-rc1
* Version: 2.3.5
* Author URI: https://www.importwp.com
* Network: True
*/
Expand Down

0 comments on commit f3999d7

Please sign in to comment.