関数
Wishlist::batchAddToCart( array $args, array $payload )
複数のお気に入りリストアイテムをカートに追加し、更新したお気に入りリストを返す
説明 説明
便宜上、ほとんどのアイテムはカートに入れることができるとしても1つでも追加できないアイテムがあれば、すべてカートに入れることができない仕様になっている。それぞれのアイテムに関してエラーを確認し、見つかった場合まずエラーを返す。
- $args
(配列) (必須) URIパラメータ
- $payload
(配列) (必須)
$_POST['payload']に格納されたjson_encodedデータから抽出された配列- 'itemIds'
(配列)カートに追加するお気に入りリストアイテムのIDの配列
- 'itemIds'
ファイル: src/API/Wishlist.php
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();
}