クラス
Wishlist
ソース ソース
ファイル: src/API/Wishlist.php
class Wishlist
{
/**
* Master object
*
* @var Master
*/
private $master;
/**
* Contains methods for checking stock, etc.
*
* @var Validation
*/
public $guards;
/**
* Sets up `Wishlist`
*
* @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);
}
/**
* Clears cart if `clearCart` is set in `$payload`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @param array $args URI parameters. **Unused**
* @param array $payload {
* The array parsed from `json_encoded` data stored in `$_POST['payload']`.
*
* @type bool $clearCart If `true`, the cart will be cleared before adding an item to it.
* }
* @return void
*/
public function maybeClearCart(array $args, array $payload) {
global $usces;
if (!empty($payload['clearCart'])) {
$usces->cart->clear_cart();
}
}
/**
* Returns a `GenericError` if the given wishlist item cannot be added to cart
* for some reason, `void` otherwise
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param array $args $args {
* URI parameters.
*
* @type int $itemId The wishlist item ID
* }
* @return GenericError|void
*/
public function canAddToCart(array $args) {
$id = (int)$args['itemId'];
$item = CRUD::getWishlistItemById($id);
if (empty($item)) {
return $this->master->estore->getErrorResponse(Store::WISHLIST_ITEM_NOT_FOUND, [$id]);
}
$sku = urldecode($item['sku']);
$post_id = (int)$item['postId'];
$exists = $this->guards->itemExists($post_id, $sku);
if ($exists instanceof GenericError) {
return $exists;
}
$instock = $this->guards->inStock($post_id, $sku, (int)$item['quantity']);
if ($instock instanceof GenericError) {
return $instock;
}
if ($item['optionsAreValid'] === false) {
return $this->master->estore->getErrorResponse(Store::ITEM_OPTIONS_INVALID, [$sku], [$sku]);
}
$allowed = $this->guards->cartAllowsThisItem($post_id);
if ($allowed instanceof GenericError) {
return $allowed;
}
}
/**
* Deletes a wishlist item by ID and returns the updated wishlist
*
* @important
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param array $args {
* URI parameters.
*
* @type int $itemId The wishlist item ID
* }
* @param array $payload The array parsed from `json_encoded` data stored in `$_POST['payload']`.
* @return array|GenericError See \Aivec\Welcart\Extensions\Wishlist\API\CRUD::buildWishlistRow()
*/
public function delete(array $args, array $payload) {
$res = CRUD::executeDeletion((int)$args['itemId']);
if ($res === false) {
return $this->master->estore->getErrorResponse(Store::INTERNAL_SERVER_ERROR);
}
/**
* Fires after deleting a wishlist item.
*
* @important
* @param \Aivec\Welcart\Extensions\Wishlist\API\Wishlist $this Instance of the `Wishlist` API class
* @param array $args See \Aivec\Welcart\Extensions\Wishlist\API\Wishlist::delete()
* for details
* @param array $payload See \Aivec\Welcart\Extensions\Wishlist\API\Wishlist::delete()
* for details
*/
do_action('wcexwl_wishlist_api_delete_on_complete', $this, $args, $payload);
return CRUD::getAndBuildWishlistItems();
}
/**
* Deletes multiple wishlist items at once and returns the updated wishlist
*
* @important
* @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 $itemIds Array of wishlist item IDs to delete
* }
* @return array See \Aivec\Welcart\Extensions\Wishlist\API\CRUD::buildWishlistRow()
*/
public function batchDelete(array $args, array $payload) {
foreach ($payload['itemIds'] as $id) {
CRUD::executeDeletion($id);
}
/**
* Fires after batch deleting multiple wishlist items at once.
*
* @important
* @param \Aivec\Welcart\Extensions\Wishlist\API\Wishlist $this Instance of the `Wishlist` API class
* @param array $payload See \Aivec\Welcart\Extensions\Wishlist\API\Wishlist::batchDelete()
* for details
*/
do_action('wcexwl_wishlist_api_batch_delete_on_complete', $this, $payload);
return CRUD::getAndBuildWishlistItems();
}
/**
* Adds a wishlist item to the cart and returns the updated wishlist
*
* This API method will return an error if the item is out of stock or cannot
* be added to the cart for some other reason.
*
* @important
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @param array $args {
* URI parameters.
*
* @type int $itemId The wishlist item ID
* }
* @param array $payload The array parsed from `json_encoded` data stored in `$_POST['payload']`.
* @return array|GenericError See \Aivec\Welcart\Extensions\Wishlist\API\CRUD::buildWishlistRow()
*/
public function addToCart(array $args, array $payload) {
global $usces;
$echeck = $this->canAddToCart($args);
if ($echeck instanceof GenericError) {
return $echeck;
}
$id = (int)$args['itemId'];
$item = CRUD::getWishlistItemById($id);
$serial = $item['serial'];
if (!isset($_SESSION['usces_cart'][$serial])) {
$_SESSION['usces_cart'][$serial]['quant'] = (int)$item['quantity'];
$_SESSION['usces_cart'][$serial]['advance'] = (string)$item['advance'];
$unit_price = null;
// `get_realprice` uses `$this->serial` internally so we need to update it here
$usces->cart->serial = $serial;
$price = $usces->cart->get_realprice(
$item['postId'],
$item['sku'],
(int)$item['quantity'],
null,
$unit_price
);
$_SESSION['usces_cart'][$serial]['price'] = $price;
$_SESSION['usces_cart'][$serial]['unit_price'] = $unit_price;
if (!empty($item['advance'])) {
$_SESSION['usces_cart'][$serial]['advance'] = $usces->cart->wc_serialize((string)$item['advance']);
}
}
/**
* Fires after adding an item from the wishlist to the cart.
*
* @important
* @param \Aivec\Welcart\Extensions\Wishlist\API\Wishlist $this Instance of the `Wishlist` API class
* @param array $args See \Aivec\Welcart\Extensions\Wishlist\API\Wishlist::addToCart()
* for details
* @param array $payload See \Aivec\Welcart\Extensions\Wishlist\API\Wishlist::addToCart()
* for details
*/
do_action('wcexwl_wishlist_api_add_to_cart_on_complete', $this, $args, $payload);
return CRUD::getAndBuildWishlistItems();
}
/**
* Adds multiple wishlist items to the cart and returns the updated wishlist
*
* For simplicities sake, even if 9 out of 10 items can be added to the cart,
* as long as at least one item cannot be added, none of them will be. This method
* will check for errors for each item and return the first error it finds.
*
* @important
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @param array $args URI parameters. **Unused**
* @param array $payload {
* The array parsed from `json_encoded` data stored in `$_POST['payload']`.
*
* @type array $itemIds Array of wishlist item IDs to add to the cart
* }
* @return array See \Aivec\Welcart\Extensions\Wishlist\API\CRUD::buildWishlistRow()
*/
public function batchAddToCart(array $args, array $payload) {
global $usces;
$itemIds = !empty($payload['itemIds']) ? (array)$payload['itemIds'] : [];
// only check charge types if dlseller is active
if (defined('WCEX_DLSELLER')) {
foreach ($itemIds as $id) {
$post_id = CRUD::getPostIdByWishlistItemId($id);
if (!empty($post_id)) {
$chargetype = $usces->getItemChargingType($post_id);
$chargetype = !empty($chargetype) ? strtolower($chargetype) : '';
if ($chargetype === 'continue') {
$itemname = $usces->getItemName($post_id);
return $this->master->estore->getErrorResponse(
Store::CANNOT_PROCESS_IN_BATCH_OP,
[$itemname],
[$itemname]
);
}
}
}
}
$promptCartClear = null;
foreach ($itemIds as $id) {
$error = $this->canAddToCart(['itemId' => $id]);
if ($error instanceof GenericError) {
if ($error->errorcode === Store::CART_CONTAINS_CONTINUE_CHARGE) {
$message = __(
'Your cart contains a subscription item. You must clear your cart to continue. Clear cart now?',
'wcexwl'
);
$error->message = $message;
$error->debugmsg = $message;
$promptCartClear = $error;
} else {
return $error;
}
}
}
if ($promptCartClear !== null) {
return $promptCartClear;
}
foreach ($itemIds as $id) {
$res = $this->addToCart(['itemId' => $id], []);
if ($res instanceof GenericError) {
return $res;
}
}
/**
* Fires after batch adding multiple items from the wishlist to the cart.
*
* @important
* @param \Aivec\Welcart\Extensions\Wishlist\API\Wishlist $this Instance of the `Wishlist` API class
* @param array $payload See \Aivec\Welcart\Extensions\Wishlist\API\Wishlist::addToCart()
* for details
*/
do_action('wcexwl_wishlist_api_batch_add_to_cart_on_complete', $this, $payload);
return CRUD::getAndBuildWishlistItems();
}
}
- __construct — Sets up `Wishlist`
- addToCart — Adds a wishlist item to the cart and returns the updated wishlist
- batchAddToCart — Adds multiple wishlist items to the cart and returns the updated wishlist
- batchDelete — Deletes multiple wishlist items at once and returns the updated wishlist
- canAddToCart — Returns a `GenericError` if the given wishlist item cannot be added to cart for some reason, `void` otherwise
- delete — Deletes a wishlist item by ID and returns the updated wishlist
- maybeClearCart — Clears cart if `clearCart` is set in `$payload`