クラス
Checkout
ソース ソース
ファイル: src/API/Checkout.php
class Checkout { /** * Registers hooks * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return void */ public static function init() { // set priority to 20 so inventory is updated last add_action('usces_action_reg_orderdata', [get_class(), 'updateInventoryOnPurchase'], 20, 1); add_action('usces_action_reg_ordercart_row', [get_class(), 'addGroupItemsToOrderCart'], 10, 4); } /** * Updates inventory on purchase for each combo-set group item * * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \usc_e_shop $usces * @param array $args * @return void */ public static function updateInventoryOnPurchase($args) { global $usces; $items = $args['cart']; foreach ($items as $index => $item) { $sels = unserialize($item['serial']); // combo-set ID not set, continue if (empty($sels['comboSetId'])) { continue; } // combo-set group items not set, continue if (!isset($sels['comboSetItems'])) { continue; } // make each group item quantity the same as the combo-set item quant foreach ($sels['comboSetItems'] as $serial => $gitem) { $sels['comboSetItems'][$serial]['quant'] = (int)$gitem['quant'] * $item['quantity']; } $realcart = $_SESSION['usces_cart']; $_SESSION['usces_cart'] = $sels['comboSetItems']; // loop combo-set group items and update inventory for each one usces_action_reg_orderdata_stocks(['cart' => $usces->cart->get_cart()]); $_SESSION['usces_cart'] = $realcart; } } /** * Registers group items in group items ordercart table if the cart item is a combo-set * * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \usc_e_shop $usces * @param int $cart_id * @param int $row_index * @param array $value * @param array $args * @return void */ public static function addGroupItemsToOrderCart($cart_id, $row_index, $value, $args) { global $usces; $sels = unserialize($value['serial']); // combo-set ID not set, continue if (empty($sels['comboSetId'])) { return; } // combo-set group items not set, continue if (!isset($sels['comboSetItems'])) { return; } $realcart = $_SESSION['usces_cart']; $_SESSION['usces_cart'] = $sels['comboSetItems']; foreach ($usces->cart->get_cart() as $index => $gitem) { $gisels = unserialize($gitem['serial']); $group = ComboGroup::getComboGroupById($gisels['groupId']); $item = GroupItem::getGroupItemById($gisels['itemId']); $groupLabel = ''; $itemLabel = ''; if ($group !== null) { $groupLabel = $group->getLabel(); } if ($item !== null) { $itemLabel = $item->getItemLabel(); } $gisels['groupLabel'] = $groupLabel; $gisels['itemLabel'] = $itemLabel; $gitem['serial'] = serialize($gisels); self::registerOrderCartDataForGroupItem($cart_id, (int)$args['order_id'], $gitem, $index); } $_SESSION['usces_cart'] = $realcart; } /** * Registers group item data in group items ordercart table and ordercart meta table * * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \wpdb $wpdb * @global \usc_e_shop $usces * @param int $combo_set_cart_id * @param int $order_id * @param array $value This is the group item cart * @param int $row_index * @return void */ public static function registerOrderCartDataForGroupItem($combo_set_cart_id, $order_id, $value, $row_index) { global $wpdb, $usces; $cart_table = Schema::getComboSetGroupItemsOrderCartTable(); $cart_meta_table = Schema::getComboSetGroupItemsOrderCartMetaTable(); $post_id = (int)$value['post_id']; $product = wel_get_product($post_id, false); $item_code = $product['itemCode']; $item_name = $product['itemName']; $skus = $usces->get_skus($value['post_id'], 'code', false); $sku_encoded = $value['sku']; $skucode = urldecode($value['sku']); $sku = $skus[$skucode]; $tax = 0; $query = $wpdb->prepare( "INSERT INTO $cart_table ( combo_set_cart_id, order_id, row_index, post_id, item_code, item_name, sku_code, sku_name, cprice, price, quantity, unit, tax, destination_id, cart_serial ) VALUES ( %d, %d, %d, %d, %s, %s, %s, %s, %f, %f, %f, %s, %d, %d, %s )", $combo_set_cart_id, $order_id, $row_index, $post_id, $item_code, $item_name, $skucode, $sku['name'], $sku['cprice'], $value['price'], $value['quantity'], $sku['unit'], $tax, null, $value['serial'] ); $wpdb->query($query); $cart_id = $wpdb->insert_id; $opt_fields = wel_get_opts($post_id, 'sort', false); if ($value['options']) { foreach ((array)$opt_fields as $okey => $val) { $enc_key = urlencode($val['name']); $means = $opt_fields[$okey]['means']; if (3 === (int)$means) { if ('' == $value['options'][$enc_key]) { $ovalue = $value['options'][$enc_key]; } else { $ovalue = urldecode($value['options'][$enc_key]); } } elseif (4 === (int)$means) { if (is_array($value['options'][$enc_key])) { $temp = []; foreach ($value['options'][$enc_key] as $v) { $temp[] = urldecode($v); } $ovalue = serialize($temp); } elseif ('' == $value['options'][$enc_key]) { $ovalue = $value['options'][$enc_key]; } else { $ovalue = urldecode($value['options'][$enc_key]); } } else { if (is_array($value['options'][$enc_key])) { $temp = []; foreach ($value['options'][$enc_key] as $k => $v) { $temp[$k] = urldecode($v); } $ovalue = serialize($temp); } else { $ovalue = urldecode($value['options'][$enc_key]); } } $wpdb->query( $wpdb->prepare( "INSERT INTO {$cart_meta_table} ( cart_id, meta_type, meta_key, meta_value ) VALUES (%d, %s, %s, %s)", $cart_id, 'option', $val['name'], $ovalue ) ); } } if ($value['advance']) { foreach ((array)$value['advance'] as $akey => $avalue) { $advance = $usces->cart->wc_unserialize($avalue); if (is_array($advance)) { if (isset($advance[$post_id][$sku_encoded]) && is_array($advance[$post_id][$sku_encoded])) { $akeys = array_keys($advance[$post_id][$sku_encoded]); foreach ((array)$akeys as $akey) { if (is_array($advance[$post_id][$sku_encoded][$akey])) { $avalue = serialize($advance[$post_id][$sku_encoded][$akey]); } else { $avalue = $advance[$post_id][$sku_encoded][$akey]; } $wpdb->query( $wpdb->prepare( "INSERT INTO {$cart_meta_table} ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, 'advance', %s, %s )", $cart_id, $akey, $avalue ) ); } } else { $akeys = array_keys($advance); $akey = empty($akeys[0]) ? 'advance' : $akeys[0]; $avalue = serialize($advance); $wpdb->query( $wpdb->prepare( "INSERT INTO {$cart_meta_table} ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, 'advance', %s, %s )", $cart_id, $akey, $avalue ) ); } } else { $avalue = urldecode($avalue); $wpdb->query( $wpdb->prepare( "INSERT INTO {$cart_meta_table} ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, 'advance', %s, %s )", $cart_id, $akey, $avalue ) ); } } } if ($usces->is_reduced_taxrate()) { if (isset($sku['taxrate']) && 'reduced' == $sku['taxrate']) { $tkey = 'reduced'; $tvalue = $usces->options['tax_rate_reduced']; } else { $tkey = 'standard'; $tvalue = $usces->options['tax_rate']; } $wpdb->query( $wpdb->prepare( "INSERT INTO {$cart_meta_table} ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, 'taxrate', %s, %s )", $cart_id, $tkey, $tvalue ) ); } } }
- addGroupItemsToOrderCart — Registers group items in group items ordercart table if the cart item is a combo-set
- init — Registers hooks
- registerOrderCartDataForGroupItem — Registers group item data in group items ordercart table and ordercart meta table
- updateInventoryOnPurchase — Updates inventory on purchase for each combo-set group item