flex-ecommerce-store-pro.php
365 lines
| 1 | <?php |
| 2 | // new added for menue |
| 3 | if (!wp_get_nav_menu_object('Primary Menu')) { |
| 4 | fleximp_setup_primary_menu(); |
| 5 | } |
| 6 | |
| 7 | if (!wp_get_nav_menu_object('footer-menus')) { |
| 8 | fleximp_setup_footer_useful_links_menu(); |
| 9 | } |
| 10 | |
| 11 | if (!wp_get_nav_menu_object('category-menu')) { |
| 12 | fleximp_setup_footer_category_menu(); |
| 13 | } |
| 14 | |
| 15 | function fleximp_setup_primary_menu() { |
| 16 | $menuname = 'Primary Menu'; |
| 17 | $bpmenulocation = 'primary-menu'; |
| 18 | $menu_exists = wp_get_nav_menu_object($menuname); |
| 19 | |
| 20 | if (!$menu_exists) { |
| 21 | $menu_id = wp_create_nav_menu($menuname); |
| 22 | |
| 23 | $base_url = home_url('/'); |
| 24 | |
| 25 | // Step 1: Add Home, About, Products |
| 26 | $main_menu_items = array( |
| 27 | array( |
| 28 | 'title' => __('Home', 'flex-import'), |
| 29 | 'url' => $base_url, |
| 30 | ), |
| 31 | array( |
| 32 | 'title' => __('About', 'flex-import'), |
| 33 | 'url' => home_url('/index.php/about/') |
| 34 | ), |
| 35 | array( |
| 36 | 'title' => __('Products', 'flex-import'), |
| 37 | 'url' => home_url('/index.php/shop/') |
| 38 | ), |
| 39 | ); |
| 40 | |
| 41 | foreach ($main_menu_items as $item) { |
| 42 | wp_update_nav_menu_item($menu_id, 0, array( |
| 43 | 'menu-item-title' => $item['title'], |
| 44 | 'menu-item-url' => $item['url'], |
| 45 | 'menu-item-status' => 'publish', |
| 46 | 'menu-item-type' => 'custom', |
| 47 | )); |
| 48 | } |
| 49 | |
| 50 | // Step 2: Add Pages |
| 51 | $pages_item_id = wp_update_nav_menu_item($menu_id, 0, array( |
| 52 | 'menu-item-title' => __('Pages', 'flex-import'), |
| 53 | 'menu-item-url' => home_url('/index.php/pages/'), |
| 54 | 'menu-item-status' => 'publish', |
| 55 | 'menu-item-type' => 'custom', |
| 56 | )); |
| 57 | |
| 58 | // Add submenus under Pages |
| 59 | $sub_menu_items = array( |
| 60 | array( |
| 61 | 'title' => __('404 Page', 'flex-import'), |
| 62 | 'url' => home_url('/index.php/404/') |
| 63 | ), |
| 64 | array( |
| 65 | 'title' => __('FAQ', 'flex-import'), |
| 66 | 'url' => home_url('/index.php/faq/') |
| 67 | ) |
| 68 | ); |
| 69 | |
| 70 | foreach ($sub_menu_items as $sub_item) { |
| 71 | wp_update_nav_menu_item($menu_id, 0, array( |
| 72 | 'menu-item-title' => $sub_item['title'], |
| 73 | 'menu-item-url' => $sub_item['url'], |
| 74 | 'menu-item-status' => 'publish', |
| 75 | 'menu-item-type' => 'custom', |
| 76 | 'menu-item-parent-id' => $pages_item_id, |
| 77 | )); |
| 78 | } |
| 79 | |
| 80 | // Step 3: Add Blogs last |
| 81 | wp_update_nav_menu_item($menu_id, 0, array( |
| 82 | 'menu-item-title' => __('Blogs', 'flex-import'), |
| 83 | 'menu-item-url' => home_url('/index.php/blog/'), |
| 84 | 'menu-item-status' => 'publish', |
| 85 | 'menu-item-type' => 'custom', |
| 86 | )); |
| 87 | |
| 88 | // Assign menu location |
| 89 | if (!has_nav_menu($bpmenulocation)) { |
| 90 | $locations = get_theme_mod('nav_menu_locations'); |
| 91 | if (!is_array($locations)) { |
| 92 | $locations = array(); |
| 93 | } |
| 94 | $locations[$bpmenulocation] = $menu_id; |
| 95 | set_theme_mod('nav_menu_locations', $locations); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | // add_action('after_setup_theme', 'fleximp_setup_primary_menu'); |
| 100 | |
| 101 | function fleximp_setup_footer_useful_links_menu() { |
| 102 | $menuname = 'footer-menus'; |
| 103 | $bpmenulocation = 'footer-menus'; |
| 104 | $menu_exists = wp_get_nav_menu_object($menuname); |
| 105 | |
| 106 | if (!$menu_exists) { |
| 107 | $menu_id = wp_create_nav_menu($menuname); |
| 108 | |
| 109 | $main_menu_items = array( |
| 110 | array('title' => __('About Us', 'flex-import'), 'url' => home_url('/index.php/about/')), |
| 111 | array('title' => __('Products', 'flex-import'), 'url' => home_url('/index.php/shop/')), |
| 112 | array('title' => __('Blog', 'flex-import'), 'url' => home_url('/index.php/blog/')), |
| 113 | array('title' => __('Contact Us', 'flex-import'), 'url' => home_url('/index.php/contact/')), |
| 114 | array('title' => __('Terms & Condition', 'flex-import'), 'url' => home_url('/index.php/terms/')), |
| 115 | array('title' => __('Privacy and Policy', 'flex-import'), 'url' => home_url('/index.php/privacy-policy/')), |
| 116 | array('title' => __('Support', 'flex-import'), 'url' => home_url('/index.php/support/')), |
| 117 | ); |
| 118 | |
| 119 | foreach ($main_menu_items as $item) { |
| 120 | wp_update_nav_menu_item($menu_id, 0, array( |
| 121 | 'menu-item-title' => $item['title'], |
| 122 | 'menu-item-url' => $item['url'], |
| 123 | 'menu-item-status' => 'publish', |
| 124 | 'menu-item-type' => 'custom', |
| 125 | )); |
| 126 | } |
| 127 | |
| 128 | if (!has_nav_menu($bpmenulocation)) { |
| 129 | $locations = get_theme_mod('nav_menu_locations'); |
| 130 | if (!is_array($locations)) $locations = array(); |
| 131 | $locations[$bpmenulocation] = $menu_id; |
| 132 | set_theme_mod('nav_menu_locations', $locations); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | // add_action('after_setup_theme', 'fleximp_setup_footer_useful_links_menu'); |
| 137 | |
| 138 | function fleximp_setup_footer_category_menu() { |
| 139 | $menuname = 'category-menu'; |
| 140 | $bpmenulocation = 'category-menu'; |
| 141 | $menu_exists = wp_get_nav_menu_object($menuname); |
| 142 | |
| 143 | if (!$menu_exists) { |
| 144 | $menu_id = wp_create_nav_menu($menuname); |
| 145 | |
| 146 | $main_menu_items = array( |
| 147 | array('title' => __('Cameras & Accessories', 'flex-import'), 'url' => home_url('index.php/product-category/cameras-accessories/')), |
| 148 | array('title' => __('Cleaning & Home Care', 'flex-import'), 'url' => home_url('index.php/product-category/cleaning-home-care/')), |
| 149 | array('title' => __('Electric Vehicle Accessories', 'flex-import'), 'url' => home_url('index.php/product-category/electric-vehicle-ev-accessories/')), |
| 150 | array('title' => __('Electronics', 'flex-import'), 'url' => home_url('index.php/product-category/electronics/')), |
| 151 | ); |
| 152 | |
| 153 | foreach ($main_menu_items as $item) { |
| 154 | wp_update_nav_menu_item($menu_id, 0, array( |
| 155 | 'menu-item-title' => $item['title'], |
| 156 | 'menu-item-url' => $item['url'], |
| 157 | 'menu-item-status' => 'publish', |
| 158 | 'menu-item-type' => 'custom', |
| 159 | )); |
| 160 | } |
| 161 | |
| 162 | if (!has_nav_menu($bpmenulocation)) { |
| 163 | $locations = get_theme_mod('nav_menu_locations'); |
| 164 | if (!is_array($locations)) $locations = array(); |
| 165 | $locations[$bpmenulocation] = $menu_id; |
| 166 | set_theme_mod('nav_menu_locations', $locations); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | //end menue |
| 171 | |
| 172 | |
| 173 | // All Product Categories Shortcode |
| 174 | add_shortcode('all_product_categories', function () { |
| 175 | if (!class_exists('WooCommerce')) { |
| 176 | return 'WooCommerce not active.'; |
| 177 | } |
| 178 | |
| 179 | $args = array( |
| 180 | 'taxonomy' => 'product_cat', |
| 181 | 'orderby' => 'name', |
| 182 | 'order' => 'ASC', |
| 183 | 'number' => 100, |
| 184 | 'hide_empty' => false, |
| 185 | 'hierarchical' => true, |
| 186 | 'pad_counts' => false, |
| 187 | ); |
| 188 | |
| 189 | $product_categories = get_categories($args); |
| 190 | $output = ''; |
| 191 | |
| 192 | if (!empty($product_categories)) { |
| 193 | $output .= '<ul class="product-categories">'; |
| 194 | foreach ($product_categories as $category) { |
| 195 | $category_link = get_term_link($category); |
| 196 | $output .= '<li><a href="' . esc_url($category_link) . '">' . esc_html($category->name) . '</a></li>'; |
| 197 | } |
| 198 | $output .= '</ul>'; |
| 199 | } |
| 200 | |
| 201 | return $output; |
| 202 | }); |
| 203 | |
| 204 | |
| 205 | // Product Slider Shortcode |
| 206 | function custom_product_slider_shortcode($atts) { |
| 207 | ob_start(); |
| 208 | |
| 209 | $args = array( |
| 210 | 'post_type' => 'product', |
| 211 | 'posts_per_page' => 10, |
| 212 | ); |
| 213 | $loop = new WP_Query($args); |
| 214 | |
| 215 | if ($loop->have_posts()) { |
| 216 | ?> |
| 217 | <div class="custom-product-slider-wrapper" style="position: relative; margin-bottom: 40px;"> |
| 218 | <!-- Navigation Arrows for Default Slider --> |
| 219 | <div class="custom-slider-nav" style="position: absolute; top: -38px; right: 97px; z-index: 10; display: flex; gap: 10px;"> |
| 220 | <div class="swiper-prev-default swiper-button-prev"></div> |
| 221 | <div class="swiper-next-default swiper-button-next"></div> |
| 222 | </div> |
| 223 | <div class="custom-product-slider swiper swiper-default"> |
| 224 | <div class="swiper-wrapper "> |
| 225 | <?php while ($loop->have_posts()) : $loop->the_post(); |
| 226 | global $product; |
| 227 | $product_link = get_permalink(); |
| 228 | ?> |
| 229 | <div class="swiper-slide "> |
| 230 | <a href="<?php echo esc_url($product_link); ?>" class="product-slide" style="display:block; text-decoration:none; color:inherit; border:1px solid #ccc; padding:15px; border-radius:10px; background:#fff;"> |
| 231 | <div class="product-image"><?php echo wp_kses_post(woocommerce_get_product_thumbnail('medium')); ?></div> |
| 232 | <h3 class="product-title" style="font-size:18px; margin:10px 0; font-weight:600"><?php echo esc_html(get_the_title()); ?></h3> |
| 233 | <div class="product-excerpt" style="font-size:14px;"><?php echo esc_html(wp_trim_words(get_the_excerpt(), 10, '...')); ?></div> |
| 234 | <div class="product-price" style="color:#000; font-size:20px; font-weight:700;"><?php echo wp_kses_post($product->get_price_html()); ?></div> |
| 235 | <div class="product-rating"><?php echo wp_kses_post(wc_get_rating_html($product->get_average_rating())); ?></div> |
| 236 | </a> |
| 237 | </div> |
| 238 | <?php endwhile; ?> |
| 239 | </div> |
| 240 | </div> |
| 241 | </div> |
| 242 | <?php |
| 243 | } |
| 244 | |
| 245 | wp_reset_postdata(); |
| 246 | |
| 247 | return ob_get_clean(); |
| 248 | } |
| 249 | add_shortcode('custom_product_slider', 'custom_product_slider_shortcode'); |
| 250 | |
| 251 | // Product Slider with Category Filter Shortcode |
| 252 | function custom_product_slider_shortcode1($atts) { |
| 253 | ob_start(); |
| 254 | |
| 255 | // Category filter logic |
| 256 | $category = isset($atts['category']) ? $atts['category'] : 'cameras-accessories'; |
| 257 | $args = array( |
| 258 | 'post_type' => 'product', |
| 259 | 'posts_per_page' => 10, |
| 260 | 'tax_query' => array( |
| 261 | array( |
| 262 | 'taxonomy' => 'product_cat', |
| 263 | 'field' => 'slug', |
| 264 | 'terms' => $category, |
| 265 | ), |
| 266 | ), |
| 267 | ); |
| 268 | |
| 269 | $loop = new WP_Query($args); |
| 270 | |
| 271 | if ($loop->have_posts()) { |
| 272 | ?> |
| 273 | <div class="custom-product-slider1-wrapper" style="position: relative; margin-bottom: 40px;"> |
| 274 | <!-- Navigation Arrows for Category-based Slider --> |
| 275 | <div class="custom-slider-nav" style="position: absolute; top: -60px; right: 10px; z-index: 10; display: flex; gap: 10px;"> |
| 276 | <div class="swiper-prev-cat custom-swiper-prev"></div> |
| 277 | <div class="swiper-next-cat custom-swiper-next"></div> |
| 278 | </div> |
| 279 | <div class="custom-product-slider1 swiper swiper-cat"> |
| 280 | <div class="swiper-wrapper"> |
| 281 | <?php while ($loop->have_posts()) : $loop->the_post(); global $product; ?> |
| 282 | <div class="swiper-slide flex-swiper-slide"> |
| 283 | <a href="<?php echo esc_url(get_permalink()); ?>" class="product-slide" style="display:block; text-decoration:none; color:inherit; border:1px solid #ccc; padding:15px; border-radius:10px; background:#fff;"> |
| 284 | <div class="product-image"><?php echo wp_kses_post(woocommerce_get_product_thumbnail('medium')); ?></div> |
| 285 | <h3 class="product-title" style="font-size:18px; margin:10px 0; font-weight:600"><?php echo esc_html(get_the_title()); ?></h3> |
| 286 | <div class="product-excerpt" style="font-size:14px;"><?php echo esc_html(wp_trim_words(get_the_excerpt(), 10)); ?></div> |
| 287 | <div class="product-price" style="color:#000; font-size:20px; font-weight:700;"><?php echo wp_kses_post($product->get_price_html()); ?></div> |
| 288 | <div class="product-rating"> |
| 289 | <?php echo wp_kses_post(wc_get_rating_html($product->get_average_rating())); ?> |
| 290 | </div> |
| 291 | </a> |
| 292 | </div> |
| 293 | <?php endwhile; ?> |
| 294 | </div> |
| 295 | </div> |
| 296 | </div> |
| 297 | <?php |
| 298 | } |
| 299 | |
| 300 | wp_reset_postdata(); |
| 301 | return ob_get_clean(); |
| 302 | } |
| 303 | add_shortcode('custom_product_slider1', 'custom_product_slider_shortcode1'); |
| 304 | |
| 305 | // contact form |
| 306 | |
| 307 | function create_subscribe_form_once() { |
| 308 | // Only run this once |
| 309 | if (get_option('fleximp_subscribe_form_created')) { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | $title = 'Subscribe Form'; |
| 314 | $hash = '37a3fb3'; |
| 315 | |
| 316 | $existing_form = get_page_by_title($title, OBJECT, 'wpcf7_contact_form'); |
| 317 | if ($existing_form) { |
| 318 | update_option('fleximp_subscribe_form_created', true); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | // Form HTML |
| 323 | $form_content = '<div class="flex-subscribe-form"> |
| 324 | <div class="flex-subscribe-input"> |
| 325 | [email* your-email placeholder "Email Address"] |
| 326 | </div> |
| 327 | <div class="flex-subscribe-submit"> |
| 328 | [submit "Submit Now"] |
| 329 | </div> |
| 330 | </div>'; |
| 331 | |
| 332 | $form_post = array( |
| 333 | 'post_title' => wp_strip_all_tags($title), |
| 334 | 'post_content' => $form_content, |
| 335 | 'post_status' => 'publish', |
| 336 | 'post_type' => 'wpcf7_contact_form', |
| 337 | ); |
| 338 | |
| 339 | $form_id = wp_insert_post($form_post); |
| 340 | if (is_wp_error($form_id) || !$form_id) { |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | update_post_meta($form_id, '_form', $form_content); |
| 345 | |
| 346 | // Mail settings |
| 347 | $mail_data = array( |
| 348 | 'subject' => 'New Subscribe Form Submission', |
| 349 | 'sender' => '[_site_title] <support@yourdomain.com>', |
| 350 | 'body' => 'Subscriber Email: [your-email]', |
| 351 | 'recipient' => '[_site_admin_email]', |
| 352 | 'additional_headers' => 'Reply-To: [your-email]', |
| 353 | 'attachments' => '', |
| 354 | 'use_html' => 1, |
| 355 | 'exclude_blank' => 0 |
| 356 | ); |
| 357 | |
| 358 | update_post_meta($form_id, '_mail', $mail_data); |
| 359 | update_post_meta($form_id, '_hash', $hash); |
| 360 | update_option('fleximp_subscribe_form_created', true); |
| 361 | } |
| 362 | create_subscribe_form_once(); |
| 363 | |
| 364 | |
| 365 |