forked from hyyan/woo-poly-integration
/
Stock.php
180 lines (155 loc) · 6.45 KB
/
Stock.php
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* This file is part of the hyyan/woo-poly-integration plugin.
* (c) Hyyan Abo Fakher <hyyanaf@gmail.com>.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Hyyan\WPI\Product;
use Hyyan\WPI\Utilities;
use Hyyan\WPI\Product\Variation;
use Hyyan\WPI\Admin\Settings;
use Hyyan\WPI\Admin\MetasList;
/**
* Product Stock.
*
* Handle stock sync
*
* @author Hyyan Abo Fakher <hyyanaf@gmail.com>
*/
class Stock
{
const STOCK_REDUCE_ACTION = 'reduce';
const STOCK_INCREASE_ACTION = 'increase';
/**
* Construct object.
*/
public function __construct()
{
// sync stock
add_action(
'woocommerce_reduce_order_stock', array($this, 'reduceStock')
);
add_filter(
'woocommerce_restore_order_stock_quantity', array($this, 'increaseStock')
);
}
/**
* Reduce stock for product and its translation.
*
* @param \WC_Order $order
*/
public function reduceStock($order)
{
/* Get array of ordered products */
$items = $order->get_items();
/* Reduce stock */
foreach ($items as $item) {
$this->change($item, self::STOCK_REDUCE_ACTION);
}
}
/**
* Increase stock for product and its translation.
*
* @param int $change the stock change
*
* @return int stock change
*/
public function increaseStock($change)
{
$orderId = absint($_POST['order_id']);
$order = new \WC_Order($orderId);
/* Get array of ordered products */
$items = $order->get_items();
/* Increase stock */
foreach ($items as $item) {
$item->change = $change;
$this->change($item, self::STOCK_INCREASE_ACTION);
}
}
/**
* Change the product stock in the given order item.
*
* @param array $item the order data
* @param string $action STOCK_REDUCE_ACTION | STOCK_INCREASE_ACTION
*/
protected function change(\WC_Order_Item_Product $item, $action = self::STOCK_REDUCE_ACTION)
{
/* Proper way to get to object so it works with IPN calls */
$productObject = $item->get_product();
$productID = $productObject->get_id();
//$productLang = pll_get_post_language($productID); //#184
$orderLang = pll_get_post_language($item->get_order_id());
/* TODO check if variations are also subject to IPN problem issue #299 */
$variationID = Utilities::get_order_item_variationid($item);
/* Handle Products */
if ($productObject && $orderLang) {
/* Get the translations IDS */
$translations = Utilities::getProductTranslationsArrayByObject(
$productObject
);
$method = ($action === self::STOCK_REDUCE_ACTION) ?
'decrease' :
'increase';
$change = ($action === self::STOCK_REDUCE_ACTION) ?
Utilities::get_order_item_quantity($item) :
Utilities::get_order_item_change($item);
$isManageStock = $productObject->managing_stock();
$isVariation = $variationID && $variationID > 0;
/* Sync stock for all translation */
foreach ($translations as $ID) {
//in 3.0.8 at least, current lang item must not be removed from array if is variable
if ($isManageStock && (!$isVariation)) {
/* Skip the current product */
if ($ID == $productID) continue;
}
/* Only if product is managing stock
* including variation with stock managed at product level*/
if ($isManageStock) {
if (($translation = wc_get_product($ID))) {
\wc_update_product_stock($translation, $change, $method);
}
}
$general = Settings::getOption(
'general', MetasList::getID(), array('total_sales')
);
if (in_array('total_sales', $general)) {
update_post_meta(
$ID, 'total_sales', get_post_meta($productID, 'total_sales', true)
);
}
}
/* Handle variation stock UNLESS stock is managed on the parent
* there is a function for this $variation->get_stock_managed_by_id() however in woo-poly-context
* this returns the master language id of either the variation of the parent.
*/
if (($isVariation) && !($isManageStock)) {
//unfortunately pll functions can't be used as the
//variations are not currently linked using pll_save_post_translations
//still it might be more sensible to get master in base language, and synchronise from that
//$variationMaster = (pll_get_post_language($variationID) == pll_default_language()) ?
// wc_get_product($variationID) : Utilities::getProductTranslationByID($variationID, pll_default_language());
$variationMasterID = get_post_meta($variationID, Variation::DUPLICATE_KEY, true);
$variationMaster = wc_get_product($variationMasterID);
if ($variationMaster) {
$variationMasterManagerStock = $variationMaster->managing_stock();
if ($variationMasterManagerStock) {
//$posts = Utilities::getProductTranslationsArrayByObject($variationMaster);
$posts = Variation::getRelatedVariation($variationMasterID);
foreach ($posts as $post) {
$variation = wc_get_product($post);
if ($variation) {
//tested with orderlang, actually here it is the product language as currently variation item
//is added and handled in original language even if order switched language
if (pll_get_post_language($variation->get_parent_id()) != pll_get_post_language($productID)) {
\wc_update_product_stock($variation, $change, $method);
}
}
}
}
}
}
}
}
}