クラス
Item
Item API
ソース ソース
ファイル: src/API/Item.php
class Item { /** * Master object * * @var Master */ private $master; /** * Contains methods for checking stock, etc. * * @var Validation */ public $guards; /** * Sets up `Item` * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param Master $master * @return void */ public function __construct(Master $master) { $this->master = $master; $this->guards = new Validation($master); } /** * Adds an item to wishlist given a set of params * * @important * @author Evan D Shaw <evandanielshaw@gmail.com> * @param int $post_id * @param string $sku **MUST** be urldecoded * @param array $item_option * @param string $advance * @return GenericError|true */ public function addToWishlist($post_id, $sku, $item_option = [], $advance = '') { $exists = $this->guards->itemExists($post_id, $sku); if ($exists instanceof GenericError) { return $exists; } if (!defined('WCEX_WIDGET_CART')) { if (!empty($item_option)) { // by default we use `wcex_widget_cart`'s way of serializing option data but multi-select // option values are serialized differently when not using `wcex_widget_cart`. Because of this, // we have to transform multi-select option values when `wcex_widget_cart` is not being used. $item_option = self::convertWidgetCartItemOptionPayloadToWelcartDefault($item_option, $post_id, $sku); } // By default, Welcart serializes with the encoded SKU. `wcex_widget_cart` serializes with the decoded SKU. $sku = urlencode($sku); } $opterror = $this->guards->requiredOptionsProvided($post_id, $sku, $item_option); if ($opterror instanceof GenericError) { return $opterror; } $tempcart = new \usces_cart(); if (!empty($item_option)) { // we are forced to set $_POST at runtime here since `in_serialize` uses $_POST internally $_POST['itemOption'] = $item_option; } $tempcart->in_serialize($post_id, $sku); unset($_POST['itemOption']); if (!empty($advance)) { $advance = $tempcart->wc_serialize($advance); } $hookargs = [ 'post_id' => $post_id, 'sku' => $sku, 'item_option' => $item_option, 'advance' => $advance, ]; $customerror = apply_filters('wcexwl_filter_item_add_to_wishlist_custom_validation', null, $hookargs); if ($customerror !== null) { return $customerror; } $serial = apply_filters('wcexwl_filter_item_add_to_wishlist_serial', $tempcart->serial, $hookargs); // check if logged in after all other checks pass so that we can add the item // to the wishlist automatically after a successful login $isloggedin = $this->guards->isLoggedIn(); if ($isloggedin instanceof GenericError) { $isloggedin->setData($serial); return $isloggedin; } $res = CRUD::executeAddToWishlist($post_id, $sku, $serial, $advance); if ($res === false) { return $this->master->estore->getErrorResponse(Store::INTERNAL_SERVER_ERROR); } return true; } /** * Given an item option array formatted the same way as WCEX Widget Cart, this method * transforms the values that differ from Welcart's default handling of item option data * so that no discrepancies exist between cart and wishlist items that are identical. * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param array $item_option * @param int $post_id * @param string $sku **MUST** be urldecoded * @return array */ public static function convertWidgetCartItemOptionPayloadToWelcartDefault($item_option, $post_id, $sku) { global $usces; $ioptkeys = $usces->get_itemOptionKey($post_id, true); if ($ioptkeys) { foreach ($ioptkeys as $key => $value) { $optValues = $usces->get_itemOptions(urldecode($value), $post_id); if (1 == $optValues['means']) { // multiselect if (!empty($item_option[$post_id][$sku][$value])) { $item_option[$post_id][$sku][$value] = explode(',', $item_option[$post_id][$sku][$value][0]); } } } } if (isset($item_option[$post_id][$sku])) { $item_option[$post_id][urlencode($sku)] = $item_option[$post_id][$sku]; if ($sku !== urlencode($sku)) { // remove after encoding and replacing, if necessary unset($item_option[$post_id][$sku]); } } return $item_option; } /** * Adds an item to wishlist with `$_POST` values * * @important * @author Evan D Shaw <evandanielshaw@gmail.com> * @return GenericError|string */ public function postToWishlist() { $requiredFields = ['postId', 'sku']; foreach ($requiredFields as $field) { if (empty($_POST[$field])) { $emessage = $this->master->estore->getErrorCodeMap()[Store::UNKNOWN_ERROR]->message; return $this->master->estore->getErrorResponse(Store::REQUIRED_FIELDS_MISSING, [$field], [$emessage]); } } $post_id = (int)$_POST['postId']; $sku = (string)$_POST['sku']; $item_option = isset($_POST['itemOption']) ? $_POST['itemOption'] : []; $advance = isset($_POST['advance']) ? $_POST['advance'] : ''; $added = $this->addToWishlist($post_id, $sku, $item_option, $advance); if ($added !== true) { return $added; } if ($_SERVER['HTTP_REFERER']) { $_SESSION['usces_previous_url'] = esc_url($_SERVER['HTTP_REFERER']); } else { $_SESSION['usces_previous_url'] = str_replace('https://', 'http://', get_home_url()) . '/'; } return 'success'; } }
- __construct — Sets up Item
- addToWishlist — Adds an item to wishlist given a set of params
- convertWidgetCartItemOptionPayloadToWelcartDefault — Given an item option array formatted the same way as WCEX Widget Cart, this method transforms the values that differ from Welcart's default handling of item option data so that no discrepancies exist between cart and wishlist items that are identical.
- postToWishlist — Adds an item to wishlist with $_POST values