From 74aaf42227d1b8c2cd183838c09fb38119806861 Mon Sep 17 00:00:00 2001 From: imisuchenko Date: Mon, 14 Oct 2024 12:24:59 +0300 Subject: [PATCH 1/4] personal page layout --- .../themes/mytheme/personal-account.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/public/wp-content/themes/mytheme/personal-account.php diff --git a/src/public/wp-content/themes/mytheme/personal-account.php b/src/public/wp-content/themes/mytheme/personal-account.php new file mode 100644 index 0000000..d8f567a --- /dev/null +++ b/src/public/wp-content/themes/mytheme/personal-account.php @@ -0,0 +1,29 @@ + + + + +
+

+
+ +
+
+
+ + +
+
+ + +
+
+
+ +
+
+ + \ No newline at end of file From 8480dc28ebec4b186904c3fc2dd18606b6cd328e Mon Sep 17 00:00:00 2001 From: imisuchenko Date: Mon, 14 Oct 2024 14:48:46 +0300 Subject: [PATCH 2/4] hidding personal page in menu --- .../wp-content/themes/mytheme/functions.php | 16 ++++++++++++++-- src/public/wp-content/themes/mytheme/style.css | 4 ++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/public/wp-content/themes/mytheme/functions.php b/src/public/wp-content/themes/mytheme/functions.php index 82fae4b..18a3773 100644 --- a/src/public/wp-content/themes/mytheme/functions.php +++ b/src/public/wp-content/themes/mytheme/functions.php @@ -378,7 +378,7 @@ function login_modal() $user = wp_signon($creds); if (is_wp_error($user)) { echo json_encode([1, $user->get_error_message()]); - } else{ + } else { echo json_encode([0, $user->user_login]); } wp_die(); @@ -388,4 +388,16 @@ function logout() { wp_logout(); wp_die(); -} \ No newline at end of file +} + +add_filter('nav_menu_css_class', 'chek_for_personal_page', 10, 4); + +function chek_for_personal_page( $classes, $item, $args, $depth ) +{ + if ($item->ID === 56 && $args->theme_location === 'header_menu' && !is_user_logged_in()) { + $classes[] = 'hidden-menu-item'; + } + + return $classes; +} + diff --git a/src/public/wp-content/themes/mytheme/style.css b/src/public/wp-content/themes/mytheme/style.css index 28f8c5b..bb7e3d2 100644 --- a/src/public/wp-content/themes/mytheme/style.css +++ b/src/public/wp-content/themes/mytheme/style.css @@ -38,6 +38,10 @@ li.menu-item a { text-decoration: none; } +.hidden-menu-item{ + display: none !important; +} + .cart-header-container, .header-cart { display: flex; From 702d45d3be1ca64e3215151485f96e8f1163731a Mon Sep 17 00:00:00 2001 From: imisuchenko Date: Thu, 17 Oct 2024 14:55:26 +0300 Subject: [PATCH 3/4] REST API handling via JS file --- .../wp-content/themes/mytheme/functions.php | 54 ++++++++++++++++++- .../wp-content/themes/mytheme/orders.js | 20 +++++++ .../themes/mytheme/personal-account.php | 7 +-- 3 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 src/public/wp-content/themes/mytheme/orders.js diff --git a/src/public/wp-content/themes/mytheme/functions.php b/src/public/wp-content/themes/mytheme/functions.php index 18a3773..8f0482d 100644 --- a/src/public/wp-content/themes/mytheme/functions.php +++ b/src/public/wp-content/themes/mytheme/functions.php @@ -1,5 +1,48 @@ .+)'; + $route_params = [ + 'methods' => 'GET', + 'callback' => 'get_user_orders', + 'args' => [ + 'email' => [ + 'type' => 'string', + 'required' => true, + ], + ], + 'permission_callback' => null, + function ($request) { + return is_user_logged_in(); + }, + ]; + + register_rest_route($namespace, $route, $route_params); +}); + +function get_user_orders(WP_REST_Request $request) +{ + + $orders = get_posts([ + 'post_type' => 'custom_order', + 'meta_key' => 'email', + 'meta_value' => $request['email'], + ]); + + if (empty($orders)) { + return new WP_Error('no_user_orders', 'Заказы не найдены', ['status' => 404]); + } + + // $meta = []; + // foreach ($orders as $order){ + // $meta[] = get_post_meta($order->ID, 'products', false); + // } + + return $orders; +} + require_once __DIR__ . '/product-rating-widget.php'; function theme_add_bootstrap() @@ -17,6 +60,7 @@ function theme_add_bootstrap() wp_enqueue_script('cart', get_template_directory_uri() . '/cart.js'); wp_enqueue_script('product-rating', get_template_directory_uri() . '/product-rating.js'); wp_enqueue_script('register-login', get_template_directory_uri() . '/register-login.js'); +wp_enqueue_script('orders', get_template_directory_uri() . '/orders.js'); $cart_ajax_data = [ 'url' => admin_url('admin-ajax.php'), @@ -52,6 +96,8 @@ function theme_add_bootstrap() add_action('wp_ajax_nopriv_login_modal', 'login_modal'); add_action('wp_ajax_logout', 'logout'); +add_action('wp_ajax_get_user_orders', 'get_user_orders'); + add_action('after_setup_theme', 'register_my_menu'); function register_my_menu() { @@ -392,7 +438,7 @@ function logout() add_filter('nav_menu_css_class', 'chek_for_personal_page', 10, 4); -function chek_for_personal_page( $classes, $item, $args, $depth ) +function chek_for_personal_page($classes, $item, $args, $depth) { if ($item->ID === 56 && $args->theme_location === 'header_menu' && !is_user_logged_in()) { $classes[] = 'hidden-menu-item'; @@ -401,3 +447,9 @@ function chek_for_personal_page( $classes, $item, $args, $depth ) return $classes; } +// function get_orders() +// { + + +// wp_die(); +// } diff --git a/src/public/wp-content/themes/mytheme/orders.js b/src/public/wp-content/themes/mytheme/orders.js new file mode 100644 index 0000000..46879ab --- /dev/null +++ b/src/public/wp-content/themes/mytheme/orders.js @@ -0,0 +1,20 @@ +jQuery(function ($) { + var email = $(".user-email > .user-info-value").text() + $.ajax({ + url: "http://localhost/wp-json/mytheme/v1/orders/" + email, + type: "GET", + success: function (response) { + console.log(response) + // const html = orders + // .map( + // (order) => + // `
` + + // `` + + // `
${order["post_type"]}$
` + + // `
` + // ) + // .join(""); + // document.querySelector(".page-content").innerHTML = html; + }, + }); +}); diff --git a/src/public/wp-content/themes/mytheme/personal-account.php b/src/public/wp-content/themes/mytheme/personal-account.php index d8f567a..b874073 100644 --- a/src/public/wp-content/themes/mytheme/personal-account.php +++ b/src/public/wp-content/themes/mytheme/personal-account.php @@ -14,16 +14,13 @@
- +
- +
-
- -
\ No newline at end of file From 2bb87def13c1ec5333c9ecc9e65b0d08113bc3f2 Mon Sep 17 00:00:00 2001 From: imisuchenko Date: Fri, 18 Oct 2024 13:47:47 +0300 Subject: [PATCH 4/4] get user orders - sketches --- .../wp-content/themes/mytheme/functions.php | 30 +++++++++++-------- .../wp-content/themes/mytheme/orders.js | 25 +++++++++------- .../themes/mytheme/personal-account.php | 2 ++ 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/public/wp-content/themes/mytheme/functions.php b/src/public/wp-content/themes/mytheme/functions.php index 8f0482d..7e9864b 100644 --- a/src/public/wp-content/themes/mytheme/functions.php +++ b/src/public/wp-content/themes/mytheme/functions.php @@ -35,12 +35,25 @@ function get_user_orders(WP_REST_Request $request) return new WP_Error('no_user_orders', 'Заказы не найдены', ['status' => 404]); } - // $meta = []; - // foreach ($orders as $order){ - // $meta[] = get_post_meta($order->ID, 'products', false); - // } + if (wp_get_current_user()->user_email === $request['email']) { + return new WP_Error('wrong_user_email', 'Неверный email', ['status' => 404]); + } + + $order_data = []; + + foreach ($orders as $order) { + $order_data['order_id'] = $order->ID; + $order_products_id = get_post_meta($order->ID, 'products', false); + $data = get_post_meta($order->ID, 'products_data', false); + $order_data['order_products_data'] = $data; + // foreach ($order_products_id as $product_id){ + // // $product = get_post($product_id); + // $order_data['order_product_name'] = get_post((int)$product_id); + // } + // $order_data['order_products_data'] = $order_products_id; + } - return $orders; + return var_dump($order_data); } require_once __DIR__ . '/product-rating-widget.php'; @@ -446,10 +459,3 @@ function chek_for_personal_page($classes, $item, $args, $depth) return $classes; } - -// function get_orders() -// { - - -// wp_die(); -// } diff --git a/src/public/wp-content/themes/mytheme/orders.js b/src/public/wp-content/themes/mytheme/orders.js index 46879ab..eeea619 100644 --- a/src/public/wp-content/themes/mytheme/orders.js +++ b/src/public/wp-content/themes/mytheme/orders.js @@ -5,16 +5,21 @@ jQuery(function ($) { type: "GET", success: function (response) { console.log(response) - // const html = orders - // .map( - // (order) => - // `
` + - // `` + - // `
${order["post_type"]}$
` + - // `
` - // ) - // .join(""); - // document.querySelector(".page-content").innerHTML = html; + const html = response + .map( + (order_data) => + ``+ + `ID` + + `type` + + `` + + ``+ + `${order_data["order_id"]}` + + `${order_data['order_products_data']}` + + // `${order_data["post_type"]}` + + `` + ) + .join(""); + document.querySelector(".user-orders").innerHTML = html; }, }); }); diff --git a/src/public/wp-content/themes/mytheme/personal-account.php b/src/public/wp-content/themes/mytheme/personal-account.php index b874073..90d6a7b 100644 --- a/src/public/wp-content/themes/mytheme/personal-account.php +++ b/src/public/wp-content/themes/mytheme/personal-account.php @@ -21,6 +21,8 @@ +

Your Orders

+
\ No newline at end of file