Plugin Directory

Changeset 3464694


Ignore:
Timestamp:
02/19/2026 12:57:28 AM (7 weeks ago)
Author:
codemstory
Message:

3.6.4

네이버페이 콜백 처리 시 PHP Warning 메시지 제거

Location:
mshop-npay/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • mshop-npay/trunk/includes/class-mnp-cart.php

    r3323199 r3464694  
    33
    44
    5 if ( !class_exists( 'MNP_Cart' ) ) {
    6     class MNP_Cart
    7     {
    8 
    9         static $shippingPolicy = null;
    10 
    11         static function get_order_key()
    12         {
    13             $data = array(
    14                 MNP_Manager::merchant_id(),
    15                 date( 'Y-m-d H:i:s' ),
    16                 strtoupper( bin2hex( openssl_random_pseudo_bytes( 20 ) ) )
    17             );
    18 
    19             return date( 'YmdHis' ) . '_' . strtoupper( md5( json_encode( $data ) ) );
    20         }
    21 
    22         static function cart_contains_npay_items()
    23         {
    24             $support_product_types = apply_filters( 'mnp_support_product_types', array( 'variable', 'variation' ) );
    25 
    26             if ( WC()->cart ) {
    27                 foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
    28 
    29                     if ( $values['variation_id'] ) {
    30                         $product_id = $values['variation_id'];
    31                         $variation = $values['variation'];
    32                     } else {
    33                         $product_id = $values['product_id'];
    34                         $variation = null;
    35                     }
    36 
    37                     $wc_product = wc_get_product( $product_id );
    38 
    39                     if ( MNP_Manager::is_purchasable( $values['product_id'] ) && MNP_Manager::is_purchasable( $product_id ) && $wc_product->is_in_stock() && $wc_product->has_enough_stock( $values['quantity'] ) && $wc_product->is_purchasable() && !$wc_product->is_virtual() &&
    40                         ( in_array( $wc_product->get_type(), $support_product_types ) || ( $wc_product->is_type( 'simple' ) && $wc_product->get_price() > 0 ) )
    41                     ) {
    42                         return true;
    43                     }
    44                 }
    45             }
    46 
    47             return false;
    48         }
    49         static function generate_product_info( $args )
    50         {
    51             $product_id = apply_filters( 'mnp_get_product_id_from_cart_item_key', $args['product_id'], $args['cart_item_key'], $args['cart_item'] );
    52             $merchant_product_id = $args['product_id'];
    53             $quantity = $args['quantity'];
    54             $variation = $args['variations'];
    55             $line_total = $args['line_total'];
    56             $line_tax = $args['line_tax'];
    57 
    58             $wc_product = wc_get_product( $merchant_product_id );
    59             $tax = 'TAX';
    60             if ( wc_tax_enabled() ) {
    61                 if ( $line_tax <= 0 ) {
    62                     $tax = 'ZERO_TAX';
    63                 }
    64 
    65                 $line_total = absint( round( $line_total + $line_tax, wc_get_price_decimals() ) );
    66             }
    67 
    68             $unit_price = $line_total / $quantity;
    69             if ( $wc_product->is_type( 'simple' ) ) {
    70                 // 단순상품 정보를 생성한다.
    71                 $single = null;
    72                 $option = apply_filters( 'mnp_generate_product_option_simple', null, $args );
    73 
    74                 if ( empty( $option ) ) {
    75                     $single = new ProductSingle( $quantity );
    76                     $option = null;
    77                 }
    78             } else if ( $wc_product->is_type( 'variation' ) ) {
    79                 // 옵션상품 정보를 생성한다.
    80                 $single = null;
    81                 $selectedItems = array();
    82 
    83                 if ( !empty( $variation ) ) {
    84                     $attributes = $variation;
    85                 } else {
    86                     $attributes = $wc_product->get_variation_attributes();
    87                 }
    88 
    89                 foreach ( $attributes as $key => $value ) {
    90                     $option_id = str_replace( 'attribute_', '', $key );
    91                     $option_name = html_entity_decode( wc_attribute_label( $option_id ) );
    92                     $term = get_term_by( 'slug', $value, $option_id );
    93                     $option_text = html_entity_decode( $term->name );
    94 
    95                     $selectedItems[] = new ProductOptionSelectedItem( ProductOptionSelectedItem::TYPE_SELECT, $option_name, $term->slug, $option_text );
    96                 }
    97 
    98                 $selectedItems = apply_filters( 'mnp_generate_product_option_variable', $selectedItems, $args );
    99 
    100                 $option = new ProductOption( $quantity, 0, null, $selectedItems );
    101             } else {
    102                 $single = apply_filters( 'mnp_generate_product_info_single', null, $args );
    103                 $option = apply_filters( 'mnp_generate_product_option_simple', null, $args );
    104             }
    105 
    106             $img_url = '';
    107             $images = wp_get_attachment_image_src( $wc_product->get_image_id(), array( 300, 300 ) );
    108 
    109             if ( !empty( $images ) ) {
    110                 $img_url = $images[0];
    111                 if ( empty( $img_url ) && !empty( $args['parent_product_id'] ) ) {
    112                     $parent_product = wc_get_product( $args['parent_product_id'] );
    113                     $images = wp_get_attachment_image_src( $parent_product->get_image_id(), array( 300, 300 ) );
    114                     if ( !empty( $images ) ) {
    115                         $img_url = $images[0];
    116                     }
    117                 }
    118                 if ( 'yes' == get_option( 'mnp-force-image-url-to-http', 'yes' ) ) {
    119                     $img_url = preg_replace( "/^https:/i", "http:", $img_url );
    120                 }
    121             }
    122 
    123             $img_url = apply_filters( 'mnp_product_image_url', $img_url, $product_id );
    124 
    125             if ( empty( $img_url ) ) {
    126                 wp_send_json_error( array( 'message' => '상품 이미지가 없습니다.' ) );
    127             }
    128             $product_id = apply_filters( 'mnp_product_id', $product_id, $args );
    129             $merchant_product_id = apply_filters( 'mnp_merchant_product_id', $merchant_product_id, $args );
    130 
    131             $supplements = apply_filters( 'mnp_supplements', array(), $args );
    132 
    133             $gifts = apply_filters( 'mnp_get_gifts_from_cart_item', array(), $args['cart_item'] );
    134             return new Product(
    135                 $product_id, /** 상품 번호 */
    136                 $merchant_product_id, /** 가맹점 상품 번호 */
    137                 apply_filters( 'mnks_ecmall_product_id', null, $product_id ), /** 지식쇼핑 EP의 Mall_pid */
    138                 html_entity_decode( $wc_product->get_title() ), /** 상품명 */
    139                 $unit_price, /** 상품가격 */
    140                 $tax, /** 세금종류 */
    141                 $wc_product->get_permalink(), /** 상품 URL */
    142                 $img_url, /** 상품 Thumbnail URL */
    143                 implode( ',', $gifts ), /** giftName */
    144                 $single, /** 단순 상품 정보 */
    145                 $option, /** 옵션 상품 정보 */
    146                 self::$shippingPolicy/** 배송 정책 */,
    147                 $supplements
    148             );
    149         }
    150         public static function checkout_cart()
    151         {
    152             MNP_Logger::add_log( 'checkout_cart' );
    153 
    154             do_action( 'mnp_before_checkout_cart' );
    155 
    156             $selected_cart_items = array_filter( explode( ',', mnp_get( $_POST, 'msbn_keys', '' ) ) );
    157             add_filter( 'mshop_membership_skip_filter', '__return_true' );
    158             mnp_maybe_define_constant( 'WOOCOMMERCE_CART', true );
    159 
    160 
    161             $support_product_types = apply_filters( 'mnp_support_product_types', array( 'variable', 'variation' ) );
    162 
    163             include_once( 'naverpay/Order.php' );
    164 
    165             self::$shippingPolicy = MNP_Shipping::get_shipping_policy( WC()->cart );
    166             wc_clear_notices();
    167 
    168             if ( !WC()->cart->check_cart_items() ) {
    169                 $msg = implode( ', ', wc_get_notices( 'error' ) );
    170                 wp_send_json_error( array( 'message' => htmlspecialchars_decode( strip_tags( $msg ) ) ) );
    171             }
    172 
    173             WC()->cart->calculate_totals();
    174 
    175             $products = array();
    176             $cart_contents = apply_filters( 'mnp_checkout_cart_get_cart_contents', WC()->cart->get_cart() );
    177 
    178             foreach ( $cart_contents as $cart_item_key => $values ) {
    179 
    180                 if ( !empty( $selected_cart_items ) && !in_array( $cart_item_key, $selected_cart_items ) ) {
    181                     continue;
    182                 }
    183 
    184                 if ( $values['variation_id'] ) {
    185                     $product_id = $values['variation_id'];
    186                     $variation = $values['variation'];
    187                 } else {
    188                     $product_id = $values['product_id'];
    189                     $variation = null;
    190                 }
    191 
    192                 $wc_product = wc_get_product( $product_id );
    193 
    194                 if ( MNP_Manager::is_purchasable( $values['product_id'] ) && MNP_Manager::is_purchasable( $product_id ) && $wc_product->is_in_stock() && $wc_product->has_enough_stock( $values['quantity'] ) && $wc_product->is_purchasable() && !$wc_product->is_virtual() &&
    195                     ( in_array( $wc_product->get_type(), $support_product_types ) || ( $wc_product->is_type( 'simple' ) && $wc_product->get_price() > 0 ) )
    196                 ) {
    197                     $products[] = self::generate_product_info( array(
    198                         'product_id' => $product_id,
    199                         'quantity' => $values['quantity'],
    200                         'variations' => $variation,
    201                         'line_total' => $values['line_total'],
    202                         'line_tax' => $values['line_tax'],
    203                         'cart_item_data' => apply_filters( 'mnp_get_product_cart_item_data', array(), $values ),
    204                         'cart_item' => $values,
    205                         'cart_item_key' => $cart_item_key
    206                     ) );
    207                 }
    208             }
    209             if ( 0 == count( $products ) ) {
    210                 wp_send_json_error( array( 'message' => '네이버페이로 구매가능한 상품이 없습니다.' ) );
    211             }
    212 
    213             $npay_order_key = self::get_order_key();
    214             self::save_cart_contents( $npay_order_key, WC()->cart );
    215             $custom_data = apply_filters( 'mnp_custom_order_data', array( 'order_key' => $npay_order_key ) );
    216             $order = new Order( $products, self::get_back_url(), $custom_data );
    217             $data = MNP_XMLSerializer::generateValidXmlFromObj( json_decode( json_encode( $order ) ), 'order' );
    218 
    219             MNP_Logger::add_log( print_r( $order, true ) );
    220 
    221             $result = MNP_API::register_order( $data );
    222             $response = $result->response;
    223 
    224             do_action( 'mnp_after_checkout_cart' );
    225 
    226             if ( $response->ResponseType == "SUCCESS" ) {
    227                 wp_send_json_success( array( 'authkey' => $response->AuthKey, 'shopcode' => $response->ShopCode ) );
    228             } else {
    229                 wp_send_json_error( array( 'message' => $response->Error->Message ) );
    230             }
    231         }
    232 
    233         protected static function get_options( $variation_id, $variations )
    234         {
    235             $wc_product = wc_get_product( $variation_id );
    236 
    237             if ( is_product( $wc_product ) && $wc_product->is_type( 'variation' ) ) {
    238                 // 옵션상품 정보를 생성한다.
    239                 $single = null;
    240                 $selectedOptions = array();
    241 
    242                 if ( !empty( $variation ) ) {
    243                     $attributes = $variation;
    244                 } else {
    245                     $attributes = $wc_product->get_variation_attributes();
    246                 }
    247 
    248                 foreach ( $attributes as $key => $value ) {
    249                     $option_id = str_replace( 'attribute_', '', $key );
    250                     $option_name = html_entity_decode( wc_attribute_label( $option_id ) );
    251                     $term = get_term_by( 'slug', $value, $option_id );
    252                     $option_text = html_entity_decode( $term->name );
    253 
    254                     $selectedOptions[] = $option_name . ' : ' . $option_text;
    255                 }
    256 
    257                 return ' ( ' . implode( ', ', $selectedOptions ) . ' )';
    258             }
    259         }
    260         protected static function get_back_url()
    261         {
    262             $back_url = remove_query_arg( 'NaPm', $_SERVER['HTTP_REFERER'] );
    263 
    264             if ( apply_filters( 'mnp_remove_query_args_from_back_url', false ) ) {
    265                 $urls = parse_url( $back_url );
    266                 $back_url = sprintf( "%s://%s%s", $urls['scheme'], $urls['host'], $urls['path'] );
    267             }
    268 
    269             return $back_url;
    270         }
    271         public static function create_order()
    272         {
    273             MNP_Logger::add_log( 'create_order' );
    274 
    275             do_action( 'mnp_before_create_order' );
    276 
    277             include_once( 'naverpay/Order.php' );
    278             add_filter( 'mshop_membership_skip_filter', '__return_true' );
    279             mnp_maybe_define_constant( 'WOOCOMMERCE_CART', true );
    280 
    281             wc_clear_notices();
    282             self::backup_cart();
    283             $_POST_ORG = wc_clean( $_POST );
    284 
    285             $products = wc_clean( $_REQUEST['products'] );
    286 
    287             foreach ( $products as $product_info ) {
    288                 $params = array();
    289                 parse_str( wc_clean( $product_info['form_data'] ), $params );
    290 
    291                 $_POST = array_merge( wc_clean( $_POST ), wc_clean( $params ) );
    292 
    293                 $product_id = !empty( $product_info['parent_product_id'] ) ? $product_info['parent_product_id'] : $product_info['product_id'];
    294                 $variation_id = !empty( $product_info['parent_product_id'] ) ? $product_info['product_id'] : 0;
    295                 $variations = apply_filters( 'mnp_get_product_variations', $product_info['attributes'], $product_info );
    296                 $cart_item_data = apply_filters( 'mnp_get_product_cart_item_data', array(), $product_info );
    297                 WC()->cart->add_to_cart( $product_id, $product_info['quantity'], $variation_id, $variations, $cart_item_data );
    298 
    299                 if ( wc_notice_count( 'error' ) > 0 ) {
    300                     self::recover_cart();
    301 
    302                     $notices = wc_get_notices( 'error' );
    303                     wc_clear_notices();
    304 
    305                     $options = self::get_options( $variation_id, $variations );
    306 
    307                     $notices = wp_list_pluck( $notices, 'notice' );
    308 
    309                     wp_send_json_error( array( 'message' => htmlspecialchars_decode( strip_tags( implode( "\n", $notices ) ) ) . $options ) );
    310                 }
    311 
    312                 $_POST = $_POST_ORG;
    313             }
    314             WC()->cart->calculate_totals();
    315 
    316             do_action( 'woocommerce_check_cart_items' );
    317 
    318             if ( wc_notice_count( 'error' ) > 0 ) {
    319                 self::recover_cart();
    320 
    321                 $notices = wc_get_notices( 'error' );
    322                 wc_clear_notices();
    323 
    324                 wp_send_json_error( array( 'message' => htmlspecialchars_decode( strip_tags( implode( "\n", $notices ) ) ) ) );
    325             }
    326             self::$shippingPolicy = MNP_Shipping::get_shipping_policy( WC()->cart );
    327             $products = array();
    328             $cart_contents = apply_filters( 'mnp_create_order_get_cart_contents', WC()->cart->get_cart() );
    329 
    330             foreach ( $cart_contents as $cart_item_key => $values ) {
    331                 if ( !empty( $values['variation_id'] ) ) {
    332                     $product_id = $values['variation_id'];
    333                     $variation = $values['variation'];
    334                 } else {
    335                     $product_id = $values['product_id'];
    336                     $variation = null;
    337                 }
    338 
    339                 $products[] = self::generate_product_info( array(
    340                     'product_id' => $product_id,
    341                     'quantity' => $values['quantity'],
    342                     'variations' => $variation,
    343                     'line_total' => $values['line_total'],
    344                     'line_tax' => $values['line_tax'],
    345                     'cart_item_data' => apply_filters( 'mnp_get_product_cart_item_data', array(), $values ),
    346                     'cart_item' => $values,
    347                     'cart_item_key' => $cart_item_key
    348                 ) );
    349             }
    350             $npay_order_key = self::get_order_key();
    351             self::save_cart_contents( $npay_order_key, WC()->cart );
    352             self::recover_cart();
    353             $custom_data = apply_filters( 'mnp_custom_order_data', array( 'order_key' => $npay_order_key ) );
    354             $order = new Order( $products, self::get_back_url(), $custom_data );
    355             $data = MNP_XMLSerializer::generateValidXmlFromObj( json_decode( json_encode( $order ) ), 'order' );
    356 
    357             MNP_Logger::add_log( print_r( $order, true ) );
    358 
    359             $result = MNP_API::register_order( $data );
    360             $response = $result->response;
    361 
    362             do_action( 'mnp_after_create_order' );
    363 
    364             if ( $response->ResponseType == "SUCCESS" ) {
    365                 wp_send_json_success( array( 'authkey' => $response->AuthKey, 'shopcode' => $response->ShopCode ) );
    366             } else {
    367                 wp_send_json_error( array( 'message' => $response->Error->Message ) );
    368             }
    369         }
    370         public static function add_to_wishlist()
    371         {
    372             global $wishlistItemId;
    373 
    374             $queryString = 'SHOP_ID=' . urlencode( MNP_Manager::merchant_id() );
    375             $queryString .= '&CERTI_KEY=' . urlencode( MNP_Manager::auth_key() );
    376 
    377             foreach ( $_REQUEST['products'] as $product_info ) {
    378                 $wc_product = wc_get_product( $product_info['product_id'] );
    379 
    380                 $img_url = wp_get_attachment_image_src( $wc_product->get_image_id(), array( 300, 300 ) )[0];
    381                 if ( 'yes' == get_option( 'mnp-force-image-url-to-http', 'yes' ) ) {
    382                     $img_url = preg_replace( "/^https:/i", "http:", $img_url );
    383                 }
    384 
    385                 $queryString .= '&ITEM_ID=' . urlencode( $product_info['product_id'] );
    386                 $queryString .= '&ITEM_NAME=' . urlencode( $wc_product->get_title() );
    387                 $queryString .= '&ITEM_DESC=' . urlencode( $wc_product->get_title() );
    388                 $queryString .= '&ITEM_UPRICE=' . $wc_product->get_price();
    389                 $queryString .= '&ITEM_IMAGE=' . urlencode( utf8_uri_encode( $img_url ) );
    390                 $queryString .= '&ITEM_THUMB=' . urlencode( utf8_uri_encode( $img_url ) );
    391                 $queryString .= '&ITEM_URL=' . urlencode( $wc_product->get_permalink() );
    392 
    393                 break;
    394             }
    395 
    396             $response = wp_remote_post( MNP_Manager::wishlist_url(), array(
    397                     'method' => 'POST',
    398                     'headers' => array( 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8;' ),
    399                     'timeout' => 45,
    400                     'redirection' => 5,
    401                     'httpversion' => '1.1',
    402                     'blocking' => true,
    403                     'body' => $queryString,
    404                     'cookies' => array()
    405                 )
    406             );
    407 
    408             if ( is_wp_error( $response ) ) {
    409                 wp_send_json_error( array( 'message' => $response->get_error_message() ) );
    410             } else {
    411                 ob_start();
    412                 wc_get_template( 'wishlist-popup' . ( wp_is_mobile() ? '-mobile' : '' ) . '.php', array( 'wishlistItemIds' => explode( ',', $response['body'] ) ), '', MNP()->template_path() );
    413                 $html = ob_get_clean();
    414 
    415                 wp_send_json_success( array(
    416                     'url' => MNP_Manager::wishlist_url(),
    417                     'html' => $html
    418                 ) );
    419             }
    420         }
    421         public static function woocommerce_after_cart_table()
    422         {
    423             if ( !apply_filters( 'mnp_enabled', true ) && !empty( $_GET['s'] ) ) {
    424                 return;
    425             }
    426 
    427             if ( MNP_Manager::is_operable() && self::cart_contains_npay_items() ) {
    428                 $dependencies = array(
    429                     'jquery',
    430                     'jquery-ui-core',
    431                     'jquery-ui-widget',
    432                     'jquery-ui-mouse',
    433                     'jquery-ui-position',
    434                     'jquery-ui-draggable',
    435                     'jquery-ui-resizable',
    436                     'jquery-ui-button',
    437                     'jquery-ui-dialog',
    438                     'underscore'
    439                 );
    440 
    441                 if ( 'yes' == get_option( 'mnp-npay-script', 'no' ) ) {
    442                     wp_enqueue_script( 'mnp-naverpay', MNP_Manager::button_js_url( wp_is_mobile() ? 'mobile' : 'pc' ), array( 'jquery' ), MNP_VERSION );
    443                     $dependencies[] = 'mnp-naverpay';
    444                 }
    445 
    446                 wp_register_script( 'mnp-frontend', MNP()->plugin_url() . '/assets/js/cart.js', $dependencies, MNP_VERSION );
    447                 wp_localize_script( 'mnp-frontend', '_mnp', array(
    448                     'ajax_url' => mnp_ajax_url( admin_url( 'admin-ajax.php', 'relative' ) ),
    449                     'order_url_pc' => MNP_Manager::ordersheet_url( 'pc' ),
    450                     'order_url_mobile' => MNP_Manager::ordersheet_url( 'mobile' ),
    451                     'button_js_url_pc' => MNP_Manager::button_js_url( 'pc' ),
    452                     'button_js_url_mobile' => MNP_Manager::button_js_url( 'mobile' ),
    453                     'wishlist_url' => MNP_Manager::wishlist_url(),
    454                     'button_key' => MNP_Manager::button_auth_key(),
    455                     'button_type_pc' => MNP_Manager::button_type_pc(),
    456                     'button_type_mobile' => MNP_Manager::button_type_mobile(),
    457                     'button_color' => MNP_Manager::button_color(),
    458                     'checkout_cart_action' => MNP()->slug() . '-checkout_cart',
    459                     'transition_mode' => get_option( 'mnp-cart-page-transition-mode', 'new-window' ),
    460                     'load_script_static' => get_option( 'mnp-npay-script', 'no' )
    461                 ) );
    462                 wp_enqueue_script( 'underscore' );
    463                 wp_enqueue_script( 'mnp-frontend' );
    464                 wp_enqueue_script( 'jquery-block-ui', MNP()->plugin_url() . '/assets/js/jquery.blockUI.js', $dependencies );
    465 
    466                 wp_register_style( 'mnp-frontend', MNP()->plugin_url() . '/assets/css/naverpay-cart.css' );
    467                 wp_enqueue_style( 'mnp-frontend' );
    468 
    469                 wc_get_template( 'cart/naverpay-button.php', array(), '', MNP()->template_path() );
    470             }
    471         }
    472         public static function woocommerce_after_add_to_cart_form()
    473         {
    474             if ( !apply_filters( 'mnp_enabled', true ) ) {
    475                 return;
    476             }
    477 
    478             if ( !empty( $_REQUEST['elementor-preview'] ) || 'elementor' == mnp_get( $_GET, 'action' ) ) {
    479                 return '';
    480             }
    481 
    482             $support_product_types = apply_filters( 'mnp_support_product_types', array( 'variable', 'grouped' ) );
    483 
    484             $product_id = get_the_ID();
    485 
    486             if ( MNP_Manager::is_operable() && MNP_Manager::is_purchasable( $product_id ) ) {
    487                 $product = wc_get_product( $product_id );
    488 
    489                 $purchasable = 'grouped' == $product->get_type() ? true : $product->is_purchasable();
    490 
    491                 if ( $purchasable && !$product->is_virtual() && ( in_array( $product->get_type(), $support_product_types ) || ( $product->is_type( 'simple' ) && $product->get_price() > 0 ) ) ) {
    492                     $dependencies = apply_filters( 'mnp_script_dependencies', array(
    493                         'jquery',
    494                         'jquery-ui-core',
    495                         'jquery-ui-widget',
    496                         'jquery-ui-mouse',
    497                         'jquery-ui-position',
    498                         'jquery-ui-draggable',
    499                         'jquery-ui-resizable',
    500                         'jquery-ui-button',
    501                         'jquery-ui-dialog',
    502                         'underscore'
    503                     ) );
    504 
    505                     if ( 'yes' == get_option( 'mnp-npay-script', 'no' ) ) {
    506                         wp_enqueue_script( 'mnp-naverpay', MNP_Manager::button_js_url( wp_is_mobile() ? 'mobile' : 'pc' ), array( 'jquery' ), MNP_VERSION );
    507                         $dependencies[] = 'mnp-naverpay';
    508                     }
    509 
    510                     wp_register_script( 'mnp-frontend', MNP()->plugin_url() . '/assets/js/frontend.js', $dependencies, MNP_VERSION );
    511                     wp_localize_script( 'mnp-frontend', '_mnp', array(
    512                         'ajax_url' => mnp_ajax_url( admin_url( 'admin-ajax.php', 'relative' ) ),
    513                         'order_url_pc' => MNP_Manager::ordersheet_url( 'pc' ),
    514                         'order_url_mobile' => MNP_Manager::ordersheet_url( 'mobile' ),
    515                         'button_js_url_pc' => MNP_Manager::button_js_url( 'pc' ),
    516                         'button_js_url_mobile' => MNP_Manager::button_js_url( 'mobile' ),
    517                         'wishlist_url' => MNP_Manager::wishlist_url(),
    518                         'button_key' => MNP_Manager::button_auth_key(),
    519                         'button_type_pc' => MNP_Manager::button_type_pc(),
    520                         'button_type_mobile' => MNP_Manager::button_type_mobile(),
    521                         'button_color' => MNP_Manager::button_color(),
    522                         'button_count_pc' => MNP_Manager::button_count( 'pc' ),
    523                         'button_count_mobile' => MNP_Manager::button_count( 'mobile' ),
    524                         'create_order_action' => MNP()->slug() . '-create_order',
    525                         'add_to_wishlist_action' => MNP()->slug() . '-add_to_wishlist',
    526                         'wrapper_selector' => get_option( 'mnp-wrapper-selector', 'div[itemtype="http://schema.org/Product"]' ),
    527                         'product_simple_class' => get_option( 'mnp-simple-class', 'product-type-simple' ),
    528                         'product_variable_class' => get_option( 'mnp-variable-class', 'product-type-variable' ),
    529                         'product_grouped_class' => get_option( 'mnp-grouped-class', 'product-type-grouped' ),
    530                         'transition_mode' => get_option( 'mnp-product-page-transition-mode', 'new-window' ),
    531                         'use_submit_handler' => get_option( 'mnp-use-submit-handler', 'no' ),
    532                         'load_script_static' => get_option( 'mnp-npay-script', 'no' )
    533                     ) );
    534 
    535                     wp_enqueue_script( 'underscore' );
    536                     wp_enqueue_script( 'mnp-frontend' );
    537                     wp_enqueue_script( 'jquery-block-ui', MNP()->plugin_url() . '/assets/js/jquery.blockUI.js', $dependencies );
    538 
    539                     wp_register_style( 'mnp-frontend', MNP()->plugin_url() . '/assets/css/naverpay-product.css' );
    540                     wp_enqueue_style( 'mnp-frontend' );
    541 
    542                     wc_get_template( 'single-product/naverpay-button.php', array(), '', MNP()->template_path() );
    543                 }
    544             }
    545         }
    546         public static function wc_validate()
    547         {
    548             if ( empty( WC()->session ) ) {
    549                 include_once( WC()->plugin_path() . '/includes/abstracts/abstract-wc-session.php' );
    550                 $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
    551                 WC()->session = new $session_class();
    552             }
    553 
    554             if ( empty( WC()->cart ) ) {
    555                 WC()->cart = new WC_Cart();
    556             }
    557 
    558             if ( empty( WC()->customer ) ) {
    559                 WC()->customer = new WC_Customer();
    560             }
    561         }
    562         public static function backup_cart( $clear = true )
    563         {
    564             self::wc_validate();
    565 
    566             if ( version_compare( WC_VERSION, '3.2.5', '>' ) ) {
    567                 $cart = WC()->session->get( 'cart', null );
    568                 WC()->session->set( 'mnp-cart', $cart );
    569 
    570                 if ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true ) ) {
    571                     $saved_cart['cart'] = array();
    572                     update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), $saved_cart );
    573                 }
    574 
    575                 WC()->cart->empty_cart( false );
    576                 WC()->session->set( 'cart', null );
    577                 WC()->cart->get_cart_from_session();
    578             } else {
    579                 $cart = WC()->session->get( 'cart', null );
    580                 WC()->session->set( 'mnp-cart', $cart );
    581 
    582                 if ( $clear ) {
    583                     WC()->cart->empty_cart( false );
    584                     WC()->session->set( 'cart', array() );
    585                     WC()->cart->get_cart_from_session();
    586                 }
    587             }
    588         }
    589         public static function recover_cart()
    590         {
    591             $cart = WC()->session->get( 'mnp-cart', null );
    592 
    593             if ( version_compare( WC_VERSION, '3.2.5', '>' ) ) {
    594                 if ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true ) ) {
    595                     $saved_cart['cart'] = $cart;
    596                     update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), $saved_cart );
    597                 }
    598             }
    599 
    600             WC()->cart->empty_cart( false );
    601             WC()->session->set( 'cart', $cart );
    602             WC()->cart->get_cart_from_session();
    603 
    604             WC()->session->set( 'mnp-cart', null );
    605         }
    606         public static function save_cart_contents( $order_key, $cart )
    607         {
    608             if ( 'yes' == get_option( 'mnp-use-cart-management', 'yes' ) ) {
    609                 do_action( 'mnp_before_save_cart_contents', $order_key, $cart );
    610                 set_transient( 'mnp_' . $order_key, $cart->get_cart(), 6 * HOUR_IN_SECONDS );
    611                 $coupons = array();
    612                 foreach ( $cart->get_coupons() as $code => $coupon ) {
    613                     // Avoid storing used_by - it's not needed and can get large.
    614                     $coupon_data = $coupon->get_data();
    615                     unset( $coupon_data['used_by'] );
    616 
    617                     $coupons[$code] = array(
    618                         'code' => $code,
    619                         'discount' => $cart->get_coupon_discount_amount( $code ),
    620                         'discount_tax' => $cart->get_coupon_discount_tax_amount( $code ),
    621                         'coupon_data' => $coupon_data
    622                     );
    623                 }
    624 
    625                 set_transient( 'mnp_coupons_' . $order_key, $coupons, 6 * HOUR_IN_SECONDS );
    626 
    627                 do_action( 'mnp_after_save_cart_contents', $order_key, $cart );
    628             }
    629         }
    630         public static function search_cart_item( $product_id, $merchant_product_id, $cart = null )
    631         {
    632 
    633             if ( is_null( $cart ) ) {
    634                 $cart = WC()->cart;
    635             }
    636 
    637             if ( is_numeric( $merchant_product_id ) ) {
    638                 $_product_id = $merchant_product_id;
    639             } else {
    640                 $_product_id = $product_id;
    641 
    642                 $wc_product = wc_get_product( $product_id );
    643 
    644                 if ( !$wc_product ) {
    645                     $_product_id = wc_get_product_id_by_sku( $product_id );
    646                 }
    647             }
    648 
    649             if ( is_callable( array( $cart, 'get_cart_contents' ) ) ) {
    650                 $cart_contents = $cart->get_cart_contents();
    651             } else {
    652                 $cart_contents = $cart->cart_contents;
    653             }
    654 
    655             foreach ( $cart_contents as $cart_item_key => $cart_item ) {
    656                 if ( apply_filters( 'mnp_search_cart_item_by_cart_item_key', false, $cart_item ) ) {
    657                     if ( $product_id == apply_filters( 'mnp_get_product_id_from_cart_item_key', '', $cart_item_key, $cart_item ) ) {
    658                         return $cart_item;
    659                     }
    660                 } else if ( $_product_id == $cart_item['product_id'] || $_product_id == $cart_item['variation_id'] ) {
    661                     return $cart_item;
    662                 }
    663             }
    664 
    665             return null;
    666         }
    667         public static function get_attribute_slug( $product, $label )
    668         {
    669             $attributes = $product->get_variation_attributes();
    670 
    671             foreach ( $attributes as $slug => $values ) {
    672                 MNP_Logger::add_log( sprintf( "=== [%s] [%s]", $label, wc_attribute_label( $slug ) ) );
    673                 if ( $label == wc_attribute_label( $slug ) ) {
    674                     return $slug;
    675                 }
    676             }
    677 
    678             return null;
    679         }
    680         public static function generate_cart( $npay_orders, $order = null )
    681         {
    682             $cart = WC()->cart;
    683 
    684             do_action( 'mnp_before_generate_cart', $npay_orders, $order );
    685 
    686             $cart->empty_cart( false );
    687 
    688             if ( is_null( $order ) ) {
    689                 $cart_contents = mnp_load_saved_cart_contents_from_npay_order( current( $npay_orders ) );
    690             } else {
    691                 $cart_contents = mnp_load_saved_cart_contents_from_order( $order );
    692             }
    693 
    694             foreach ( $npay_orders as $npay_order ) {
    695                 if ( apply_filters( 'mnp_generate_cart_skip_npay_order', false, $npay_order ) ) {
    696                     continue;
    697                 }
    698 
    699                 if ( property_exists( $npay_order->ProductOrder, 'OptionManageCode' ) ) {
    700                     $product_id = apply_filters( 'mnp_get_product_id_from_option_manage_code', $npay_order->ProductOrder->OptionManageCode, $npay_order );
    701                 } else {
    702                     $product_id = $npay_order->ProductOrder->SellerProductCode;
    703                 }
    704 
    705                 $product = wc_get_product( $product_id );
    706 
    707                 if ( $product ) {
    708                     if ( $product->is_type( 'variation' ) ) {
    709                         $product_id = $product->get_parent_id();
    710                         $variation_id = $product->get_id();
    711                     } else {
    712                         $product_id = $product->get_id();
    713                         $variation_id = '0';
    714                     }
    715 
    716                     $np_product_id = $npay_order->ProductOrder->ProductID;
    717                     $quantity = $npay_order->ProductOrder->Quantity;
    718 
    719                     if ( !empty( $cart_contents ) ) {
    720                         foreach ( $cart_contents as $cart_item_key => &$cart_item ) {
    721                             if ( empty( $cart_item['_npay_order'] ) ) {
    722                                 if ( apply_filters( 'mnp_search_cart_item_by_cart_item_key', false, $cart_item ) ) {
    723                                     $searched_product_id = apply_filters( 'mnp_get_product_id_from_cart_item_key', '', $cart_item_key, $cart_item );
    724 
    725                                     if ( $npay_order->ProductOrder->ProductID == $searched_product_id || ( property_exists( $npay_order->ProductOrder, 'OptionManageCode' ) && $npay_order->ProductOrder->OptionManageCode == $searched_product_id ) ) {
    726                                         $cart_item['_npay_product_order_id'] = $npay_order->ProductOrder->ProductOrderID;
    727                                         $cart_item['_npay_product_order_status'] = $npay_order->ProductOrder->ProductOrderStatus;
    728                                         $cart_item['_npay_order'] = json_encode( $npay_order, JSON_UNESCAPED_UNICODE );
    729                                         break;
    730                                     }
    731                                 } else if ( $cart_item['product_id'] == $product_id && $cart_item['variation_id'] == $variation_id && $cart_item['quantity'] == $quantity ) {
    732                                     $cart_item['_npay_product_order_id'] = $npay_order->ProductOrder->ProductOrderID;
    733                                     $cart_item['_npay_product_order_status'] = $npay_order->ProductOrder->ProductOrderStatus;
    734                                     $cart_item['_npay_order'] = json_encode( $npay_order, JSON_UNESCAPED_UNICODE );
    735                                     break;
    736                                 }
    737                             }
    738                         }
    739 
    740                         if ( $product->is_type( 'diy-bundle' ) ) {
    741                             $bundle_product_order_ids = array();
    742 
    743                             foreach ( $product->get_bundle_products() as $bundle_product ) {
    744                                 $bundle_product_id = $bundle_product['id'];
    745 
    746                                 foreach ( $npay_orders as $_npay_order ) {
    747 
    748                                     if ( property_exists( $_npay_order->ProductOrder, 'OptionManageCode' ) && 0 === strpos( $_npay_order->ProductOrder->OptionManageCode, 'msdp_bundle_' ) ) {
    749                                         if ( $_npay_order->ProductOrder->MerchantProductId == $product_id && apply_filters( 'mnp_get_product_id_from_option_manage_code', $_npay_order->ProductOrder->OptionManageCode, $_npay_order ) == $bundle_product_id ) {
    750                                             $bundle_product_order_ids[] = $_npay_order->ProductOrder->ProductOrderID;
    751                                         }
    752                                     }
    753                                 }
    754                             }
    755 
    756                             $cart_item['_npay_bundle_product_order_ids'] = implode( ',', $bundle_product_order_ids );
    757 
    758                         }
    759                     } else {
    760                         add_filter( 'woocommerce_add_cart_item', array( __CLASS__, 'maybe_update_cart_item_price' ), 10, 2 );
    761                         remove_action( 'woocommerce_after_calculate_totals', array( 'MSMS_Cart', 'maybe_apply_membership_coupons' ) );
    762                         $variations = array();
    763                         if ( !empty( $npay_order->ProductOrder->ProductOption ) ) {
    764                             $parent_product = wc_get_product( $product->get_parent_id() );
    765                             $options = explode( '/', $npay_order->ProductOrder->ProductOption );
    766                             foreach ( $options as $option ) {
    767                                 $values = explode( ':', $option );
    768                                 $values[0] = trim( $values[0] );
    769                                 $values[1] = trim( $values[1] );
    770 
    771                                 if ( $parent_product ) {
    772                                     $slug = self::get_attribute_slug( $parent_product, $values[0] );
    773                                     $term = get_term_by( 'name', $values[1], $slug );
    774                                 }
    775 
    776                                 if ( $parent_product && !empty( $slug ) ) {
    777                                     if ( is_object( $term ) ) {
    778                                         $variations['attribute_' . $slug] = $term->slug;
    779                                     } else {
    780                                         $variations['attribute_' . $slug] = apply_filters( 'mnp_product_attributes_label', $values[1], $slug, $parent_product );
    781                                     }
    782                                 } else {
    783                                     $variations[$values[0]] = $values[1];
    784                                 }
    785                             }
    786                         }
    787 
    788                         $product_info = array(
    789                             'product_id' => $product_id,
    790                             'np_product_id' => $np_product_id,
    791                             'price' => apply_filters( 'mnp_get_product_price_by_id', $npay_order->ProductOrder->UnitPrice, $np_product_id, $product_id, $npay_order )
    792                         );
    793 
    794                         $variations = apply_filters( 'mnp_get_product_variations', $variations, $product_info, $npay_order );
    795                         $cart_item_data = apply_filters( 'mnp_get_product_cart_item_data', array(
    796                             '_npay_product_order_id' => $npay_order->ProductOrder->ProductOrderID,
    797                             '_npay_product_order_status' => $npay_order->ProductOrder->ProductOrderStatus,
    798                             '_npay_order' => json_encode( $npay_order, JSON_UNESCAPED_UNICODE ),
    799                             '_npay_price' => $npay_order->ProductOrder->UnitPrice
    800                         ), $product_info, $npay_order );
    801 
    802                         $cart->add_to_cart( $product_id, $quantity, $variation_id, $variations, $cart_item_data );
    803 
    804                         add_action( 'woocommerce_after_calculate_totals', array(
    805                             'MSMS_Cart',
    806                             'maybe_apply_membership_coupons'
    807                         ) );
    808                         remove_filter( 'woocommerce_add_cart_item', array(
    809                             __CLASS__,
    810                             'maybe_update_cart_item_price'
    811                         ), 10 );
    812                     }
    813 
    814                 }
    815             }
    816             foreach ( $cart_contents as $cart_content_key => $cart_content ) {
    817                 if ( empty( $cart_content['_npay_order'] ) ) {
    818                     unset( $cart_contents[$cart_content_key] );
    819                 }
    820             }
    821 
    822             if ( !empty( $cart_contents ) ) {
    823                 if ( is_callable( array( WC()->cart, 'set_cart_contents' ) ) ) {
    824                     $cart->set_cart_contents( $cart_contents );
    825                 } else {
    826                     $cart->cart_contents = $cart_contents;
    827                 }
    828             }
    829 
    830             do_action( 'mnp_after_generate_cart', $npay_orders, $order );
    831         }
    832 
    833         public static function maybe_update_cart_item_price( $cart_item_data, $cart_item_key )
    834         {
    835             if ( isset( $cart_item_data['_npay_price'] ) ) {
    836                 $cart_item_data['data']->set_price( floatval( $cart_item_data['_npay_price'] ) );
    837             }
    838 
    839             return $cart_item_data;
    840         }
     5if ( ! class_exists( 'MNP_Cart' ) ) {
     6    class MNP_Cart {
     7
     8        static $shippingPolicy = null;
     9
     10        static function get_order_key() {
     11            $data = array(
     12                MNP_Manager::merchant_id(),
     13                date( 'Y-m-d H:i:s' ),
     14                strtoupper( bin2hex( openssl_random_pseudo_bytes( 20 ) ) )
     15            );
     16
     17            return date( 'YmdHis' ) . '_' . strtoupper( md5( json_encode( $data ) ) );
     18        }
     19
     20        static function cart_contains_npay_items() {
     21            $support_product_types = apply_filters( 'mnp_support_product_types', array( 'variable', 'variation' ) );
     22
     23            if ( WC()->cart ) {
     24                foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
     25
     26                    if ( $values[ 'variation_id' ] ) {
     27                        $product_id = $values[ 'variation_id' ];
     28                        $variation  = $values[ 'variation' ];
     29                    } else {
     30                        $product_id = $values[ 'product_id' ];
     31                        $variation  = null;
     32                    }
     33
     34                    $wc_product = wc_get_product( $product_id );
     35
     36                    if ( MNP_Manager::is_purchasable( $values[ 'product_id' ] ) && MNP_Manager::is_purchasable( $product_id ) && $wc_product->is_in_stock() && $wc_product->has_enough_stock( $values[ 'quantity' ] ) && $wc_product->is_purchasable() && ! $wc_product->is_virtual() &&
     37                         ( in_array( $wc_product->get_type(), $support_product_types ) || ( $wc_product->is_type( 'simple' ) && $wc_product->get_price() > 0 ) )
     38                    ) {
     39                        return true;
     40                    }
     41                }
     42            }
     43
     44            return false;
     45        }
     46        static function generate_product_info( $args ) {
     47            $product_id          = apply_filters( 'mnp_get_product_id_from_cart_item_key', $args[ 'product_id' ], $args[ 'cart_item_key' ], $args[ 'cart_item' ] );
     48            $merchant_product_id = $args[ 'product_id' ];
     49            $quantity            = $args[ 'quantity' ];
     50            $variation           = $args[ 'variations' ];
     51            $line_total          = $args[ 'line_total' ];
     52            $line_tax            = $args[ 'line_tax' ];
     53
     54            $wc_product = wc_get_product( $merchant_product_id );
     55            $tax = 'TAX';
     56            if ( wc_tax_enabled() ) {
     57                if ( $line_tax <= 0 ) {
     58                    $tax = 'ZERO_TAX';
     59                }
     60
     61                $line_total = absint( round( $line_total + $line_tax, wc_get_price_decimals() ) );
     62            }
     63
     64            $unit_price = $line_total / $quantity;
     65            if ( $wc_product->is_type( 'simple' ) ) {
     66                // 단순상품 정보를 생성한다.
     67                $single = null;
     68                $option = apply_filters( 'mnp_generate_product_option_simple', null, $args );
     69
     70                if ( empty( $option ) ) {
     71                    $single = new ProductSingle( $quantity );
     72                    $option = null;
     73                }
     74            } elseif ( $wc_product->is_type( 'variation' ) ) {
     75                // 옵션상품 정보를 생성한다.
     76                $single        = null;
     77                $selectedItems = array();
     78
     79                if ( ! empty( $variation ) ) {
     80                    $attributes = $variation;
     81                } else {
     82                    $attributes = $wc_product->get_variation_attributes();
     83                }
     84
     85                foreach ( $attributes as $key => $value ) {
     86                    $option_id   = str_replace( 'attribute_', '', $key );
     87                    $option_name = html_entity_decode( wc_attribute_label( $option_id ) );
     88                    $term        = get_term_by( 'slug', $value, $option_id );
     89                    $option_text = html_entity_decode( $term->name );
     90
     91                    $selectedItems[] = new ProductOptionSelectedItem( ProductOptionSelectedItem::TYPE_SELECT, $option_name, $term->slug, $option_text );
     92                }
     93
     94                $selectedItems = apply_filters( 'mnp_generate_product_option_variable', $selectedItems, $args );
     95
     96                $option = new ProductOption( $quantity, 0, null, $selectedItems );
     97            } else {
     98                $single = apply_filters( 'mnp_generate_product_info_single', null, $args );
     99                $option = apply_filters( 'mnp_generate_product_option_simple', null, $args );
     100            }
     101
     102            $img_url = '';
     103            $images  = wp_get_attachment_image_src( $wc_product->get_image_id(), array( 300, 300 ) );
     104
     105            if ( ! empty( $images ) ) {
     106                $img_url = $images[ 0 ];
     107                if ( empty( $img_url ) && ! empty( $args[ 'parent_product_id' ] ) ) {
     108                    $parent_product = wc_get_product( $args[ 'parent_product_id' ] );
     109                    $images         = wp_get_attachment_image_src( $parent_product->get_image_id(), array( 300, 300 ) );
     110                    if ( ! empty( $images ) ) {
     111                        $img_url = $images[ 0 ];
     112                    }
     113                }
     114                if ( 'yes' == get_option( 'mnp-force-image-url-to-http', 'yes' ) ) {
     115                    $img_url = preg_replace( "/^https:/i", "http:", $img_url );
     116                }
     117            }
     118
     119            $img_url = apply_filters( 'mnp_product_image_url', $img_url, $product_id );
     120
     121            if ( empty( $img_url ) ) {
     122                wp_send_json_error( array( 'message' => '상품 이미지가 없습니다.' ) );
     123            }
     124            $product_id = apply_filters( 'mnp_product_id', $product_id, $args );
     125            $merchant_product_id = apply_filters( 'mnp_merchant_product_id', $merchant_product_id, $args );
     126
     127            $supplements = apply_filters( 'mnp_supplements', array(), $args );
     128
     129            $gifts = apply_filters( 'mnp_get_gifts_from_cart_item', array(), $args[ 'cart_item' ] );
     130            return new Product(
     131                $product_id, /** 상품 번호 */
     132                $merchant_product_id, /** 가맹점 상품 번호 */
     133                apply_filters( 'mnks_ecmall_product_id', null, $product_id ), /** 지식쇼핑 EP의 Mall_pid */
     134                html_entity_decode( $wc_product->get_title() ), /** 상품명 */
     135                $unit_price, /** 상품가격 */
     136                $tax, /** 세금종류 */
     137                $wc_product->get_permalink(), /** 상품 URL */
     138                $img_url, /** 상품 Thumbnail URL */
     139                implode( ',', $gifts ), /** giftName */
     140                $single, /** 단순 상품 정보 */
     141                $option, /** 옵션 상품 정보 */
     142                self::$shippingPolicy/** 배송 정책 */,
     143                $supplements
     144            );
     145        }
     146        public static function checkout_cart() {
     147            MNP_Logger::add_log( 'checkout_cart' );
     148
     149            do_action( 'mnp_before_checkout_cart' );
     150
     151            $selected_cart_items = array_filter( explode( ',', mnp_get( $_POST, 'msbn_keys', '' ) ) );
     152            add_filter( 'mshop_membership_skip_filter', '__return_true' );
     153            mnp_maybe_define_constant( 'WOOCOMMERCE_CART', true );
     154
     155
     156            $support_product_types = apply_filters( 'mnp_support_product_types', array( 'variable', 'variation' ) );
     157
     158            include_once( 'naverpay/Order.php' );
     159
     160            self::$shippingPolicy = MNP_Shipping::get_shipping_policy( WC()->cart );
     161            wc_clear_notices();
     162
     163            if ( ! WC()->cart->check_cart_items() ) {
     164                $msg = implode( ', ', wc_get_notices( 'error' ) );
     165                wp_send_json_error( array( 'message' => htmlspecialchars_decode( strip_tags( $msg ) ) ) );
     166            }
     167
     168            WC()->cart->calculate_totals();
     169
     170            $products      = array();
     171            $cart_contents = apply_filters( 'mnp_checkout_cart_get_cart_contents', WC()->cart->get_cart() );
     172
     173            foreach ( $cart_contents as $cart_item_key => $values ) {
     174
     175                if ( ! empty( $selected_cart_items ) && ! in_array( $cart_item_key, $selected_cart_items ) ) {
     176                    continue;
     177                }
     178
     179                if ( $values[ 'variation_id' ] ) {
     180                    $product_id = $values[ 'variation_id' ];
     181                    $variation  = $values[ 'variation' ];
     182                } else {
     183                    $product_id = $values[ 'product_id' ];
     184                    $variation  = null;
     185                }
     186
     187                $wc_product = wc_get_product( $product_id );
     188
     189                if ( MNP_Manager::is_purchasable( $values[ 'product_id' ] ) && MNP_Manager::is_purchasable( $product_id ) && $wc_product->is_in_stock() && $wc_product->has_enough_stock( $values[ 'quantity' ] ) && $wc_product->is_purchasable() && ! $wc_product->is_virtual() &&
     190                     ( in_array( $wc_product->get_type(), $support_product_types ) || ( $wc_product->is_type( 'simple' ) && $wc_product->get_price() > 0 ) )
     191                ) {
     192                    $products[] = self::generate_product_info( array(
     193                        'product_id'     => $product_id,
     194                        'quantity'       => $values[ 'quantity' ],
     195                        'variations'     => $variation,
     196                        'line_total'     => $values[ 'line_total' ],
     197                        'line_tax'       => $values[ 'line_tax' ],
     198                        'cart_item_data' => apply_filters( 'mnp_get_product_cart_item_data', array(), $values ),
     199                        'cart_item'      => $values,
     200                        'cart_item_key'  => $cart_item_key
     201                    ) );
     202                }
     203            }
     204            if ( 0 == count( $products ) ) {
     205                wp_send_json_error( array( 'message' => '네이버페이로 구매가능한 상품이 없습니다.' ) );
     206            }
     207
     208            $npay_order_key = self::get_order_key();
     209            self::save_cart_contents( $npay_order_key, WC()->cart );
     210            $custom_data = apply_filters( 'mnp_custom_order_data', array( 'order_key' => $npay_order_key ) );
     211            $order       = new Order( $products, self::get_back_url(), $custom_data );
     212            $data        = MNP_XMLSerializer::generateValidXmlFromObj( json_decode( json_encode( $order ) ), 'order' );
     213
     214            MNP_Logger::add_log( print_r( $order, true ) );
     215
     216            $result   = MNP_API::register_order( $data );
     217            $response = $result->response;
     218
     219            do_action( 'mnp_after_checkout_cart' );
     220
     221            if ( $response->ResponseType == "SUCCESS" ) {
     222                wp_send_json_success( array( 'authkey' => $response->AuthKey, 'shopcode' => $response->ShopCode ) );
     223            } else {
     224                wp_send_json_error( array( 'message' => $response->Error->Message ) );
     225            }
     226        }
     227
     228        protected static function get_options( $variation_id, $variations ) {
     229            $wc_product = wc_get_product( $variation_id );
     230
     231            if ( is_product( $wc_product ) && $wc_product->is_type( 'variation' ) ) {
     232                // 옵션상품 정보를 생성한다.
     233                $single          = null;
     234                $selectedOptions = array();
     235
     236                if ( ! empty( $variation ) ) {
     237                    $attributes = $variation;
     238                } else {
     239                    $attributes = $wc_product->get_variation_attributes();
     240                }
     241
     242                foreach ( $attributes as $key => $value ) {
     243                    $option_id   = str_replace( 'attribute_', '', $key );
     244                    $option_name = html_entity_decode( wc_attribute_label( $option_id ) );
     245                    $term        = get_term_by( 'slug', $value, $option_id );
     246                    $option_text = html_entity_decode( $term->name );
     247
     248                    $selectedOptions[] = $option_name . ' : ' . $option_text;
     249                }
     250
     251                return ' ( ' . implode( ', ', $selectedOptions ) . ' )';
     252            }
     253        }
     254        protected static function get_back_url() {
     255            $back_url = remove_query_arg( 'NaPm', $_SERVER[ 'HTTP_REFERER' ] );
     256
     257            if ( apply_filters( 'mnp_remove_query_args_from_back_url', false ) ) {
     258                $urls     = parse_url( $back_url );
     259                $back_url = sprintf( "%s://%s%s", $urls[ 'scheme' ], $urls[ 'host' ], $urls[ 'path' ] );
     260            }
     261
     262            return $back_url;
     263        }
     264        public static function create_order() {
     265            MNP_Logger::add_log( 'create_order' );
     266
     267            do_action( 'mnp_before_create_order' );
     268
     269            include_once( 'naverpay/Order.php' );
     270            add_filter( 'mshop_membership_skip_filter', '__return_true' );
     271            mnp_maybe_define_constant( 'WOOCOMMERCE_CART', true );
     272
     273            wc_clear_notices();
     274            self::backup_cart();
     275            $_POST_ORG = wc_clean( $_POST );
     276
     277            $products = wc_clean( $_REQUEST[ 'products' ] );
     278
     279            foreach ( $products as $product_info ) {
     280                $params = array();
     281                parse_str( wc_clean( $product_info[ 'form_data' ] ), $params );
     282
     283                $_POST = array_merge( wc_clean( $_POST ), wc_clean( $params ) );
     284
     285                $product_id     = ! empty( $product_info[ 'parent_product_id' ] ) ? $product_info[ 'parent_product_id' ] : $product_info[ 'product_id' ];
     286                $variation_id   = ! empty( $product_info[ 'parent_product_id' ] ) ? $product_info[ 'product_id' ] : 0;
     287                $variations     = apply_filters( 'mnp_get_product_variations', $product_info[ 'attributes' ], $product_info );
     288                $cart_item_data = apply_filters( 'mnp_get_product_cart_item_data', array(), $product_info );
     289                WC()->cart->add_to_cart( $product_id, $product_info[ 'quantity' ], $variation_id, $variations, $cart_item_data );
     290
     291                if ( wc_notice_count( 'error' ) > 0 ) {
     292                    self::recover_cart();
     293
     294                    $notices = wc_get_notices( 'error' );
     295                    wc_clear_notices();
     296
     297                    $options = self::get_options( $variation_id, $variations );
     298
     299                    $notices = wp_list_pluck( $notices, 'notice' );
     300
     301                    wp_send_json_error( array( 'message' => htmlspecialchars_decode( strip_tags( implode( "\n", $notices ) ) ) . $options ) );
     302                }
     303
     304                $_POST = $_POST_ORG;
     305            }
     306            WC()->cart->calculate_totals();
     307
     308            do_action( 'woocommerce_check_cart_items' );
     309
     310            if ( wc_notice_count( 'error' ) > 0 ) {
     311                self::recover_cart();
     312
     313                $notices = wc_get_notices( 'error' );
     314                wc_clear_notices();
     315
     316                wp_send_json_error( array( 'message' => htmlspecialchars_decode( strip_tags( implode( "\n", $notices ) ) ) ) );
     317            }
     318            self::$shippingPolicy = MNP_Shipping::get_shipping_policy( WC()->cart );
     319            $products      = array();
     320            $cart_contents = apply_filters( 'mnp_create_order_get_cart_contents', WC()->cart->get_cart() );
     321
     322            foreach ( $cart_contents as $cart_item_key => $values ) {
     323                if ( ! empty( $values[ 'variation_id' ] ) ) {
     324                    $product_id = $values[ 'variation_id' ];
     325                    $variation  = $values[ 'variation' ];
     326                } else {
     327                    $product_id = $values[ 'product_id' ];
     328                    $variation  = null;
     329                }
     330
     331                $products[] = self::generate_product_info( array(
     332                    'product_id'     => $product_id,
     333                    'quantity'       => $values[ 'quantity' ],
     334                    'variations'     => $variation,
     335                    'line_total'     => $values[ 'line_total' ],
     336                    'line_tax'       => $values[ 'line_tax' ],
     337                    'cart_item_data' => apply_filters( 'mnp_get_product_cart_item_data', array(), $values ),
     338                    'cart_item'      => $values,
     339                    'cart_item_key'  => $cart_item_key
     340                ) );
     341            }
     342            $npay_order_key = self::get_order_key();
     343            self::save_cart_contents( $npay_order_key, WC()->cart );
     344            self::recover_cart();
     345            $custom_data = apply_filters( 'mnp_custom_order_data', array( 'order_key' => $npay_order_key ) );
     346            $order       = new Order( $products, self::get_back_url(), $custom_data );
     347            $data        = MNP_XMLSerializer::generateValidXmlFromObj( json_decode( json_encode( $order ) ), 'order' );
     348
     349            MNP_Logger::add_log( print_r( $order, true ) );
     350
     351            $result   = MNP_API::register_order( $data );
     352            $response = $result->response;
     353
     354            do_action( 'mnp_after_create_order' );
     355
     356            if ( $response->ResponseType == "SUCCESS" ) {
     357                wp_send_json_success( array( 'authkey' => $response->AuthKey, 'shopcode' => $response->ShopCode ) );
     358            } else {
     359                wp_send_json_error( array( 'message' => $response->Error->Message ) );
     360            }
     361        }
     362        public static function add_to_wishlist() {
     363            global $wishlistItemId;
     364
     365            $queryString = 'SHOP_ID=' . urlencode( MNP_Manager::merchant_id() );
     366            $queryString .= '&CERTI_KEY=' . urlencode( MNP_Manager::auth_key() );
     367
     368            foreach ( $_REQUEST[ 'products' ] as $product_info ) {
     369                $wc_product = wc_get_product( $product_info[ 'product_id' ] );
     370
     371                $img_url = wp_get_attachment_image_src( $wc_product->get_image_id(), array( 300, 300 ) )[ 0 ];
     372                if ( 'yes' == get_option( 'mnp-force-image-url-to-http', 'yes' ) ) {
     373                    $img_url = preg_replace( "/^https:/i", "http:", $img_url );
     374                }
     375
     376                $queryString .= '&ITEM_ID=' . urlencode( $product_info[ 'product_id' ] );
     377                $queryString .= '&ITEM_NAME=' . urlencode( $wc_product->get_title() );
     378                $queryString .= '&ITEM_DESC=' . urlencode( $wc_product->get_title() );
     379                $queryString .= '&ITEM_UPRICE=' . $wc_product->get_price();
     380                $queryString .= '&ITEM_IMAGE=' . urlencode( utf8_uri_encode( $img_url ) );
     381                $queryString .= '&ITEM_THUMB=' . urlencode( utf8_uri_encode( $img_url ) );
     382                $queryString .= '&ITEM_URL=' . urlencode( $wc_product->get_permalink() );
     383
     384                break;
     385            }
     386
     387            $response = wp_remote_post( MNP_Manager::wishlist_url(), array(
     388                    'method'      => 'POST',
     389                    'headers'     => array( 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8;' ),
     390                    'timeout'     => 45,
     391                    'redirection' => 5,
     392                    'httpversion' => '1.1',
     393                    'blocking'    => true,
     394                    'body'        => $queryString,
     395                    'cookies'     => array()
     396                )
     397            );
     398
     399            if ( is_wp_error( $response ) ) {
     400                wp_send_json_error( array( 'message' => $response->get_error_message() ) );
     401            } else {
     402                ob_start();
     403                wc_get_template( 'wishlist-popup' . ( wp_is_mobile() ? '-mobile' : '' ) . '.php', array( 'wishlistItemIds' => explode( ',', $response[ 'body' ] ) ), '', MNP()->template_path() );
     404                $html = ob_get_clean();
     405
     406                wp_send_json_success( array(
     407                    'url'  => MNP_Manager::wishlist_url(),
     408                    'html' => $html
     409                ) );
     410            }
     411        }
     412        public static function woocommerce_after_cart_table() {
     413            if ( ! apply_filters( 'mnp_enabled', true ) && ! empty( $_GET[ 's' ] ) ) {
     414                return;
     415            }
     416
     417            if ( MNP_Manager::is_operable() && self::cart_contains_npay_items() ) {
     418                $dependencies = array(
     419                    'jquery',
     420                    'jquery-ui-core',
     421                    'jquery-ui-widget',
     422                    'jquery-ui-mouse',
     423                    'jquery-ui-position',
     424                    'jquery-ui-draggable',
     425                    'jquery-ui-resizable',
     426                    'jquery-ui-button',
     427                    'jquery-ui-dialog',
     428                    'underscore'
     429                );
     430
     431                if ( 'yes' == get_option( 'mnp-npay-script', 'no' ) ) {
     432                    wp_enqueue_script( 'mnp-naverpay', MNP_Manager::button_js_url( wp_is_mobile() ? 'mobile' : 'pc' ), array( 'jquery' ), MNP_VERSION );
     433                    $dependencies[] = 'mnp-naverpay';
     434                }
     435
     436                wp_register_script( 'mnp-frontend', MNP()->plugin_url() . '/assets/js/cart.js', $dependencies, MNP_VERSION );
     437                wp_localize_script( 'mnp-frontend', '_mnp', array(
     438                    'ajax_url'             => mnp_ajax_url( admin_url( 'admin-ajax.php', 'relative' ) ),
     439                    'order_url_pc'         => MNP_Manager::ordersheet_url( 'pc' ),
     440                    'order_url_mobile'     => MNP_Manager::ordersheet_url( 'mobile' ),
     441                    'button_js_url_pc'     => MNP_Manager::button_js_url( 'pc' ),
     442                    'button_js_url_mobile' => MNP_Manager::button_js_url( 'mobile' ),
     443                    'wishlist_url'         => MNP_Manager::wishlist_url(),
     444                    'button_key'           => MNP_Manager::button_auth_key(),
     445                    'button_type_pc'       => MNP_Manager::button_type_pc(),
     446                    'button_type_mobile'   => MNP_Manager::button_type_mobile(),
     447                    'button_color'         => MNP_Manager::button_color(),
     448                    'checkout_cart_action' => MNP()->slug() . '-checkout_cart',
     449                    'transition_mode'      => get_option( 'mnp-cart-page-transition-mode', 'new-window' ),
     450                    'load_script_static'   => get_option( 'mnp-npay-script', 'no' )
     451                ) );
     452                wp_enqueue_script( 'underscore' );
     453                wp_enqueue_script( 'mnp-frontend' );
     454                wp_enqueue_script( 'jquery-block-ui', MNP()->plugin_url() . '/assets/js/jquery.blockUI.js', $dependencies );
     455
     456                wp_register_style( 'mnp-frontend', MNP()->plugin_url() . '/assets/css/naverpay-cart.css' );
     457                wp_enqueue_style( 'mnp-frontend' );
     458
     459                wc_get_template( 'cart/naverpay-button.php', array(), '', MNP()->template_path() );
     460            }
     461        }
     462        public static function woocommerce_after_add_to_cart_form() {
     463            if ( ! apply_filters( 'mnp_enabled', true ) ) {
     464                return;
     465            }
     466
     467            if ( ! empty( $_REQUEST[ 'elementor-preview' ] ) || 'elementor' == mnp_get( $_GET, 'action' ) ) {
     468                return '';
     469            }
     470
     471            $support_product_types = apply_filters( 'mnp_support_product_types', array( 'variable', 'grouped' ) );
     472
     473            $product_id = get_the_ID();
     474
     475            if ( MNP_Manager::is_operable() && MNP_Manager::is_purchasable( $product_id ) ) {
     476                $product = wc_get_product( $product_id );
     477
     478                $purchasable = 'grouped' == $product->get_type() ? true : $product->is_purchasable();
     479
     480                if ( $purchasable && ! $product->is_virtual() && ( in_array( $product->get_type(), $support_product_types ) || ( $product->is_type( 'simple' ) && $product->get_price() > 0 ) ) ) {
     481                    $dependencies = apply_filters( 'mnp_script_dependencies', array(
     482                        'jquery',
     483                        'jquery-ui-core',
     484                        'jquery-ui-widget',
     485                        'jquery-ui-mouse',
     486                        'jquery-ui-position',
     487                        'jquery-ui-draggable',
     488                        'jquery-ui-resizable',
     489                        'jquery-ui-button',
     490                        'jquery-ui-dialog',
     491                        'underscore'
     492                    ) );
     493
     494                    if ( 'yes' == get_option( 'mnp-npay-script', 'no' ) ) {
     495                        wp_enqueue_script( 'mnp-naverpay', MNP_Manager::button_js_url( wp_is_mobile() ? 'mobile' : 'pc' ), array( 'jquery' ), MNP_VERSION );
     496                        $dependencies[] = 'mnp-naverpay';
     497                    }
     498
     499                    wp_register_script( 'mnp-frontend', MNP()->plugin_url() . '/assets/js/frontend.js', $dependencies, MNP_VERSION );
     500                    wp_localize_script( 'mnp-frontend', '_mnp', array(
     501                        'ajax_url'               => mnp_ajax_url( admin_url( 'admin-ajax.php', 'relative' ) ),
     502                        'order_url_pc'           => MNP_Manager::ordersheet_url( 'pc' ),
     503                        'order_url_mobile'       => MNP_Manager::ordersheet_url( 'mobile' ),
     504                        'button_js_url_pc'       => MNP_Manager::button_js_url( 'pc' ),
     505                        'button_js_url_mobile'   => MNP_Manager::button_js_url( 'mobile' ),
     506                        'wishlist_url'           => MNP_Manager::wishlist_url(),
     507                        'button_key'             => MNP_Manager::button_auth_key(),
     508                        'button_type_pc'         => MNP_Manager::button_type_pc(),
     509                        'button_type_mobile'     => MNP_Manager::button_type_mobile(),
     510                        'button_color'           => MNP_Manager::button_color(),
     511                        'button_count_pc'        => MNP_Manager::button_count( 'pc' ),
     512                        'button_count_mobile'    => MNP_Manager::button_count( 'mobile' ),
     513                        'create_order_action'    => MNP()->slug() . '-create_order',
     514                        'add_to_wishlist_action' => MNP()->slug() . '-add_to_wishlist',
     515                        'wrapper_selector'       => get_option( 'mnp-wrapper-selector', 'div[itemtype="http://schema.org/Product"]' ),
     516                        'product_simple_class'   => get_option( 'mnp-simple-class', 'product-type-simple' ),
     517                        'product_variable_class' => get_option( 'mnp-variable-class', 'product-type-variable' ),
     518                        'product_grouped_class'  => get_option( 'mnp-grouped-class', 'product-type-grouped' ),
     519                        'transition_mode'        => get_option( 'mnp-product-page-transition-mode', 'new-window' ),
     520                        'use_submit_handler'     => get_option( 'mnp-use-submit-handler', 'no' ),
     521                        'load_script_static'     => get_option( 'mnp-npay-script', 'no' )
     522                    ) );
     523
     524                    wp_enqueue_script( 'underscore' );
     525                    wp_enqueue_script( 'mnp-frontend' );
     526                    wp_enqueue_script( 'jquery-block-ui', MNP()->plugin_url() . '/assets/js/jquery.blockUI.js', $dependencies );
     527
     528                    wp_register_style( 'mnp-frontend', MNP()->plugin_url() . '/assets/css/naverpay-product.css' );
     529                    wp_enqueue_style( 'mnp-frontend' );
     530
     531                    wc_get_template( 'single-product/naverpay-button.php', array(), '', MNP()->template_path() );
     532                }
     533            }
     534        }
     535        public static function wc_validate() {
     536            if ( empty( WC()->session ) ) {
     537                include_once( WC()->plugin_path() . '/includes/abstracts/abstract-wc-session.php' );
     538                $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
     539                WC()->session  = new $session_class();
     540            }
     541
     542            if ( empty( WC()->cart ) ) {
     543                WC()->cart = new WC_Cart();
     544            }
     545
     546            if ( empty( WC()->customer ) ) {
     547                WC()->customer = new WC_Customer();
     548            }
     549        }
     550        public static function backup_cart( $clear = true ) {
     551            self::wc_validate();
     552
     553            $cart = WC()->session->get( 'cart', null );
     554            WC()->session->set( 'mnp-cart', $cart );
     555
     556            if ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true ) ) {
     557                $saved_cart[ 'cart' ] = array();
     558                update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), $saved_cart );
     559            }
     560
     561            WC()->cart->empty_cart( false );
     562            WC()->session->set( 'cart', null );
     563            WC()->cart->get_cart_from_session();
     564        }
     565        public static function recover_cart() {
     566            $cart = WC()->session->get( 'mnp-cart', null );
     567
     568            if ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true ) ) {
     569                $saved_cart[ 'cart' ] = $cart;
     570                update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), $saved_cart );
     571            }
     572
     573            WC()->cart->empty_cart( false );
     574            WC()->session->set( 'cart', $cart );
     575            WC()->cart->get_cart_from_session();
     576
     577            WC()->session->set( 'mnp-cart', null );
     578        }
     579        public static function save_cart_contents( $order_key, $cart ) {
     580            if ( 'yes' == get_option( 'mnp-use-cart-management', 'yes' ) ) {
     581                do_action( 'mnp_before_save_cart_contents', $order_key, $cart );
     582                set_transient( 'mnp_' . $order_key, $cart->get_cart(), 6 * HOUR_IN_SECONDS );
     583                $coupons = array();
     584                foreach ( $cart->get_coupons() as $code => $coupon ) {
     585                    // Avoid storing used_by - it's not needed and can get large.
     586                    $coupon_data = $coupon->get_data();
     587                    unset( $coupon_data[ 'used_by' ] );
     588
     589                    $coupons[ $code ] = array(
     590                        'code'         => $code,
     591                        'discount'     => $cart->get_coupon_discount_amount( $code ),
     592                        'discount_tax' => $cart->get_coupon_discount_tax_amount( $code ),
     593                        'coupon_data'  => $coupon_data
     594                    );
     595                }
     596
     597                set_transient( 'mnp_coupons_' . $order_key, $coupons, 6 * HOUR_IN_SECONDS );
     598
     599                do_action( 'mnp_after_save_cart_contents', $order_key, $cart );
     600            }
     601        }
     602        public static function search_cart_item( $product_id, $merchant_product_id, $cart = null ) {
     603
     604            if ( is_null( $cart ) ) {
     605                $cart = WC()->cart;
     606            }
     607
     608            if ( is_numeric( $merchant_product_id ) ) {
     609                $_product_id = $merchant_product_id;
     610            } else {
     611                $_product_id = $product_id;
     612
     613                $wc_product = wc_get_product( $product_id );
     614
     615                if ( ! $wc_product ) {
     616                    $_product_id = wc_get_product_id_by_sku( $product_id );
     617                }
     618            }
     619
     620            if ( is_callable( array( $cart, 'get_cart_contents' ) ) ) {
     621                $cart_contents = $cart->get_cart_contents();
     622            } else {
     623                $cart_contents = $cart->cart_contents;
     624            }
     625
     626            foreach ( $cart_contents as $cart_item_key => $cart_item ) {
     627                if ( apply_filters( 'mnp_search_cart_item_by_cart_item_key', false, $cart_item ) ) {
     628                    if ( $product_id == apply_filters( 'mnp_get_product_id_from_cart_item_key', '', $cart_item_key, $cart_item ) ) {
     629                        return $cart_item;
     630                    }
     631                } elseif ( is_array( $cart_item ) && ( $_product_id == mnp_get( $cart_item, 'product_id' ) || $_product_id == mnp_get( $cart_item, 'variation_id' ) ) ) {
     632                    return $cart_item;
     633                }
     634            }
     635
     636            return null;
     637        }
     638        public static function get_attribute_slug( $product, $label ) {
     639            $attributes = $product->get_variation_attributes();
     640
     641            foreach ( $attributes as $slug => $values ) {
     642                MNP_Logger::add_log( sprintf( "=== [%s] [%s]", $label, wc_attribute_label( $slug ) ) );
     643                if ( $label == wc_attribute_label( $slug ) ) {
     644                    return $slug;
     645                }
     646            }
     647
     648            return null;
     649        }
     650        public static function generate_cart( $npay_orders, $order = null ) {
     651            $cart = WC()->cart;
     652
     653            do_action( 'mnp_before_generate_cart', $npay_orders, $order );
     654
     655            $cart->empty_cart( false );
     656
     657            if ( is_null( $order ) ) {
     658                $cart_contents = mnp_load_saved_cart_contents_from_npay_order( current( $npay_orders ) );
     659            } else {
     660                $cart_contents = mnp_load_saved_cart_contents_from_order( $order );
     661            }
     662
     663            foreach ( $npay_orders as $npay_order ) {
     664                if ( apply_filters( 'mnp_generate_cart_skip_npay_order', false, $npay_order ) ) {
     665                    continue;
     666                }
     667
     668                if ( property_exists( $npay_order->ProductOrder, 'OptionManageCode' ) ) {
     669                    $product_id = apply_filters( 'mnp_get_product_id_from_option_manage_code', $npay_order->ProductOrder->OptionManageCode, $npay_order );
     670                } else {
     671                    $product_id = $npay_order->ProductOrder->SellerProductCode;
     672                }
     673
     674                $product = wc_get_product( $product_id );
     675
     676                if ( $product ) {
     677                    if ( $product->is_type( 'variation' ) ) {
     678                        $product_id   = $product->get_parent_id();
     679                        $variation_id = $product->get_id();
     680                    } else {
     681                        $product_id   = $product->get_id();
     682                        $variation_id = '0';
     683                    }
     684
     685                    $np_product_id = $npay_order->ProductOrder->ProductID;
     686                    $quantity      = $npay_order->ProductOrder->Quantity;
     687
     688                    if ( ! empty( $cart_contents ) ) {
     689                        foreach ( $cart_contents as $cart_item_key => &$cart_item ) {
     690                            if ( empty( $cart_item[ '_npay_order' ] ) ) {
     691                                if ( apply_filters( 'mnp_search_cart_item_by_cart_item_key', false, $cart_item ) ) {
     692                                    $searched_product_id = apply_filters( 'mnp_get_product_id_from_cart_item_key', '', $cart_item_key, $cart_item );
     693
     694                                    if ( $npay_order->ProductOrder->ProductID == $searched_product_id || ( property_exists( $npay_order->ProductOrder, 'OptionManageCode' ) && $npay_order->ProductOrder->OptionManageCode == $searched_product_id ) ) {
     695                                        $cart_item[ '_npay_product_order_id' ]     = $npay_order->ProductOrder->ProductOrderID;
     696                                        $cart_item[ '_npay_product_order_status' ] = $npay_order->ProductOrder->ProductOrderStatus;
     697                                        $cart_item[ '_npay_order' ]                = json_encode( $npay_order, JSON_UNESCAPED_UNICODE );
     698                                        break;
     699                                    }
     700                                } elseif ( $cart_item[ 'product_id' ] == $product_id && $cart_item[ 'variation_id' ] == $variation_id && $cart_item[ 'quantity' ] == $quantity ) {
     701                                    $cart_item[ '_npay_product_order_id' ]     = $npay_order->ProductOrder->ProductOrderID;
     702                                    $cart_item[ '_npay_product_order_status' ] = $npay_order->ProductOrder->ProductOrderStatus;
     703                                    $cart_item[ '_npay_order' ]                = json_encode( $npay_order, JSON_UNESCAPED_UNICODE );
     704                                    break;
     705                                }
     706                            }
     707                        }
     708
     709                        if ( $product->is_type( 'diy-bundle' ) ) {
     710                            $bundle_product_order_ids = array();
     711
     712                            foreach ( $product->get_bundle_products() as $bundle_product ) {
     713                                $bundle_product_id = $bundle_product[ 'id' ];
     714
     715                                foreach ( $npay_orders as $_npay_order ) {
     716
     717                                    if ( property_exists( $_npay_order->ProductOrder, 'OptionManageCode' ) && 0 === strpos( $_npay_order->ProductOrder->OptionManageCode, 'msdp_bundle_' ) ) {
     718                                        if ( $_npay_order->ProductOrder->MerchantProductId == $product_id && apply_filters( 'mnp_get_product_id_from_option_manage_code', $_npay_order->ProductOrder->OptionManageCode, $_npay_order ) == $bundle_product_id ) {
     719                                            $bundle_product_order_ids[] = $_npay_order->ProductOrder->ProductOrderID;
     720                                        }
     721                                    }
     722                                }
     723                            }
     724
     725                            $cart_item[ '_npay_bundle_product_order_ids' ] = implode( ',', $bundle_product_order_ids );
     726
     727                        }
     728                    } else {
     729                        add_filter( 'woocommerce_add_cart_item', array( __CLASS__, 'maybe_update_cart_item_price' ), 10, 2 );
     730                        remove_action( 'woocommerce_after_calculate_totals', array( 'MSMS_Cart', 'maybe_apply_membership_coupons' ) );
     731                        $variations = array();
     732                        if ( ! empty( $npay_order->ProductOrder->ProductOption ) ) {
     733                            $parent_product = wc_get_product( $product->get_parent_id() );
     734                            $options        = explode( '/', $npay_order->ProductOrder->ProductOption );
     735                            foreach ( $options as $option ) {
     736                                $values      = explode( ':', $option );
     737                                $values[ 0 ] = trim( $values[ 0 ] );
     738                                $values[ 1 ] = trim( $values[ 1 ] );
     739
     740                                if ( $parent_product ) {
     741                                    $slug = self::get_attribute_slug( $parent_product, $values[ 0 ] );
     742                                    $term = get_term_by( 'name', $values[ 1 ], $slug );
     743                                }
     744
     745                                if ( $parent_product && ! empty( $slug ) ) {
     746                                    if ( is_object( $term ) ) {
     747                                        $variations[ 'attribute_' . $slug ] = $term->slug;
     748                                    } else {
     749                                        $variations[ 'attribute_' . $slug ] = apply_filters( 'mnp_product_attributes_label', $values[ 1 ], $slug, $parent_product );
     750                                    }
     751                                } else {
     752                                    $variations[ $values[ 0 ] ] = $values[ 1 ];
     753                                }
     754                            }
     755                        }
     756
     757                        $product_info = array(
     758                            'product_id'    => $product_id,
     759                            'np_product_id' => $np_product_id,
     760                            'price'         => apply_filters( 'mnp_get_product_price_by_id', $npay_order->ProductOrder->UnitPrice, $np_product_id, $product_id, $npay_order )
     761                        );
     762
     763                        $variations     = apply_filters( 'mnp_get_product_variations', $variations, $product_info, $npay_order );
     764                        $cart_item_data = apply_filters( 'mnp_get_product_cart_item_data', array(
     765                            '_npay_product_order_id'     => $npay_order->ProductOrder->ProductOrderID,
     766                            '_npay_product_order_status' => $npay_order->ProductOrder->ProductOrderStatus,
     767                            '_npay_order'                => json_encode( $npay_order, JSON_UNESCAPED_UNICODE ),
     768                            '_npay_price'                => $npay_order->ProductOrder->UnitPrice
     769                        ), $product_info, $npay_order );
     770
     771                        $cart->add_to_cart( $product_id, $quantity, $variation_id, $variations, $cart_item_data );
     772
     773                        add_action( 'woocommerce_after_calculate_totals', array(
     774                            'MSMS_Cart',
     775                            'maybe_apply_membership_coupons'
     776                        ) );
     777                        remove_filter( 'woocommerce_add_cart_item', array(
     778                            __CLASS__,
     779                            'maybe_update_cart_item_price'
     780                        ), 10 );
     781                    }
     782
     783                }
     784            }
     785            foreach ( $cart_contents as $cart_content_key => $cart_content ) {
     786                if ( empty( $cart_content[ '_npay_order' ] ) ) {
     787                    unset( $cart_contents[ $cart_content_key ] );
     788                }
     789            }
     790
     791            if ( ! empty( $cart_contents ) ) {
     792                if ( is_callable( array( WC()->cart, 'set_cart_contents' ) ) ) {
     793                    $cart->set_cart_contents( $cart_contents );
     794                } else {
     795                    $cart->cart_contents = $cart_contents;
     796                }
     797            }
     798
     799            do_action( 'mnp_after_generate_cart', $npay_orders, $order );
     800        }
     801
     802        public static function maybe_update_cart_item_price( $cart_item_data, $cart_item_key ) {
     803            if ( isset( $cart_item_data[ '_npay_price' ] ) ) {
     804                $cart_item_data[ 'data' ]->set_price( floatval( $cart_item_data[ '_npay_price' ] ) );
     805            }
     806
     807            return $cart_item_data;
     808        }
    841809
    842810        public static function maybe_output_npay_button_image() {
    843811            wc_get_template( 'single-product/naverpay-button-image.php', array(), '', MNP()->template_path() );;
    844812        }
    845     }
     813    }
    846814}
    847815
  • mshop-npay/trunk/languages/mshop-npay.pot

    r3432334 r3464694  
    33msgid ""
    44msgstr ""
    5 "Project-Id-Version: mshop-npay 3.6.3\n"
     5"Project-Id-Version: mshop-npay 3.6.4\n"
    66"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/mshop-npay\n"
    7 "POT-Creation-Date: 2026-01-05 00:55:30+00:00\n"
     7"POT-Creation-Date: 2026-02-19 00:53:09+00:00\n"
    88"MIME-Version: 1.0\n"
    99"Content-Type: text/plain; charset=utf-8\n"
  • mshop-npay/trunk/mshop-npay.php

    r3432334 r3464694  
    44Plugin URI:
    55Description: 코드엠샵에서 개발, 운영되는 네이버페이 - 주문형 결제 시스템으로 네이버페이센터 어드민과 연동합니다.
    6 Version: 3.6.3
     6Version: 3.6.4
    77Author: CodeMShop
    88Author URI: www.codemshop.com
     
    2020
    2121        protected static $_instance = null;
    22         public $version = '3.6.3';
     22        public $version = '3.6.4';
    2323        public $plugin_url;
    2424        public $naverpay_register_order_url;
  • mshop-npay/trunk/readme.txt

    r3432334 r3464694  
    66Tested up to: 6.9
    77Requires PHP: 7.0
    8 Stable tag: 3.6.3
     8Stable tag: 3.6.4
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    7979
    8080== Changelog ==
     81
     82= 3.6.4 =
     83네이버페이 콜백 처리 시 PHP Warning 메시지 제거
    8184
    8285= 3.6.3 =
Note: See TracChangeset for help on using the changeset viewer.