Add submenu entry to WooCommerce "Products" admin menu - php

I would like to add a submenu entry under the WooCommerce "Products" admin menu. Does anybody know what the $parent_slug for this menu is?
I can add a submenu item to the "WooCommerce" menu using add_submenu_page and 'woocommerce' for $parent_slug (via the admin_menu hook), but can't seem to figure out what the $parent_slug for the Products menu is...
if ( is_admin() ) {
add_action( 'admin_menu', 'add_products_menu_entry', 100 );
}
function add_products_menu_entry() {
add_submenu_page(
'woocommerce-product', // This is what I can't figure out
__( 'Product Grabber' ),
__( 'Grab New' ),
'manage_woocommerce', // Required user capability
'ddg-product',
'generate_grab_product_page'
);
}
function generate_grab_product_page() {
// Page generation code will go here
}
WooCommerce Products Admin Menu

Got it, it was edit.php?post_type=product
if ( is_admin() ) {
add_action( 'admin_menu', 'add_products_menu_entry', 100 );
}
function add_products_menu_entry() {
add_submenu_page(
'edit.php?post_type=product',
__( 'Product Grabber' ),
__( 'Grab New' ),
'manage_woocommerce', // Required user capability
'ddg-product',
'generate_grab_product_page'
);
}
function generate_grab_product_page() {
echo "<h2>Hello, it worked! :-)</h2>";
}
Thank-you to Derick Rethans / XDebug!

Related

Wordpress submenu is blank

I've been willing to add a submenu to an existing plugin (school project), except that i can see it in the menu, but anytime i open it the page is empty.
Anyone know why it might does this?
static function admin_menu() {
add_menu_page(
_x( 'Foyer', 'plugin name in admin menu', 'foyer' ),
_x( 'Foyer', 'plugin name in admin menu', 'foyer' ),
'edit_posts',
'foyer',
array(),
'dashicons-welcome-view-site',
31
);
add_submenu_page(
'tools.php',
'Foyer Settings', //page title
'Settings', //menu title
'edit_plugins', //capability,
'foyer-settings',//menu slug
'my_custom_submenu_page_content'//callback function
);
}
function my_custom_submenu_page_content() {
echo '<div class="wrap">';
echo '<h2>Page Title</h2>';
echo '</div>';
}

Add custom link as a sub menu, below WooCommerce in Admin Area

I'm trying to add a custom link as a submenu, below WooCommerce, under "Orders".
I've tried using the following code, but I'm missing something. How do I add a custom link?
add_action('admin_menu', 'register_my_custom_submenu_page');
function register_my_custom_submenu_page() {
add_submenu_page( 'woocommerce', 'My Custom Submenu Page', 'My Custom Submenu Page',
'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback' );
}
function my_custom_submenu_page_callback() {
echo '<h3>My Custom Submenu Page</h3>';
}
I used the following code to add a custom link, under WooCommerce:
// Adding custom sub menu
add_action('admin_menu', 'add_custom_link_into_appearnace_menu');
function add_custom_link_into_appearnace_menu() {
global $submenu;
$permalink = 'http://www.cusomtlink.com';
$submenu['woocommerce'][] = array( 'POS Orders', 'manage_options', $permalink );
}

How to create a new page in Wordpress Admin for Editing the Details

Can you please help me with my proble regarding creating a New Page for Editing Details.
Currently, I have a list of Properties. This is a Custom Table not a post type. I already listed the properties but my problem is how am I gonna make the Edit Details Page?
add_action( 'admin_menu', 'pp_create_property_management_menu' ); function pp_create_property_management_menu() {
add_menu_page( 'PP M', 'PP M', 'manage_options', 'property-management', 'pp_property_management_page', 'dashicons-tickets', 5 ); }
Many thanks,
The fifth parameter in the add_menu_page() function calls a function to output the contents of the menu page. Here's an example I have taken from the WordPress site on how to add a custom admin menu and show the details of the page.
add_action( 'admin_menu', 'my_plugin_menu' );
function my_plugin_menu() {
add_menu_page( 'My Page Title', 'My Menu Title', 'manage_options', 'my-unique-identifier', 'my_plugin_options' );
}
function my_plugin_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
echo '<div class="wrap">';
echo '<p>Put all your options in here</p>';
echo '</div>';
}

Directing to filtered pages from Wordpress Admin Sub Menus

I want to add two additional sub-menu items to my Wordpress Admin menu. The top-level menu I want to hook into is the 'Products' menu created by WooCommerce.
edit.php?post_type=product
The content I want the menu items to show can be accessed by filtering the products by Product Category. E.g.
http://dev3.benefacto.org/wp-admin/edit.php?s&post_type=product&product_cat=manchester
I've come up with a working solution (below) to do this - but it is inelegant because it requires calling a function when I feel like I should be able to simply add something into the 'menu slug' variable perhaps.
Any thoughts much appreciated.
// Hook into the Admin Menu
add_action( 'admin_menu', 'lnz_wp_adminmenu_addproductpages' );
// Add Product Categories
function lnz_wp_adminmenu_addproductpages() {
add_submenu_page( 'edit.php?post_type=product', 'Manchester Charities - Page', 'Manchester Charities- Menu', 'manage_options', 'product_cat_manchester', 'lnz_wp_adminmenu_redirectmanchester' );
add_submenu_page( 'edit.php?post_type=product', 'London Charities - Page', 'London Charities- Menu', 'manage_options', 'product_cat_london', 'lnz_wp_adminmenu_redirectlondon' );
}
// Create Redirects for relevant links
function lnz_wp_adminmenu_redirectmanchester() {
header('Location: http://dev3.benefacto.org/wp-admin/edit.php?s&post_type=product&product_cat=manchester');
exit();
}
function lnz_wp_adminmenu_redirectlondon() {
header('Location: http://dev3.benefacto.org/wp-admin/edit.php?s&post_type=product&product_cat=london');
exit();
}
As far as I know, there's no direct way to achieve this using WP hooks or modifying something like global $submenu...
It can be done with jQuery modifying the href attribute of the submenu items:
add_action( 'admin_menu', function() {
add_submenu_page( 'edit.php?post_type=product', 'Manchester', 'Manchester', 'manage_options', 'cat_manchester', '__return_null' );
add_submenu_page( 'edit.php?post_type=product', 'London', 'London', 'manage_options', 'cat_london', '__return_null' );
});
add_action('admin_footer', function(){
?>
<script>
jQuery(document).ready( function($) {
var admin_url = 'http://dev3.benefacto.org/wp-admin/edit.php';
$('#menu-posts-product').find('a[href*="cat_manchester"]').attr('href',admin_url+'?s&post_type=product&product_cat=manchester');
$('#menu-posts-product').find('a[href*="cat_london"]').attr('href',admin_url+'?s&post_type=product&product_cat=london');
});
</script>
<?php
});

