/** * Prepare bundle options. * * @param BundleProduct $product * @return array */ protected function prepareBundleOptions(BundleProduct $product) { $bundleSelections = $product->getBundleSelections(); $bundleOptions = isset($bundleSelections['bundle_options']) ? $bundleSelections['bundle_options'] : []; $result = []; foreach ($bundleOptions as $optionKey => $bundleOption) { $optionData = ['title' => $bundleOption['title'], 'type' => $bundleOption['type'], 'is_require' => $bundleOption['required']]; foreach ($bundleOption['assigned_products'] as $productKey => $assignedProduct) { $price = isset($assignedProduct['data']['selection_price_value']) ? $assignedProduct['data']['selection_price_value'] : $bundleSelections['products'][$optionKey][$productKey]->getPrice(); $optionData['options'][$productKey] = ['title' => $assignedProduct['search_data']['name'], 'price' => number_format($price, 2)]; } $result[$optionKey] = $optionData; } return $result; }
/** * Parse bundle selections in response. * * @param string $response * @return array */ protected function parseResponseSelections($response) { $selectionIdKey = 1; $optionIdKey = 2; $productNameKey = 3; $responseSelections = []; $bundleSelections = $this->fixture->getBundleSelections(); preg_match_all('/{.*"selection_id":"(\\d+)".*"option_id":"(\\d+)".*"name":"([^"]+)".*}/', $response, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $productName = $match[$productNameKey]; $responseSelections[$productName] = ['selection_id' => $match[$selectionIdKey], 'option_id' => $match[$optionIdKey]]; } foreach ($bundleSelections['bundle_options'] as $optionKey => $option) { foreach ($option['assigned_products'] as $assignedKey => $optionValue) { $productName = $optionValue['search_data']['name']; $bundleSelections['bundle_options'][$optionKey]['assigned_products'][$assignedKey] += $responseSelections[$productName]; } } return ['bundle_selections' => $bundleSelections]; }
/** * Prepare data for bundle product. * * @param BundleProduct $product * @return array * * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function prepareBundleData(BundleProduct $product) { $result = []; $checkoutData = $product->getCheckoutData(); $bundleOptions = isset($checkoutData['options']['bundle_options']) ? $checkoutData['options']['bundle_options'] : []; $bundleSelections = $product->getBundleSelections(); $bundleSelectionsData = []; $result['qty'] = $checkoutData['qty']; foreach ($bundleSelections['bundle_options'] as $option) { foreach ($option['assigned_products'] as $productData) { $productName = $productData['search_data']['name']; $bundleSelectionsData[$productName] = ['selection_id' => $productData['selection_id'], 'option_id' => $productData['option_id']]; } } foreach ($bundleOptions as $option) { $productName = $option['value']['name']; foreach ($bundleSelectionsData as $fullProductName => $value) { if (null !== strpos($fullProductName, $productName)) { $productName = $fullProductName; } } if (isset($bundleSelectionsData[$productName])) { $optionId = $bundleSelectionsData[$productName]['option_id']; $selectionId = $bundleSelectionsData[$productName]['selection_id']; $result['bundle_option'][$optionId] = $selectionId; } } return $result; }
/** * Prepare bundle product options. * * @param BundleProduct $product * @return array */ protected function prepareBundleOptions(BundleProduct $product) { $options = []; foreach ($product->getCheckoutData()['options']['bundle_options'] as $checkoutOption) { foreach ($product->getBundleSelections()['bundle_options'] as $productOption) { if (strpos($productOption['title'], $checkoutOption['title']) !== false) { $option = []; foreach ($productOption['assigned_products'] as $productData) { if (strpos($productData['search_data']['name'], $checkoutOption['value']['name']) !== false) { $qty = isset($checkoutOption['qty']) ? $checkoutOption['qty'] : $productData['data']['selection_qty']; $option['option_id'] = $productData['option_id']; $option['option_selections'][] = $productData['selection_id']; $option['option_qty'] = $qty; } } $options[] = $option; } } } return ['extension_attributes' => ['bundle_options' => $options]]; }