クラス
Wishlist
ソース ソース
ファイル: src/Wishlist.php
class Wishlist
{
/**
* Registers hooks
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public static function init() {
add_action('wcexwl_wishlist_api_add_to_cart_on_complete', [get_class(), 'addLabelsOnAddToCart'], 10, 2);
add_filter(
'wcexwl_filter_item_add_to_wishlist_custom_validation',
[get_class(), 'validateComboSetOnAddToWishlist'],
10,
2
);
add_filter(
'wcexwl_filter_item_add_to_wishlist_serial',
[get_class(), 'filterItemAddToWishlistSerial'],
10,
2
);
add_filter('wcexwl_filter_wlpage_cart_item_name', [get_class(), 'filterWishlistCartItemName'], 10, 2);
add_action('wcexwl_wishlist_page_on_load_assets', function () {
Components\Cart\Cart::loadCss();
});
}
/**
* Adds group and group item labels to the combo-set cart item when added from the wishlist page
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param \Aivec\Welcart\Extensions\Wishlist\API\Wishlist $instance
* @param array $args
* @return void
*/
public static function addLabelsOnAddToCart($instance, $args) {
$id = (int)$args['itemId'];
$item = \Aivec\Welcart\Extensions\Wishlist\API\CRUD::getWishlistItemById($id);
$serial = $item['serial'];
$maps = API\Cart::getGroupAndItemLabelsFromSerial($serial);
if (empty($maps)) {
return;
}
$_SESSION['usces_cart'][$serial]['grouplabelmap'] = $maps['grouplabelmap'];
$_SESSION['usces_cart'][$serial]['itemlabelmap'] = $maps['itemlabelmap'];
}
/**
* Performs validation for a combo-set being added to the wishlist from the item page
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param null $null
* @param array $args
* @return null|GenericError
*/
public static function validateComboSetOnAddToWishlist($null, $args) {
// we need to make sure the item is a combo-set item
$comboset = API\ComboSet::getComboSetFromSkuCode($args['post_id'], urldecode($args['sku']));
if ($comboset instanceof GenericError) {
// error fetching automatically sets the response code (bad design...)
http_response_code(200);
return $null;
}
$pvals = API\Cart::getAddToCartPostValues($comboset->getId());
$groupids = $pvals['groupids'];
// check that all group IDs and item IDs exist. Check that required groups have selections
$error = API\PurchaseSanityChecks::comboSetSelectionsAreValid($comboset->getId(), $groupids);
if ($error instanceof GenericError) {
return $error;
}
return $null;
}
/**
* Filters serialization of a combo-set when adding to wishlist from the item page
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $serial
* @param array $args
* @return string
*/
public static function filterItemAddToWishlistSerial($serial, $args) {
// we need to make sure the item is a combo-set item
$comboset = API\ComboSet::getComboSetFromSkuCode($args['post_id'], urldecode($args['sku']));
if ($comboset instanceof GenericError) {
// error fetching automatically sets the response code (bad design...)
http_response_code(200);
return $serial;
}
$pvals = API\Cart::getAddToCartPostValues($comboset->getId());
$groupids = $pvals['groupids'];
$itemoptmap = $pvals['itemoptmap'];
// get post data for constructing a cart out of the selected group items
$postdata = API\Cart::getInCartPostData($comboset, $groupids, $itemoptmap);
// Now we need to construct a cart out of all the group items
$cartapi = new API\Cart();
$groupitemscart = $cartapi->getGroupItemsCart($postdata)['rawCart'];
// set $this->payload for use in processing the combo-set item
$cartapi->payload = [];
$cartapi->payload['comboSetId'] = $comboset->getId();
$cartapi->payload['groupItemsCart'] = $groupitemscart;
$tempcart = new \usces_cart();
if (!empty($args['item_option'])) {
// we are forced to set $_POST at runtime here since `in_serialize` uses $_POST internally
$_POST['itemOption'] = $args['item_option'];
}
add_filter('usces_filter_in_serialize', [$cartapi, 'filterComboSetInSerialize'], 10);
$tempcart->in_serialize($args['post_id'], $args['sku']);
unset($_POST['itemOption']);
remove_filter('usces_filter_in_serialize', [$cartapi, 'filterComboSetInSerialize']);
return $tempcart->serial;
}
/**
* Adds combo-set details below the cart item name on the wishlist page
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @param string $html
* @param array $row
* @return string
*/
public static function filterWishlistCartItemName($html, $row) {
global $usces;
$sels = unserialize($row['serial']);
if (empty($sels['comboSetId']) || !isset($sels['comboSetItems'])) {
return $html;
}
$rows = [];
foreach ($sels['comboSetItems'] as $serial => $gitem) {
$gisels = unserialize($serial);
$gicsels = (new \usces_cart())->key_unserialize($serial);
$glabel = '';
$group = API\ComboGroup::getComboGroupById($gisels['groupId']);
if ($group instanceof Types\ComboGroup) {
$glabel = $group->getLabel();
}
$itemLabel = '';
$itemtype = API\GroupItem::getGroupItemById($gisels['itemId']);
if ($itemtype instanceof Types\GroupItem) {
$itemLabel = $itemtype->getItemLabel();
}
if (empty($itemLabel)) {
$itemLabel = wcexics_get_group_item_default_label(
$usces->getItemName($gicsels['post_id']),
$usces->getItemSkuDisp($gicsels['post_id'], urldecode($gicsels['sku'])),
$gitem['quant'],
$gitem['price']
);
}
$rows[$gisels['groupId']]['groupLabel'] = $glabel;
$rows[$gisels['groupId']]['groupItems'][] = [
'name' => $itemLabel,
'options' => !empty($gicsels['options']) ? $gicsels['options'] : [],
];
}
return $html . Components\Cart\Cart::getComboSetCartDetailsHtml($rows);
}
}
- addLabelsOnAddToCart — Adds group and group item labels to the combo-set cart item when added from the wishlist page
- filterItemAddToWishlistSerial — Filters serialization of a combo-set when adding to wishlist from the item page
- filterWishlistCartItemName — Adds combo-set details below the cart item name on the wishlist page
- init — Registers hooks
- validateComboSetOnAddToWishlist — Performs validation for a combo-set being added to the wishlist from the item page