AvS_FastSimpleImport
AvS_FastSimpleImport copied to clipboard
Incorrect references after invalidBundleProductSku error
The situation: We have one required product per option. Each bundle is a complete set of unchangeable items and once an item is out of stock, the whole bundle is not saleable:
Bundle A
- Option A1: Simple 1
- Option A2: Simple 2
Bundle B
- Option B1: Simple 3
- Option B2: Simple 4
The error: When a number of items (e.g. Simple 2) is not available in the catalog, all subsequent references (=option selections) are wrong:
Bundle A
- Option A1: Simple 1
- Option A2: Simple 3
Bundle B
- Option B1: Simple 4
- Option B2:
The effect:
- Bundle A has wrong items
- Bundle B is not saleable
I know this is mainly caused by data errors in the source. If it affected only Bundle A then I would not even care. But the behaviour corrupts otherwise faultless bundles.
The cause:
If an SKU does not exist in the catalog, a row error is added and no selection is collected in $bundleSelections
for later storage. The bundle option that is supposed to contain the selection(s) is not dropped when empty. This leads to a mismatch between $bundleOptions
and $bundleSelections
.
The $optionId
simply gets incremented for selection storage. While $bundleSelections
already looped to the next $productId
, $bundleOptions
is still in scope of the previous one. The option-to-selection(s) references are wrong.
The solution:
I fixed this behaviour for the given situation by aligning $optionId
on every product (bundle) iteration: I additionally increased the $optionId
by the number of empty options. This way the data error remains at the faultly bundle while subsequent bundles receive correct references.
211 211 foreach ($bundleSelections as $productId => $selections) {
212 212 foreach ($selections as $title => $selection) {
213 213 foreach ($selection as &$sel) {
214 214 $productRelations[] = array(
215 215 'parent_id' => $sel['parent_product_id'],
216 216 'child_id' => $sel['product_id']
217 217 );
218 218 $sel['option_id'] = $optionId;
219 219 }
220 220 $optionId++;
221 221 $optionSelections = array_merge($optionSelections, $selection);
222 222 }
223 + $optionId+= (count($bundleOptions[$productId]) - count($bundleSelections[$productId]));
223 224 }
Bundle A
- Option A1: Simple 1
- Option A2:
Bundle B
- Option B1: Simple 3
- Option B2: Simple 4
Integration: I suggest testing the given solution in others than the given situation like different multiplicity or different import behaviour.
The same error occurs when there are no selections actually available in the catalog (opposed to some as reported before).
I fixed this by comparing the array keys (product IDs) of $bundleOptions
and $bundleSelections
:
171 + $bundleOptions = array_intersect_key($bundleOptions, $bundleSelections);
171 172 if (count($bundleOptions)) {