'getItemsForProductsWithVariations', false => 'getItemsForProducts', ); protected $excludedProdStatusesArr = array(); public function __construct($data, $type = '') { parent::__construct($data, $type); $excludedProdStatusesStr = UserSettings::getOption('excludedProdStatuses', ''); if (strlen($excludedProdStatusesStr) > 0) { $this->excludedProdStatusesArr = explode(',', $excludedProdStatusesStr); } } protected function getItems() { $withVariations = !(!empty($this->profile) && !empty($this->profile->onlyParent) && intval($this->profile->onlyParent)); $getItemsMethod = $this->getItemsMethods[$withVariations]; $this->items = $this->$getItemsMethod(); } protected function getItemsForProductsWithVariations() { $productsIds = isset($this->data[static::IMPORT_ITEMS_IDS_PARAM]) ? $this->data[static::IMPORT_ITEMS_IDS_PARAM] : array(); $products = $this->getProductsByIdsFilteredByProductsStatus($productsIds); $variations = array(); foreach ($products as $product) { $wcProduct = \wc_get_product($product->ID); $children = $wcProduct->get_children(); foreach ($children as $variationId) { $variation = get_post($variationId); if ($variation->post_type == "product_variation") { $variations[] = $variation; } } } $productsWithoutVariations = uslp_a4bExcludePostsByIds($products, uslp_a4bObjectsFieldToArray($variations, 'post_parent')); $variations = $this->filterVariationsByProductsStatus($variations); $productsWithVariations = array_values(array_merge($productsWithoutVariations, $variations)); return $this->sortByIds($productsWithVariations, $productsIds); } protected function getItemsForProducts() { $productsIds = isset($this->data[static::IMPORT_ITEMS_IDS_PARAM]) ? $this->data[static::IMPORT_ITEMS_IDS_PARAM] : array(); $importType = isset($this->data["isImportSingleVariation"]) ? $this->data["isImportSingleVariation"] : ""; if ($importType === "variation") { $products = uslp_a4bGetPosts(array('post__in' => !empty($productsIds) ? $productsIds : array(), 'post_type' => 'product_variation')); } else { $products = $this->getProductsByIdsFilteredByProductsStatus($productsIds); } return $this->sortByIds($products, $productsIds); } protected function sortByIds($targetArray, $orderArray) { $targetArray = array_values($targetArray); $foundProductsIds = array(); foreach ($targetArray as $key => $product) { if ('product_variation' === $product->post_type) { $foundProductsIds[$key] = $product->post_parent; } else { $foundProductsIds[$key] = $product->ID; } } if ($orderArray) { uksort($targetArray, function ($key1, $key2) use ($orderArray, $foundProductsIds) { $product1Id = $foundProductsIds[$key1]; $product2Id = $foundProductsIds[$key2]; if (array_search($product1Id, $orderArray) === array_search($product2Id, $orderArray)) { return 0; } else { return array_search($product1Id, $orderArray) > array_search($product2Id, $orderArray) ? 1 : -1; } }); } return $targetArray; } protected function getProductsByIdsFilteredByProductsStatus($productsIds) { $products = uslp_a4bGetPosts(array( 'post_type' => array('product', 'product_variation'), 'post__in' => !empty($productsIds) ? $productsIds : array(0), )); $foundProductsIds = uslp_a4bObjectsFieldToArray($products, 'ID'); $productsExcludedByStatus = !empty($this->excludedProdStatusesArr) ? uslp_a4bGetPosts(array( 'post__in' => !empty($foundProductsIds) ? $foundProductsIds : array(0), 'post_status' => $this->excludedProdStatusesArr, )) : array(); return uslp_a4bExcludePostsByIds($products, uslp_a4bObjectsFieldToArray($productsExcludedByStatus, 'ID')); } protected function filterVariationsByProductsStatus($variations) { $variationsForExclude = !empty($this->excludedProdStatusesArr) ? uslp_a4bGetPosts(array( 'post_type' => 'product_variation', 'post__in' => !empty($variations) ? uslp_a4bObjectsFieldToArray($variations, 'ID') : array(0), "post_status" => $this->excludedProdStatusesArr, )) : array(); return uslp_a4bExcludePostsByIds($variations, uslp_a4bObjectsFieldToArray($variationsForExclude, 'ID')); } protected function getAppointments() { $appointmentsIds = isset($this->data['appointmentsIds']) ? $this->data['appointmentsIds'] : null; if (!$appointmentsIds) { return array(); } $request = new \WP_Query(array('post_type' => 'wc_appointment', 'post__in' => $appointmentsIds, 'posts_per_page' => -1, 'ignore_sticky_posts' => 1)); if ($request->have_posts()) { return $request->posts; } else { return array(); } } protected function getFileOptions($itemData) { if ( UserSettings::getUserOption('useStockQuantity', 0) && 'yes' === get_post_meta($itemData->ID, '_manage_stock', true) ) { $quantity = get_post_meta($itemData->ID, '_stock', true); $quantity = (empty($quantity) || '0' === $quantity || $quantity < 0) ? 0 : (int) $quantity; } elseif ( UserSettings::getUserOption('useStockQuantity', 0) && 'outofstock' === get_post_meta($itemData->ID, '_stock_status', true) ) { $quantity = 0; } else { $quantity = 1; } $fileOptions = parent::getFileOptions($itemData); $fileOptions['quantity'] = $quantity; return $fileOptions; } protected function getCodeField($post) { if ($this->activeTemplate->code_match) { if ('product_variation' === $post->post_type) { return $this->getCodeValue($post, $this->activeTemplate->variable_product_code); } elseif ('product' === $post->post_type) { return $this->getCodeValue($post, $this->activeTemplate->single_product_code); } elseif ('shop_order' === $post->post_type || 'shop_order_placehold' === $post->post_type) { return $this->getCodeValue($post, $this->activeTemplate->single_product_code); } else { return ''; } } else { return isset($this->data['lineBarcode']) ? $this->getField($post, $this->data['lineBarcode']) : ''; } } public function getTemplateReplacements($post, $shortcodesArgs) { $replacements = new \ArrayObject(); if (!empty($this->prodListTemplate) && 'orders' === $this->type) { $replacements[$this->prodListShortcode] = $this->getProdListHtml($post); } if ( is_plugin_active('tier-pricing-table/tier-pricing-table.php') || is_plugin_active('tier-pricing-table-premium/tier-pricing-table.php') ) { if (!empty($this->tieredPriceTemplate) && in_array($post->post_type, array('product', 'product_variation'))) { $replacements[$this->tieredPriceShortcode] = $this->getTieredPriceListHtml($post); } } foreach ($shortcodesArgs as $shortCode => $args) { try { $replacements[$shortCode] = $this->getShortcodeFieldValue($post, $args); } catch (LabelGenerationError $e) { $replacements[$shortCode] = ''; $replacements['barcode_error_message'] = $e->getMessage(); } } return $replacements; } protected function getProdListHtml($post) { $prodListHtml = ''; $orderProducts = $this->getOrderItemsProducts(array($post->ID)); foreach ($orderProducts as $product) { $productHtml = $this->prodListTemplate; foreach ($this->prodListShortcodesArgs as $shortCode => $args) { $args['qty'] = $product->productInOrderQty; try { $productHtml = str_replace($shortCode, $this->getShortcodeFieldValue($product, $args), $productHtml); } catch (LabelGenerationError $e) { $productHtml = ''; } } $prodListHtml .= $productHtml; } return $prodListHtml; } protected function getTieredPriceListHtml($post) { $resultHtml = ''; $product = wc_get_product( $post->ID ); $product_id = $post->ID; $priceRules = PriceManager::getPriceRules($product_id); if (empty($priceRules) && 'product_variation' === $post->post_type) { $product_id = $post->post_parent; $priceRules = PriceManager::getPriceRules($product_id); $product = wc_get_product($product_id); } $pricingType = PriceManager::getPricingType($product_id); if ('fixed' !== $pricingType) { return $resultHtml; } $realPrice = $product->get_price(); $minimum = PriceManager::getProductQtyMin($product_id, 'view'); if (empty($priceRules) || (1 >= array_keys($priceRules)[0] - $minimum)) { $currentRangeAmount = $minimum; } else { $currentRangeAmount = $minimum . ' - ' . (array_keys($priceRules)[0] - 1); } $currentRangePrice = $realPrice; $resultHtml .= $this->getTieredPriceRangeHtml($currentRangeAmount, $currentRangePrice); $iterator = new \ArrayIterator($priceRules); while ($iterator->valid()) { $currentRangePrice = $iterator->current(); $current_quantity = $iterator->key(); $iterator->next(); if ($iterator->valid()) { $quantity = $current_quantity; $currentRangeAmount = $quantity; if (intval($iterator->key() - 1 != $current_quantity)) { $currentRangeAmount = number_format_i18n($quantity) . ' - ' . number_format_i18n(intval($iterator->key() - 1)); } } else { $currentRangeAmount = number_format_i18n($current_quantity) . '+'; } $resultHtml .= $this->getTieredPriceRangeHtml($currentRangeAmount, $currentRangePrice); } return $resultHtml; } protected function getTieredPriceRangeHtml($currentRangeAmount, $currentRangePrice) { $priceRangeHtml = $this->tieredPriceTemplate; foreach ($this->tieredPriceShortcodesArgs as $shortCode => $args) { if (false !== strpos($shortCode, '[qty-range-amount')) { $priceRangeHtml = str_replace($shortCode, $currentRangeAmount, $priceRangeHtml); } elseif (false !== strpos($shortCode, '[qty-range-price')) { $field = array('args' => $args); $wpCustomShortcode = Custom::getInstance(); $priceRangeHtml = str_replace($shortCode, $wpCustomShortcode->getValueWithCurrency($currentRangePrice, $field), $priceRangeHtml); } } return $priceRangeHtml; } public function extractTemplateShortcodes($template, &$shortcodesArgs) { $this->extractListTemplateData( $template, '[product-list-start', '[product-list-end]', $this->prodListTemplate, $this->prodListShortcode, $this->prodListShortcodesArgs ); $this->extractListTemplateData( $template, '[tiered-price-table-start', '[tiered-price-table-end]', $this->tieredPriceTemplate, $this->tieredPriceShortcode, $this->tieredPriceShortcodesArgs ); parent::extractTemplateShortcodes($template, $shortcodesArgs); } }