简体   繁体   中英

Remove WooCommerce Marketing Menu Item (wp-admin) Using Hook

With the new WooCommerce 4.1.0 just released, there's a new Marketing item in the menu. Looking at the URL, it goes to admin.php?page=wc-admin&path=/marketing and by using the admin_menu hook, I'm trying to remove this menu option.

I tried with the sub_menu option and with remove_menu_page option without success. If anyone can correct my code I would be very grateful.

add_action( 'admin_menu', 'remove_woocommerce_marketing_menu_option' );
function remove_woocommerce_marketing_menu_option(){
remove_menu_page( 'admin.php?page=wc-admin&path=/marketing' );
}

For WooCommerce <= v4.2

// Remove Marketing Hub menu item
add_filter( 'woocommerce_marketing_menu_items', '__return_empty_array' );

For WooCommerce >= v4.3

WooCommerce 4.3 removed the woocommerce_marketing_menu_items filter so the above snippet will no longer work. Thankfully, we can hook into another filter introduced in WooCommerce 4.0 as such:

add_filter( 'woocommerce_admin_features', function( $features ) {
    /**
     * Filter list of features and remove those not needed     *
     */
    return array_values(
        array_filter( $features, function($feature) {
            return $feature !== 'marketing';
        } ) 
    );
} );

Is the code working?

I have tested the above code snippet on WordPress version 5.5 and WooCommerce version 4.4 and it works as expected.

Coupons moved under Marketing in WooCommerce 4.4

In WooCommerce version 4.4, the Coupons feature was moved to the Marketing menu item as sub-item. There will still be a coupon menu item in the old location, and anyone stumbling upon it will be guided on the new location and asked to remove the legacy WooCommerce > Coupons menu item.

However, if you are using the above code snippet, your Marketing , and so, the Marketing > Coupons menu items will be not available. In that case, your Coupons menu item will reside in the old place as WooCommerce > Coupons .

Credits:https://cinchws.com/remove-woocommerce-marketing-hub-menu-item/

add_filter('woocommerce_marketing_menu_items', 'woocommerce_marketing_menu_items');

function woocommerce_marketing_menu_items($marketing_pages){
    return array();
}

Use this For WooCommerce Version <= 4.2:

add_action( 'admin_init', 'remove_marketing_menu_page' );

function remove_marketing_menu_page() {
    remove_menu_page( 'wc-admin&path=/marketing' );
}

For WooCommerce Version >= 4.3 Use this:

add_filter( 'woocommerce_admin_features', function( $features ) {
    return array_values(
        array_filter( $features, function($feature) {
            return $feature !== 'marketing';
        } ) 
    );
} );
// Remove Marketing Hub menu item
add_filter( 'woocommerce_marketing_menu_items', '__return_empty_array' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM