Categories -
WordPress Development
Woocommerce
Tags -
WordPress Plugin Development
WordPress
Woocommerce
Maniruzzaman Akash
1 year ago
577
3 minutes
0
Woocommerce New Features Navigation Breadcrumb Add
- Add a filter to add breadcrumb for new Woocommerce Admin navigation
- Full Example with a Menu Registration in Woocommerce and then add breadcrumb
¶Add a filter to add breadcrumb for new Woocommerce Admin navigation
¶Solve in OOP Way
<?php
class Menu {
public function __construct() {
add_filter( 'woocommerce_navigation_get_breadcrumbs', [ $this, 'add_breadcrumb_for_new_navigation' ] );
}
/**
* Filter breadcrumbs for core pages that aren't explicitly connected.
*
* @param array $breadcrumbs Breadcrumb pieces.
*
* @return array Filtered breadcrumb pieces.
*/
public function add_breadcrumb_for_new_navigation( $breadcrumbs ) {
if ( ! $this->is_new_navigation_enabled() ) {
return;
}
// Check if current screen is for CartPulse.
$screen = get_current_screen();
// Our page screen id = woocommerce_page_cart-pulse
// If slug = cart-pulse, then id would be woocommerce_page_cart-pulse
if ( ! empty( $screen->id ) && $screen->id !== 'woocommerce_page_cart-pulse' ) {
return $breadcrumbs;
}
$breadcrumbs[] = __('Cart Pulse', 'cp'); // Here Cart Pulse is plugin breadcrumb name.
return $breadcrumbs;
}
/**
* Checks if the woocommerce new features navigation is activated or not.
*
* @see https://developer.woocommerce.com/extension-developer-guide/registering-with-woocommerce-admin-navigation/
*
* @return bool
*/
private function is_new_navigation_enabled() {
// Check first if the new features classes exists
if (
! class_exists( 'Automattic\WooCommerce\Admin\Features\Navigation\Menu' ) ||
! class_exists( 'Automattic\WooCommerce\Admin\Features\Navigation\Screen' ) ||
! class_exists( 'Automattic\WooCommerce\Admin\Features\Features' )
) {
return false;
}
// Check if new featured navigation is enabled or not.
if ( ! \Automattic\WooCommerce\Admin\Features\Features::is_enabled( 'navigation' ) ) {
return false;
}
return true;
}
}
¶Full Example with a Menu Registration in Woocommerce and then add breadcrumb
<?php
namespace WeDevs\CartPulse\Admin;
/**
* Admin Menu Class.
*
* Handles menu registration for Admin menubar.
*/
class Menu {
/**
* Menu constructor.
*
* @return void
*/
public function __construct() {
add_action( 'admin_menu', [ $this, 'register_admin_menu' ] );
add_filter( 'woocommerce_navigation_get_breadcrumbs', [ $this, 'add_breadcrumb_for_new_navigation' ] );
}
/**
* Register our menu page.
*
* @see https://developer.woocommerce.com/extension-developer-guide/registering-with-woocommerce-admin-navigation/
*
* @return void
*/
public function register_admin_menu() {
$slug = 'cart-pulse';
$capability = cp_admin_menu_capability();
// Add submenu page.
add_submenu_page( 'woocommerce', esc_html__( 'CartPulse', 'cp' ), esc_html__( 'CartPulse', 'cp' ), $capability, $slug, [ $this, 'plugin_page' ] );
/**
* If woocommerce featured navigation is not enabled,
* Then no need to process further.
* Register it as a normal submenu under Woocommerce
*
* Otherwise, register this submenu to new Featured Navigation menu.
*/
if ( ! $this->is_new_navigation_enabled() ) {
return;
}
$menu_item = [
'id' => $slug,
'title' => __( 'CartPulse', 'cp' ),
'capability' => $capability,
'url' => $slug,
'screen_id' => wc_get_page_screen_id( $slug ),
'path' => 'admin.php?page=' . $slug,
];
// Add the plugin to the extension sidebar menu.
\Automattic\WooCommerce\Admin\Features\Navigation\Menu::add_plugin_item( $menu_item );
}
/**
* Filter breadcrumbs for core pages that aren't explicitly connected.
*
* @param array $breadcrumbs Breadcrumb pieces.
*
* @return array Filtered breadcrumb pieces.
*/
public function add_breadcrumb_for_new_navigation( $breadcrumbs ) {
if ( ! $this->is_new_navigation_enabled() ) {
return;
}
// Check if current screen is for CartPulse.
$screen = get_current_screen();
if ( ! empty( $screen->id ) && $screen->id !== 'woocommerce_page_cart-pulse' ) {
return $breadcrumbs;
}
$breadcrumbs[] = __('CartPulse', 'cp');
return $breadcrumbs;
}
/**
* Checks if the woocommerce new features navigation is activated or not.
*
* @see https://developer.woocommerce.com/extension-developer-guide/registering-with-woocommerce-admin-navigation/
*
* @return bool
*/
private function is_new_navigation_enabled() {
// Check first if the new features classes exists
if (
! class_exists( 'Automattic\WooCommerce\Admin\Features\Navigation\Menu' ) ||
! class_exists( 'Automattic\WooCommerce\Admin\Features\Navigation\Screen' ) ||
! class_exists( 'Automattic\WooCommerce\Admin\Features\Features' )
) {
return false;
}
// Check if new featured navigation is enabled or not.
if ( ! \Automattic\WooCommerce\Admin\Features\Features::is_enabled( 'navigation' ) ) {
return false;
}
return true;
}
/**
* Load Plugin Template files.
*
* @return void
*/
public function plugin_page() {
// require_once CART_PULSE_TEMPLATE_PATH . '/app.php';
?>
<div>
Your Plugin code...
or you can use a template file like above
</div>
<?php
}
}
Tags: Woocommerce New Features Navigation Breadcrumb Add, woocommerce new navigation breadcrumb, woocommerce navigation, woocommerce features, woocommerce new menu register.
Previous
PHP If-else-elseif and Switch-case
PHP If-else-elseif and Switch-case
Next
PHP String Functions - All necessary String functions in PHP to manage strings better.
PHP String Functions - All necessary String functions in PHP to manage strings better.
Advertisements
Popular Tutorials
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development
Advertisements