I'm trying to get the current menuid. I see many solutions where pageid is being used, but this doesn't work when there are more than 1 menu-item linking to the same page. As far as I can see §wp_query gives me page-id, not menu-id.
Does anybody have an idea what to do?
Add a filter to wp_nav_menu_objects,
add_filter( 'wp_nav_menu_objects', 'mwilson123_wp_nav_menu_objects' );
function mwilson123_wp_nav_menu_objects( $menu_items )
{
foreach ( $menu_items as $menu_item ) {
if ( $menu_item->current ) {
$GLOBALS['mwilson123_id'] = $menu_item->ID;
break;
}
}
return $sorted_menu_items;
}
Now you can use this global variable $GLOBALS['mwilson123_id'] where ever you need.
Related
If a woocommerce cart has a virtual item only it does not show shipping methods.
I have a unique situation where I'd like this to be the case also if the cart contains physical products but at least one virtual product (ie any virtual product in the cart, regardless of whatever else is in the cart, hide shipping methods). Whereas if no virtual products are in the cart then show shipping methods as normal.
I've tried the code below but it doesn't seem to work:
Thanks for your help
add_filter( 'woocommerce_cart_needs_shipping_address', 'filter_cart_needs_shipping_address_callback' );
function filter_cart_needs_shipping_address_callback( $needs_shipping_address ){
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
if ( $item['data']->is_virtual() ) {
$needs_shipping_address = false;
break; // Stop the loop
}
}
return $needs_shipping_address;
}
Thanks, it was throwing the error "Uncaught Error: Call to a member function get_cart() on null" but have managed to fix it with this:
if ( is_null( WC()->cart ) ) {
wc_load_cart();
} else
Let me know if there is a better way to do it? Thanks
add_filter( 'woocommerce_cart_needs_shipping', 'filter_cart_needs_shipping_address_callback' );
function filter_cart_needs_shipping_address_callback( $needs_shipping ){
// Loop through cart items
if ( is_null( WC()->cart ) ) {
wc_load_cart();
} else {
foreach ( WC()->cart->get_cart() as $item ) {
if ( $item['data']->is_virtual() ) {
$needs_shipping = false;
break; // Stop the loop
}
}
return $needs_shipping;
}
}
I think I figured out how to do it for you.
The Hook you're using in your example is not the right one since it's just filtering the shipping address callback.
What I think you need to do is use the woocommerce cart needs shipping hook.
Since it was throwing an error you could try to only run the code on the cart and checkout page. You can do that by using if ( is_cart() || is_checkout() ) {
Try the edited code below and let me know how it works.
Have a wonderful day!
add_filter( 'woocommerce_cart_needs_shipping', 'remove_virtual_product_shipping_methods' );
function remove_virtual_product_shipping_methods( $needs_shipping ){
//Only execute code on cart and checkout page.
if ( is_cart() || is_checkout() ) {
//Loop trough cart items
foreach ( WC()->cart->get_cart() as $item ) {
//If a product in cart is a vritual product remove all shipping
if ( $item['data']->is_virtual() ) {
$needs_shipping = false;
break;
}
}
return $needs_shipping;
}
}
I am using the "custom product options" plugin and created a datepicker for customers to choose a requested ship date. I need to create a column on my orders admin page for their selection so I do not have to go into the order to find the date. I have found a few different threads that are similar but they don't really apply to my exact situation. Would someone be able to provide some help on this? Apologies for a lack of detail that is getting me downvotes, but If I knew what I needed I wouldn't be here asking what to do. I am a novice, cut me a little slack. Datepicker can be seen here.
https://chrish148.sg-host.com/product/butterscotch-oatmeal-copy/
current layout looks like this.
current layout image
This is the code that I used for one of my WooCommerce sites to get order-colors:
add_filter( 'manage_edit-shop_order_columns','shopOrder',10 );
function shopOrder($columns)
{
$columns['custom_color_column'] = "Färger";
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'populateShopOrder' );
function populateShopOrder( $column ) {
global $the_order, $post;
if( $column == 'custom_color_column' ) {
global $post, $the_order;
if ( empty( $the_order ) || $the_order->get_id() !== $post->ID ) {
$the_order = wc_get_order( $post->ID );
}
if ( empty( $the_order ) ) {
return;
}
$items = $the_order->get_items();
foreach ($items as $item) {
// load meta data - product attributes
foreach ($item->get_meta_data() as $metaData) {
$attribute = $metaData->get_data();
// attribute value
$value = $attribute['value'];
if($value != null && $attribute['key'] == "pa_farg"){
echo "- " . $value;
if(count($items) > 1) echo "<br>";
}
else return;
}
}
}
}
However this might not be the exakt case for you. Since I do not know the exact plugin that you are using I can not know for sure.
However it should be close.
If you can figure out the attribute names then you could switch it from "fa_farg" to that, or you could link the plugin thet you are using and I will take a look at it.
I'm trying to create quick access link for protected page. For example if the person went to http://example.com/post/?password=PASSWORD they would be straight in without being asked for the password.
So far I've added filter for the 'the_content' which checks if the post is protected, and it compares the value from url with post password.
This part works fine, but how do I return the content after that? Is there any way I can simulate the password form submission, or get content somehow?
function render_content_or_not_to_render( $content ) {
global $post;
if ( post_password_required() ) {
if ( $_GET['password'] == $post->post_password ) {
return $content; // this way it just returns the password form
} else {
return get_the_password_form();
}
}
}
add_filter( 'the_content', 'render_content_or_not_to_render' );
Added filter for the 'post_password_required' gets the job done. Not sure about security, please consider this as rookie solution.
function modof_post_password_required( $post = null ){
global $post;
if ( $_GET['password'] == $post->post_password ) {
return false;
} else {
return true;
}
}
add_filter( 'post_password_required', 'modof_post_password_required', 0 );
Hi I am trying to limit the options certain users have when viewing the products list within woocommerce for wordpress admin pages.
if( current_user_can('vendor') ) {
function my_columns_filter( $columns ) {
unset($columns['tags']);
unset($columns['featured']);
unset($columns['type']);
return $columns;
}
}
add_filter( 'manage_edit-product_columns', 'my_columns_filter', 10, 1 );
Any help or guidance would be much appreciated.
You're doing it wrong.
Place if/else inside the function instead of wrapping the function.
function my_columns_filter( $columns ) {
if( current_user_can('vendor') ) {
unset($columns['tags']);
unset($columns['featured']);
unset($columns['type']);
return $columns;
}
}
add_filter( 'manage_edit-product_columns', 'my_columns_filter', 10, 1 );
You are not using the correct column names, or perhaps WC has changed them since you posted your question. It also better to check if a column still exists before removing it in case WC does change their column names.
Here is a future proof solution you can past into your theme's functions.php:
function my_product_columns_filter( $columns ) {
if ( current_user_can( 'vendor' ) ) {
foreach ( array( 'product_tag', 'featured', 'product_type' ) as $name ) {
if ( isset( $columns[ $name ] ) ) {
unset( $columns[ $name ] );
}
}
}
return $columns;
}
add_filter( 'manage_edit-product_columns', 'my_product_columns_filter' );
no coding solution
If you're just looking for a quick solution without the need for coding, you could use the free admin columns plugin from wordpress.org, which allows you to add and remove columns with a few clicks.
I was wondering if there is an easy solution to add some php "if" code when my website tries to show the widgets and if it has $_SESSION I set on homepage (based on the source of which they came) not to show one of them?
I think maybe this will answer your question.
You finally could have something like this:
add_filter( 'sidebars_widgets', 'hidemywidget' );
function hidemywidget($all_widgets) {
if( $_SESSION['%your key%'] == '%your value%' ) {
foreach ( $all_widgets['primary-widget-area'] as $i => $inst ) {
$pos = strpos( $inst, '%the widget you want to hide%');
if( $pos !== false ) {
unset( $all_widgets['primary-widget-area'][$i] );
}
}
}
return $all_widgets;
}