関数
PurchaseSanityChecks::checkCartItemsInventory( string $mes, array $cart )
パラメータ パラメータ
- $mes
(文字列) (必須)
- $cart
(配列) (必須)
ファイル: src/API/PurchaseSanityChecks.php
public static function checkCartItemsInventory($mes, $cart) { global $usces; // just quit if none of the cart items are combo-sets $found = false; foreach ($cart as $crow) { $sels = unserialize($crow['serial']); if (!empty($sels['comboSetId']) && !empty($sels['comboSetItems'])) { $found = true; break; } } if ($found === false) { return $mes; } $mes = ''; $stocks = []; foreach ($cart as $i => $cart_row) { $rowmes = ''; $sels = unserialize($cart_row['serial']); $cart_row = $cart[$i]; $post_id = $cart_row['post_id']; $sku = $cart_row['sku']; $sku_code = urldecode($cart_row['sku']); $quant = isset($_POST['quant'][$i][$post_id][$sku]) ? (int)trim($_POST['quant'][$i][$post_id][$sku]) : $cart_row['quantity']; $stock = $usces->getItemZaikoNum($post_id, $sku_code); if (!isset($stocks[$post_id][$sku])) { if (!\WCUtils::is_blank($stock)) { $stocks[$post_id][$sku] = $stock; } else { $stocks[$post_id][$sku] = null; } } $checkstock = $stocks[$post_id][$sku]; $stocks[$post_id][$sku] = $stocks[$post_id][$sku] - $quant; $product = wel_get_product($post_id); $itemRestriction = isset($product['itemRestriction']) ? $product['itemRestriction'] : ''; $itemOrderAcceptable = $usces->getItemOrderAcceptable($post_id); $post_status = get_post_status($post_id); if (1 > (int)$quant) { $rowmes .= sprintf(__('Enter the correct amount for the No.%d item.', 'usces'), ($i + 1)) . '<br />'; } elseif (!$usces->is_item_zaiko($post_id, $sku_code) || ($itemOrderAcceptable != 1 && \WCUtils::is_zero($stock)) || 'publish' != $post_status) { $rowmes .= sprintf(__('Sorry, No.%d item is sold out.', 'usces'), ($i + 1)) . '<br />'; } elseif ($quant > (int)$itemRestriction && !\WCUtils::is_blank($itemRestriction) && !\WCUtils::is_zero($itemRestriction)) { $rowmes .= sprintf(__('This article is limited by %1$d at a time for the No.%2$d item.', 'usces'), $itemRestriction, ($i + 1)) . '<br />'; } elseif ($itemOrderAcceptable != 1 && 0 > $stocks[$post_id][$sku] && !\WCUtils::is_blank($stock)) { $rowmes .= sprintf(__('Stock of No.%1$d item is remainder %2$d.', 'usces'), ($i + 1), $checkstock) . '<br />'; } $mes .= $rowmes; if ((int)$quant > 0 && empty($rowmes)) { $gierrors = []; if (!empty($sels['comboSetId']) && !empty($sels['comboSetItems'])) { $mapifiedselections = []; foreach ($sels['comboSetItems'] as $giserial => $gitem) { $data = unserialize($giserial); $mapifiedselections[$data['groupId']][] = $data['itemId']; } $result = self::comboSetSelectionsAreValid($sels['comboSetId'], $mapifiedselections); if ($result instanceof GenericError) { $message = $result->message; if ($result->errorcode === ErrorStore::COMBO_GROUP_IS_REQUIRED) { $message = __('Selections for this combo-set item have been updated. Please refresh the page and try again.', 'wcexics'); } $gierrors[] = $message; } else { $giindex = 0; foreach ($sels['comboSetItems'] as $giserial => $gitem) { $gitemquant = $sels['comboSetItems'][$giserial]['quant'] * $quant; $gicrow = (new \usces_cart())->key_unserialize($giserial); $gipost_id = $gicrow['post_id']; $gisku = $gicrow['sku']; $gisku_code = urldecode($gicrow['sku']); $stock = $usces->getItemZaikoNum($gipost_id, $gisku_code); if (!isset($stocks[$gipost_id][$gisku])) { if (!\WCUtils::is_blank($stock)) { $stocks[$gipost_id][$gisku] = $stock; } else { $stocks[$gipost_id][$gisku] = null; } } $checkstock = $stocks[$gipost_id][$gisku]; $stocks[$gipost_id][$gisku] = $stocks[$gipost_id][$gisku] - $gitemquant; $giproduct = wel_get_product($gipost_id); $itemRestriction = isset($giproduct['itemRestriction']) ? $giproduct['itemRestriction'] : ''; $itemOrderAcceptable = $usces->getItemOrderAcceptable($gipost_id); $post_status = get_post_status($gipost_id); if (!$usces->is_item_zaiko($gipost_id, $gisku_code) || ($itemOrderAcceptable != 1 && \WCUtils::is_zero($stock)) || 'publish' != $post_status) { $gierrors[] = sprintf(__('Sorry, No.%d item is sold out.', 'usces'), ($giindex + 1)); } elseif ($gitemquant > (int)$itemRestriction && !\WCUtils::is_blank($itemRestriction) && !\WCUtils::is_zero($itemRestriction)) { $gierrors[] = sprintf(__('This article is limited by %1$d at a time for the No.%2$d item.', 'usces'), $itemRestriction, ($giindex + 1)); } elseif ($itemOrderAcceptable != 1 && 0 > $stocks[$gipost_id][$gisku] && !\WCUtils::is_blank($stock)) { $gierrors[] = sprintf(__('Stock of No.%1$d item is remainder %2$d.', 'usces'), ($giindex + 1), $checkstock); } $giindex++; } } } $mes .= self::constructIndexedErrorMessage($i, $gierrors); } } return $mes; }