WooCommerce – Conditionally enable shipping for virtual products

You can try following code: add_filter(‘woocommerce_cart_needs_shipping_address’,’fun_return_shipping_param’); function fun_return_shipping_param($needs_shipping_address) { $items = WC()->cart->get_cart(); $product_ids = array(); foreach($items as $item => $values) { $product_ids[] = $values[‘data’]->get_id(); //You can get product id of product added in cart } if(in_array($your_product_id, $product_ids)) // check whether your product is in cart $needs_shipping_address = true; return $needs_shipping_address; }

Remove Bulk Action For non admin user

Here’s how you can remove bulk actions for any user not an administrator. add_action( ‘wp_loaded’, ‘my_remove_bulk_actions’ ); function my_remove_bulk_actions() { if ( ! is_admin() ) return; if ( ! current_user_can( ‘administrator’ ) ) { add_filter( ‘bulk_actions-edit-shop_order’, ‘__return_empty_array’, 100 ); } } If you would like to target just the shop manager you can edit the … Read more

How to rewrite product permalinks in Woocommerce to use category slugs

Ok, so the second code is: function custom_rewrite_basic() { $prodCatArgs = [‘taxonomy’ => ‘product_cat’]; $wooCats = get_categories($prodCatArgs); $catSlugs = []; foreach($wooCats as $wooCat) { $catSlugs[] = $wooCat->slug; } add_rewrite_rule( ‘^(‘.implode(‘|’, $catSlugs).’)/([^/]*)/?’, ‘index.php?post_type=product&category=$matches[1]&product=$matches[2]’, ‘top’ ); flush_rewrite_rules(); } add_action(‘init’, ‘custom_rewrite_basic’);

How to get woocommerce selected variation from order object

Following below code according your need To get the selected variation attributes using get_variation_attributes( ) method. // Get an instance of the WC_Order object from an Order ID $order = wc_get_order( $order_id ); // Loop though order “line items” foreach( $order->get_items() as $item_id => $item ){ $product_id = $item->get_product_id(); //Get the product ID $quantity = … Read more

How to check woocommerce single product tag

To check if the product has the tag, you can use has_term() or is_object_in_term(). has_term( ‘tag_slug_or_ID’, ‘product_tag’ ) Or: // checking if the product has any tag is_object_in_term( $product_id, ‘product_tag’ ) // checking if the product has a given tag is_object_in_term( $product_id, ‘product_tag’, ‘tag_slug_or_ID’ )