Override External Product URL to "Add to Cart" product button

I work on site that use External products from Amazon, but want instead pointing users to that external URL, first to add to cart that product. I have this function, that change Default Button text for each product, to Add to cart.
function sv_wc_external_product_button( $button_text, $product ) {
if ( 'external' === $product->get_type() ) {
// enter the default text for external products
return $product->button_text ? $product->button_text : 'Add To Cart';
}
return $button_text;
}
add_filter( 'woocommerce_product_single_add_to_cart_text',
'sv_wc_external_product_button', 10, 2 );
But this function not add product to cart.
How to make this function to Add selected product to cart?
Thanks.
Updated 2020
This is a complete different way with simple products and a custom field external link.
In this answer we will use simple products instead of external products.
We add an "External URL" custom field in product option settings and we save the data.
Add a custom field on general product option settings for simple products only :
add_action( 'woocommerce_product_options_general_product_data', 'simple_product_with_external_url' );
function simple_product_with_external_url() {
global $product_object;
echo '<div class="options_group show_if_simple hidden">';
// External Url
woocommerce_wp_text_input( array(
'id' => '_ext_url_cust',
'label' => 'External Url',
'description' => 'Custom external URL',
'desc_tip' => 'true',
'placeholder' => 'Enter here your custom external URL'
) );
echo '</div>';
}
Save the custom field data if it's a simple product and not empty:
add_action( 'woocommerce_admin_process_product_object', 'save_simple_product_with_external_url' );
function save_simple_product_with_external_url( $product ) {
if( $product->is_type('simple') && isset($_POST['_ext_url_cust']) ) {
$product->update_meta_data( '_ext_url_cust', sanitize_url($_POST['_ext_url_cust']) );
}
}
2) This will not work on shop pages and archives pages, if we don't set in WooCommerce the cart redirection when adding a product to cart.
So we will replace add-to-cart button (just for our simple products with a custom link redirection) on shop pages and archives pages by a linked custom button to single product pages.
Replacing add-to-cart button in shop pages and archives pages (for simple products with custom external url):
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
$external_url = $product->get_meta('_ext_url_cust');
if ( ! empty($external_url) ) {
$html = sprintf( '%s', $product->get_permalink(), __("Read More", "woocommerce") );
}
return $html;
}
3) If the custom field value is not empty, the product is added to cart first and then redirected to the external URL (our custom field value in single product pages)
External URL redirection after adding to cart (when custom field is not empty in simple products):
add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_simple_product_with_external_url' );
function redirect_simple_product_with_external_url( $url ) {
if( isset($_REQUEST['add-to-cart']) && absint( $_REQUEST['add-to-cart'] ) > 0 )
return get_post_meta( absint( $_REQUEST['add-to-cart'] ), '_ext_url_cust', true );
return $url;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works on WooCommerce version 3+
Use https://stackoverflow.com/a/44036965/3730754 instead.
You should try to use woocommerce_product_add_to_cart_url filter hook to change the add-to-cart link (here for grouped products), this way:
add_filter( 'woocommerce_product_add_to_cart_url', 'override_external_product_url', 10, 2 );
function override_external_product_url( $url, $product ){
if ( 'external' === $product->get_type() ) {
//Get product ID -- WooCommerce compatibility
if ( method_exists( $product, 'get_id' ) ) {
$product_id = $product->get_id();
} else {
$product_id = $product->id;
}
// custom add to cart url example
$url = home_url( "/product/?add-to-cart=$product_id");
}
return $url;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Update: But this will not add to cart this external product before redirecting to an external url even if it works displaying the add-to-cart url (as add-to-cart is ajax driven).
I fixed myself. For External products, to replace default "Buy This Product" with other generic text, add this functions into functions.php file into theme:
add_filter( 'woocommerce_product_add_to_cart_text' ,
'wpf_custom_add_cart_text_archive',11);
function wpf_custom_add_cart_text_archive() {
global $product;
$product_type = $product->product_type;
switch ( $product_type ) {
case 'external':
return __( 'Add to Cart', 'woocommerce' );
break;
case 'grouped':
return __( 'View products', 'woocommerce' );
break;
case 'simple':
return __( 'Add to cart', 'woocommerce' );
break;
case 'variable':
return __( 'Select options', 'woocommerce' );
break;
default:
return __( 'Read more', 'woocommerce' );
}
}
add_filter( 'woocommerce_product_single_add_to_cart_text',
'wpf_custom_add_cart_text',11);
and this one:
function wpf_custom_add_cart_text() {
return __( 'Add to Cart', 'woocommerce' );
}
to replace text everywhere.

Categories