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