Remove WooCommerce admin order action from order preview - php

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.

Related

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/

Conditional Add To Cart Button On Woocommerce Product Archive

I'm trying to replace the default woocommerce product archive add to cart button based on a condition.
For example
Product A - Checkbox Active --> Display Find A Dealer Button
Product B - Checkbox inactive -- > Display default add to cart button
I have managed to successfully write the code to add the checkbox and the condition to replace the button if the product has a custom checkbox active. The button for product A works fine and diaplyas as intended in the shop archives.
I am however not sure how to retain the woocommerce default add to cart button if for products that do no have this checkbox activate. I thought adding the action would work however I am stumped. Any help would be greatly appreciated. Thank you in advance.
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button' );
function replace_default_button(){
global $product;
if ($product->get_meta('_checkbox_active') === 'yes' ){
return '<button>Finda Dealer</button>';}
else {add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );}
You just forgot the hooked function variables arguments. Try the following instead:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
if ( $product->get_meta('_checkbox_active') === 'yes' ){
$button = '' . __( "Find a dealer", "woocommerce" ) . '';
}
return $button;
}
Code goes in functions.php file of your active child theme (or active theme). It should work.

Display custom data on Woocommerce admin order preview

I would like to add some custom data to the end of the preview order in Woocommerce order listing page.
For that I have tried the hook 'woocommerce_admin_order_preview_end'. But no way to pass any arguments to that action.
add_action( 'woocommerce_admin_order_preview_end', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin( $order ){
//$order is empty here
}
Does anybody have an idea on this? I'm stuck on this.
You can't get the order object as it's a template that loads specific data via Ajax and there is no arguments for woocommerce_admin_order_preview_end action hook.
Instead the filter hook woocommerce_admin_order_preview_get_order_details will allow you first to add some custom data that you will be able to call and display it after in woocommerce_admin_order_preview_end action hook.
The code:
// Add custom order meta data to make it accessible in Order preview template
add_filter( 'woocommerce_admin_order_preview_get_order_details', 'admin_order_preview_add_custom_meta_data', 10, 2 );
function admin_order_preview_add_custom_meta_data( $data, $order ) {
// Replace '_custom_meta_key' by the correct postmeta key
if( $custom_value = $order->get_meta('_custom_meta_key') )
$data['custom_key'] = $custom_value; // <= Store the value in the data array.
return $data;
}
// Display custom values in Order preview
add_action( 'woocommerce_admin_order_preview_end', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin(){
// Call the stored value and display it
echo '<div>Value: {{data.custom_key}}</div><br>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Note: You can also use woocommerce_admin_order_preview_start hook if needed…

WooCommerce Button to Clear Cart, Add One Item and go to Checkout

I need a button on a unique landing page which does this:
clears the cart
adds a specific Item to the cart
go directly to checkout
(disable or hide menue-bar in checkout) <-I tried this with JavaScript but failed
I got a button which calls the link: https://www.snoooze.co/?add-to-cart= 12374
And then this snippet in my "functions.php"
add_filter ('woocommerce_add_to_cart_redirect', 'woo_redirect_to_checkout');
function woo_redirect_to_checkout() {
global $woocommerce;
$woocommerce->cart->empty_cart();
$checkout_url = WC()->cart->get_checkout_url();
return $checkout_url; }
The problem is that if I add the item in my function with add_to_Cart() it does this every time I want to add it manually in the shop, not just if I click on the button on the landing page.
I see that I have to assign the function to the button in some way, so doesn't get called on other sites, but how?
Any help please.
First you need a custom button embedded in an form. So here is a simple shortcode that you can use in a content editor or in php:
// Shortcode: Special button "add-to-cart" with form
function special_button() {
return '<form method="post" action="">
<button type="submit" class="button" name="add_to_cart_special">Special add to cart</button>
</form>';
}
add_shortcode( 'special_button', 'special_button' );
// Usage: [special_button]
// or for php: echo do_shortcode("[special_button]");
And this custom hooked function that will be triggered when you press that custom button. It will:
empty cart,
add product 12374 to cart
redirect to checkout
display a custom notice in checkout page (optionally)
The code:
// Special add to cart (empty cart before and redirect to checkout)
add_action( 'template_redirect', 'special_add_to_cart' );
function special_add_to_cart() {
if ( isset($_POST['add_to_cart_special']) ){
WC()->cart->empty_cart();
WC()->cart->add_to_cart( 12374 );
wc_add_notice( __('this product X has been added to cart'), 'notice' );
wp_redirect( wc_get_checkout_url() );
exit();
}
}
ALL Code goes in function.php file of your active child theme (or active theme).
Tested and works.
But to disable or hide your "menu bar" in checkout, I really don't know… This should be another new question with more details…

To remove the Add to Cart button from a specific product and replace a link instead

I searched about removing the Add to Cart button from a specific product and I used this code in functions.php.
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
return ($product->id == 305 ? false : $is_purchasable);
}
Now I want to replace a direct download link instead of the Add to Cart button.
I have free products and I want direct download link instead of the Add to Cart button.
EDITED:
I used this code but I saw direct download link in all single-product page. I want to see direct download link just in pages which have free product.
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_startsinfo', 11 );
function woocommerce_template_single_startsinfo() {
echo '<div class="starsinfo">
<p>direct download
</div>';
}
The add to cart button is really an HTML a element. You can change the href of this HTML a element by using the filter 'woocommerce_product_add_to_cart_url'.
add_filter( 'woocommerce_product_add_to_cart_url', function( $url, $product ) {
return $your_url;
}, 10, 2 );
Of course, you should use the $product which is a subclass of WC_Product to determine $your_url.

Categories