Open a specific product tab programmatically in a Woocommerce - php

I am using woocommerce. After submission of certain form I want to send it to product page with opening a certain tab, here "Addition Information" tab...
How can I achieve this ?
I have tried url like 'WEBSITE/product/product-3/#tab-additional_information'
But it didn't worked.

Updated:
Yes this is possible with a little bit of javascript and jQuery embedded code in a custom php function hooked in wp_footer action hook.
The call will be made in the url like: WEBSITE/product/product-3/?tab=additional_information
Here is that function code:
add_action( 'wp_footer', 'custom_reordering_product_tabs_action' );
function custom_reordering_product_tabs_action() {
// Only in single product pages and a specific url (using GET method)
if( isset( $_GET['tab'] ) && is_product()) :
?>
<script type="text/javascript">
(function($){
setTimeout(function() {
$('ul.tabs.wc-tabs > li.active').removeClass("active");
$('div#tab-description').hide();
$('ul.tabs.wc-tabs > li.<?php echo $_GET['tab']; ?>_tab').addClass("active");
$('div#tab-<?php echo $_GET['tab']; ?>').show();
}, 500);
})(jQuery);
</script>
<?php
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on Woocommerce 3+ and works
May be what you can do instead of open a product tab automatically, is to reorder in 1st position automatically that tab trough $_GET method (adding a variable in the url).
The call in your url will need to be like: WEBSITE/product/product-3/?tab=additional_information
You will be able to do it with that custom function hooked in woocommerce_product_tabs filter hook:
add_filter( 'woocommerce_product_tabs', 'custom_reordering_product_tabs_action', 98 );
function custom_reordering_product_tabs_action( $tabs ) {
if( empty( $_GET['tab'] ) ) return $tabs;
$tabs[$_GET['tab']]['priority'] = 2;
return $tabs;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on Woocommerce 3+ and works

Related

WooCommerce: How to exclude a specific page in a hooked action

This code works perfectly except I'm trying to determine how to EXCLUDE the main shop page from being included in the action. This hook adds the HTML code to all archive-type pages including the main shop page. Given this is a link back to the shop page, it should be excluded on the shop page.
Any help would be greatly appreciated.
function add_back_to_catalog_category() {
?>
<div><br><p class="button-catalog">Back to Catalog - All Products</p></div><?php
}
add_action( 'woocommerce_after_shop_loop', 'add_back_to_catalog_category' );
Visual reference to woocommerce_after_shop_loop action hook.
You can use the WooCommerce conditional tag is_shop() like:
add_action( 'woocommerce_after_shop_loop', 'add_back_to_catalog_category' );
function add_back_to_catalog_category() {
// Not on shop page
if( ! is_shop() ) {
printf( '<div><br><p class="button-catalog">%s</p></div>',
get_permalink( wc_get_page_id( 'shop' ) ),
__("Back to Catalog - All Products", "your-text-domain")
);
}
}
Code goes in functions.php file of the active child theme (or active theme). It should works.

Disallow purchases from virtual products in WooCommerce

I'm trying to remove the add to cart button from the categories page for products that are not purchasable.
This is my current code, which I have placed in the functions.php file of my child theme;
function buy_filter()
{
if ( ! is_product() ) return;
$product_id=get_the_ID();
$product = wc_get_product($product_id);
if ($product->is_virtual('yes'))
if ($product->is_virtual('yes'))
{
add_filter( 'woocommerce_is_purchasable', '__return_false');
}
}
add_action ('wp', 'buy_filter');
I've managed to remove the button from the individual product pages, however I still can't get it to be removed from the categories page. Now when I inspect the button shows the following code:
<a href="?add-to-cart=16972" data-quantity="1" class="button product_type_simple add_to_cart_button
ajax_add_to_cart" data-product_id="16972" data-product_sku="604544617405" aria-label=
"Add “Federal - 9MM Syntech, 115gr” to your cart" rel="nofollow">Add to cart</a>
Does this button need to be disabled in another way?
I really need this button to be removed as its a product that we only want to sell in our store, but we want people to know that we do stock that product as well.
Below is an image of what it currently looks like, and what I want to go. Ideally, I would like the button gone completely, but I will definitely settle for a replacement to the read more button if that is easier.
To remove it from everywhere (on single pages and category pages) use directly the filter instead, replacing your code with:
add_filter( 'woocommerce_variation_is_purchasable', 'filter_virtual_products', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'filter_virtual_products', 10, 2 );
function filter_virtual_products( $is_purchasable, $product )
{
if ( $product->is_virtual('yes') ) {
$is_purchasable = false;
}
return $is_purchasable;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Please try this code inside your functions.php file
add_filter('woocommerce_is_purchasable', 'woocommerce_cloudways_purchasable');
function woocommerce_cloudways_purchasable($cloudways_purchasable, $product) {
return ($product->id == your_specific_product_id (like 22) ? false : $cloudways_purchasable);
}
Source: https://www.cloudways.com/blog/how-to-remove-hide-or-disable-add-to-cart-button-in-woocommerce/
or Use this plugin will solve your issue without any coding.
https://wordpress.org/plugins/non-purchasable-woocommerce-products/

Remove WooCommerce admin order action from order preview

I want to remove, for shop-managers, the ability to mark an order as completed. To do so, I used the following based on "Hide a specific action button conditionally in Woocommerce admin Orders list" answer in my theme's functions.php file:
add_filter( 'woocommerce_admin_order_actions', 'custom_admin_order_actions', 900, 2 );
function custom_admin_order_actions( $actions, $the_order ){
if(isset(wp_get_current_user()->roles[0]) && wp_get_current_user()->roles[0] == 'shop-manager')
unset($actions['complete']);
return $actions;
}
By this, I succesfully removed the complete button from the shop_order page. However, the shop-manager is still able to complete the order using the Complete button that appears in the order preview. To avoid this, I tried the next action after the previous one:
add_action( 'woocommerce_admin_order_preview_start', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin(){
// Call the stored value and display it
echo '<div>Class = "button hidden wc-action-button wc-action-button-complete complete"</div><br>';
}
However, this does not remove the button from the preview window because it does not substitute the line in the code.
Is there a way to remove this ability from the shop_order page and the order preview at once? If not, how can I hide this button from the preview window?
To remove the "complete" update order status button from admin order preview for "Shop manager" user role, use the following:
add_filter( 'woocommerce_admin_order_preview_actions', 'filter_admin_order_preview_actions', 10, 2 );
function filter_admin_order_preview_actions( $actions, $order ) {
if( current_user_can('shop-manager') && isset($actions['status']['actions']['complete']) ) {
unset($actions['status']['actions']['complete']);
}
return $actions;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Hide a custom button if cart is not empty on ajax add to cart in Woocommerce

I have a function that checks if the WooCommerce cart is empty, and if it is, adds a button to the page header that links to my shop page. However, this code only works on page load, but won’t update (and remove the button) after adding to cart via AJAX. How do I detect if an item has been added to the cart?
function shop_button_when_empty_cart() {
if ( WC()->cart->get_cart_contents_count() == 0 ) { ?>
<button>Your cart is empty, go to shop</button>
<?php
}
}
add_action( 'storefront_header', 'shop_button_when_empty_cart', 40 );
Based on this existing answer here is the correct way to make this work in Woocommerce archive pages where Ajax is enabled on add to cart button. On ajax add to cart the button will be hidden:
add_action( 'storefront_header', 'shop_button_when_empty_cart', 40 );
function shop_button_when_empty_cart() {
$style = WC()->cart->get_cart_contents_count() > 0 ? ' style="display: none;"' : '';
$class = 'class="button go-shop"';
echo '<a href="/shop" '.$class.$style.'>Your cart is empty, go to shop</a>';
if( is_shop() || is_product_category() || is_product_tag() ):
?>
<script type="text/javascript">
// Ready state
(function($){
$( document.body ).on( 'added_to_cart', function(){
$('a.button.go-shop').hide();
console.log('action');
});
})(jQuery);
</script>
<?php
endif;
}
Code goes in function.php file of your active child theme (or active theme). tested and work.
Related:
Woocommerce: custom jquery event after added to cart
Run javascript code after ajax add to cart event in Woocommerce
If you are using ajax then you will have to use ajax handler. Woocommerce have an event called added_to_cart
Here you can inject your code when this event is calling,
$(document).on('added_to_cart',function(){
//here goes your button add code
}
$(document).on( 'added_to_cart removed_from_cart', function(){
//here goes if cart item is removed
}

How do you remove/edit the Dokan Store Tabs

I am using Dokan for a listings site. The core set of products that will be used are bookable products.
As such I want to rename the products field to 'Bookings'. I have digging around in the plugin template files and can find where the tabs are included in the store template but can't seem to find where the actual tabs are defined.
Any help would be awesome thanks. For reference below the code where the tabs are being included:
<?php if ( $store_tabs ) { ?>
<div class="dokan-store-tabs<?php echo $no_banner_class_tabs; ?>">
<ul class="dokan-list-inline">
<?php foreach( $store_tabs as $key => $tab ) { ?>
<li><?php echo $tab['title']; ?></li>
<?php } ?>
<?php do_action( 'dokan_after_store_tabs', $store_user->get_id() ); ?>
</ul>
</div>
I am also having an issue with the reviews page (I removed the sidebars from store.php and store-review.php in child theme folder) and reviews page now just loads header and footer of my theme).
You can disable any tabs you want by adding a code in your function.php file (to avoid code deletion when theme updates).
For example:
To remove the reviews tab
// Disable Woocommerce Reviews Form Tab
add_filter( 'woocommerce_product_tabs', 'wcs_woo_remove_reviews_tab', 98 );
function wcs_woo_remove_reviews_tab($tabs) {
unset($tabs['reviews']);
return $tabs;
}
And to remove extra tabs by calling tab name
// Disable Seller More Products Tab
add_filter( 'woocommerce_product_tabs', 'wcs_woo_remove_more_seller_product_tab', 98 );
function wcs_woo_remove_more_seller_product_tab($tabs) {
unset($tabs['more_seller_product']);
return $tabs;
}
Hope this can help you :)
Managed to figure out how to do it.
In the dokan plugin folder navigate to the includes folder. Inside this folder there is a functions.php file.
Search for 'dokan_get_store_tabs('
Find and replace Products with text of your choice within this function.
for remove Seller info tab on product page in dokan plugin:
add_filter( 'woocommerce_product_tabs', 'dokan_remove_seller_info_tab', 50 );
function dokan_remove_seller_info_tab( $array ) {
unset( $array['seller'] );
return $array;
}
for remove seller button on WC dashboard in dokan plugin:
remove_action( 'woocommerce_after_my_account', array( Dokan_Pro::init(), 'dokan_account_migration_button' ) );
for disable registration as a seller in WC registration in dokan plugin:
remove_action( 'woocommerce_register_form', 'dokan_seller_reg_form_fields' );

Categories