クラス
Cart
Cart API
ソース ソース
ファイル: src/API/Cart.php
class Cart { /** * Master object * * @var Master */ public $master; /** * Contains methods for checking stock, etc. * * @var Validation */ public $guards; /** * Sets up `Cart` * * @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 a serialized item to wishlist * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param string $serial The Welcart item serialized * @param string $advance Default: '' * @return GenericError|true */ public function addToWishlistBySerial($serial, $advance = '') { $item = unserialize($serial); if (!is_array($item) || empty($item)) { return $this->master->estore->getErrorResponse(Store::UNSERIALIZATION_ERROR, [$serial]); } $ids = array_keys($item); if (empty($ids)) { return $this->master->estore->getErrorResponse(Store::SERIALIZED_ITEM_IS_MALFORMED); } $post_id = (int)$ids[0]; $skus = array_keys($item[$post_id]); if (empty($skus)) { return $this->master->estore->getErrorResponse(Store::SERIALIZED_ITEM_IS_MALFORMED); } $sku = (string)$skus[0]; $exists = $this->guards->itemExists($post_id, $sku); if ($exists instanceof GenericError) { return $exists; } // 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; } /** * Adds an item or multiple items to wishlist by cart serial * * @todo Consider requiring `advance` in `$payload` since currently this method * implicitly relies on `$_SESSION['usces_cart']` for the `advance` value * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \wpdb $wpdb * @param array $args URI parameters. **Unused** * @param array $payload { * The array parsed from `json_encoded` data stored in `$_POST['payload']`. * * @type array $serials Array of one or more serialized Welcart items * } * @return GenericError|string If no error occurs, `success` is returned */ public function postToWishlistBySerial(array $args, array $payload) { global $wpdb; if (empty($payload['serials'])) { $emessage = $this->master->estore->getErrorCodeMap()[Store::UNKNOWN_ERROR]->message; return $this->master->estore->getErrorResponse(Store::REQUIRED_FIELDS_MISSING, ['serials'], [$emessage]); } $serials = !is_array($payload['serials']) ? [] : $payload['serials']; $cart = isset($_SESSION['usces_cart']) ? $_SESSION['usces_cart'] : []; $loggedout = false; $wpdb->query('START TRANSACTION;'); foreach ($serials as $serial) { $serial = urldecode($serial); $advance = isset($cart[$serial]['advance']) ? $cart[$serial]['advance'] : ''; $res = $this->addToWishlistBySerial($serial, $advance); if ($res instanceof GenericError) { if ($res->errorcode !== Store::NOT_LOGGED_IN) { $wpdb->query('ROLLBACK;'); return $res; } $loggedout = true; } /** * Fires after **one** serialized item from the cart is added to the wishlist. * * @important * @param \Aivec\Welcart\Extensions\Wishlist\API\Cart $this Instance of the `Wishlist` API class * @param string $serial The serialized Welcart item * @param string $advance Extra form data * @param array $payload See \Aivec\Welcart\Extensions\Wishlist\API\Cart::postToWishlistBySerial() * for details */ do_action('wcexwl_cart_api_add_to_wishlist_on_complete', $this, $serial, $advance, $payload); } if ($loggedout === true) { $error = $this->master->estore->getErrorResponse(Store::NOT_LOGGED_IN); $error->setData($serials); return $error; } $wpdb->query('COMMIT;'); /** * Fires after **one or more** serialized items from the cart have been added to the wishlist. * * @important * @param \Aivec\Welcart\Extensions\Wishlist\API\Cart $this Instance of the `Wishlist` API class * @param array $serials Array of one or more serialized Welcart items * @param array $payload See \Aivec\Welcart\Extensions\Wishlist\API\Cart::postToWishlistBySerial() * for details */ do_action('wcexwl_cart_api_batch_add_to_wishlist_on_complete', $this, $serials, $payload); return 'success'; } /** * Batch deletes items from cart * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param array $args URI parameters. **Unused** * @param array $payload { * The array parsed from `json_encoded` data stored in `$_POST['payload']`. * * @type array $serials Array of one or more serialized Welcart items * } * @return GenericError|string If no error occurs, `success` is returned */ public function batchDelete(array $args, array $payload) { if (empty($payload['serials'])) { $emessage = $this->master->estore->getErrorCodeMap()[Store::UNKNOWN_ERROR]->message; return $this->master->estore->getErrorResponse(Store::REQUIRED_FIELDS_MISSING, ['serials'], [$emessage]); } $serials = $payload['serials']; foreach ($serials as $serial) { $serial = urldecode($serial); unset($_SESSION['usces_cart'][$serial]); /** * Fires after **one** serialized item is deleted from the cart. * * @important * @param \Aivec\Welcart\Extensions\Wishlist\API\Cart $this Instance of the `Wishlist` API class * @param string $serial The serialized Welcart item * @param array $payload See \Aivec\Welcart\Extensions\Wishlist\API\Cart::batchDelete() * for details */ do_action('wcexwl_cart_api_delete_on_complete', $this, $serial, $payload); } /** * Fires after **one or more** serialized items have been deleted from the cart. * * @important * @param \Aivec\Welcart\Extensions\Wishlist\API\Cart $this Instance of the `Wishlist` API class * @param array $serials Array of one or more serialized Welcart items * @param array $payload See \Aivec\Welcart\Extensions\Wishlist\API\Cart::postToWishlistBySerial() * for details */ do_action('wcexwl_cart_api_batch_delete_on_complete', $this, $serials, $payload); return 'success'; } }
- __construct — Sets up Cart
- addToWishlistBySerial — Adds a serialized item to wishlist
- batchDelete — Batch deletes items from cart
- postToWishlistBySerial — Adds an item or multiple items to wishlist by cart serial