VariationOptions.php
85 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpae\App\Service\Pro\VariationOptions; |
| 4 | |
| 5 | |
| 6 | use Wpae\App\Service\VariationOptions\VariationOptionsInterface; |
| 7 | use Wpae\App\Service\VariationOptions\VariationOptions as BasicVariationOptions; |
| 8 | |
| 9 | class VariationOptions extends BasicVariationOptions implements VariationOptionsInterface |
| 10 | { |
| 11 | public function preprocessPost(\WP_Post $entry) |
| 12 | { |
| 13 | $productVariationMode = \XmlExportEngine::getProductVariationMode(); |
| 14 | |
| 15 | if (!$this->shouldTitleBeProcessed($productVariationMode)) { |
| 16 | return $entry; |
| 17 | } |
| 18 | |
| 19 | if($entry->post_type != 'product_variation') { |
| 20 | return $entry; |
| 21 | } |
| 22 | |
| 23 | if ($entry->post_type == 'product_variation') { |
| 24 | $entryId = $entry->ID; |
| 25 | $entryTitle = $entry->post_title; |
| 26 | $entryStatus = $entry->post_status; |
| 27 | $entryOrder = $entry->menu_order; |
| 28 | $parentId = $entry->post_parent; |
| 29 | $parent = get_post($parentId); |
| 30 | if ( ! empty($parent) ){ |
| 31 | /** @noinspection PhpDynamicFieldDeclarationInspection */ |
| 32 | $parent->originalPost = clone $entry; |
| 33 | $entry = $parent; |
| 34 | $entry->ID = $entryId; |
| 35 | $entry->post_status = $entryStatus; |
| 36 | $entry->menu_order = $entryOrder; |
| 37 | $entry->post_parent = $parentId; |
| 38 | if (\XmlExportEngine::getProductVariationTitleMode() == \XmlExportEngine::VARIATION_USE_DEFAULT_TITLE) { |
| 39 | $entry->post_title = $entryTitle; |
| 40 | } |
| 41 | $entry->post_type = 'product_variation'; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return $entry; |
| 46 | } |
| 47 | |
| 48 | public function getQueryWhere($wpdb, $where, $join, $closeBracket = false) |
| 49 | { |
| 50 | if (\XmlExportEngine::getProductVariationMode() == \XmlExportEngine::VARIABLE_PRODUCTS_EXPORT_PARENT) { |
| 51 | return " AND ($wpdb->posts.post_type = 'product') "; |
| 52 | } else if (\XmlExportEngine::getProductVariationMode() == \XmlExportEngine::VARIABLE_PRODUCTS_EXPORT_VARIATION) { |
| 53 | |
| 54 | return " AND $wpdb->posts.ID NOT IN ( |
| 55 | SELECT DISTINCT $wpdb->posts.post_parent |
| 56 | FROM $wpdb->posts |
| 57 | WHERE $wpdb->posts.post_type = 'product_variation' |
| 58 | ) AND $wpdb->posts.ID NOT IN (SELECT o.ID FROM $wpdb->posts o |
| 59 | LEFT OUTER JOIN $wpdb->posts r |
| 60 | ON o.post_parent = r.ID |
| 61 | WHERE r.post_status = 'trash' AND o.post_type = 'product_variation') |
| 62 | OR ($wpdb->posts.post_type = 'product_variation' AND $wpdb->posts.post_status <> 'trash' |
| 63 | |
| 64 | AND $wpdb->posts.post_parent IN ( |
| 65 | SELECT DISTINCT $wpdb->posts.ID |
| 66 | FROM $wpdb->posts $join |
| 67 | WHERE $where |
| 68 | ) |
| 69 | ".$this->getVariationsWhere($where, $join)." |
| 70 | )"; |
| 71 | } else { |
| 72 | return $this->defaultQuery($wpdb, $where, $join, $closeBracket); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param $productVariationMode |
| 78 | * @return bool |
| 79 | */ |
| 80 | private function shouldTitleBeProcessed($productVariationMode) |
| 81 | { |
| 82 | return $productVariationMode != \XmlExportEngine::VARIABLE_PRODUCTS_EXPORT_PARENT || |
| 83 | $productVariationMode == \XmlExportEngine::VARIABLE_PRODUCTS_EXPORT_VARIATION; |
| 84 | } |
| 85 | } |