how to disable cart functionality from woocommerce? - wordpress

How to disable cart functionality from woocommerce store. I want user can only see products available.customer can not purchase from store.

the easiest way is make the products not purchasable..
add_filter( 'woocommerce_is_purchasable','__return_false',10,2);

To fully disable woocommerce purchase functionality:
add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS
More option you can find here: https://react2wp.com/remove-hide-add-to-cart-button-in-woocommerce-while-disabling-keeping-purchase-functionality/

If you need conditions, you can use the following code:
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
// Conditions here.
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );
For example, if you need to check users:
// Disable purchase for non-logged-in users.
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
if ( ! is_user_logged_in() ) {
return false;
}
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );

Related

Remove some features of WooCommerce

I'm modifying a theme for WordPress and i can't find a solution for:
   1. How can I delete the WooCommerce product image inside the gallery? Because this image is added automatically and cannot be deleted.
IMAGES: https://imgur.com/a/qd0INEX
   2. How can I deactivate the cart page, is it possible? I'm only interested in the Checkout page. I've been looking at some codes but they don't allow to select more than 2 products.
Greetings to all who comment, I hope these issues can help more people in the future.
Redirect to Checkout when a Product has been added to the Cart
Second question answer:
How can I deactivate the cart page, is it possible? I'm only interested in the Checkout page. I've been looking at some codes but they don't allow to select more than 2 products.
Step 1.
// Disable AJAX add to cart buttons
First of all, we have to do some small configurations in WooCommerce Settings – Uncheck the “Enable AJAX add to cart buttons on archives” checkbox.
Step 2.
// Change text on add to cart buttons
/*
* Change button text on Product Archives
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'nik_add_to_cart_text_1' );
function nik_add_to_cart_text_1( $add_to_cart_html ) {
return str_replace( 'Add to cart', 'Buy now', $add_to_cart_html );
}
/*
* Change button text on product pages
*/
add_filter( 'woocommerce_product_single_add_to_cart_text', 'nik_add_to_cart_text_2' );
function nik_add_to_cart_text_2( $product ){
return 'Buy now';
}
I decided that str_replace() for this situation is the most simple and easy solution, but if you do not want to use it, you can replace the first part of the code with this one:
/*
* Change button text on Product Archives
*/
add_filter( 'woocommerce_product_add_to_cart_text', 'nik_add_to_cart_text_1', 10, 2 );
function nik_add_to_cart_text_1( $text, $product ){
return $product->is_purchasable() && $product->is_in_stock() ? 'Buy Now' : 'Read more';
}
Step 3.
// Redirect to Checkout Page
add_filter( 'woocommerce_add_to_cart_redirect', 'nik_skip_cart_redirect_checkout' );
function nik_skip_cart_redirect_checkout( $url ) {
return wc_get_checkout_url();
}
Step 4.
// Remove “The product has been added to your cart” message
add_filter( 'wc_add_to_cart_message_html', 'nik_remove_add_to_cart_message' );
function nik_remove_add_to_cart_message( $message ){
return '';
}

URL to product page with quantity inputed - WooCommerce

I am trying to find a way to edit a product url in WooCommerce so that when visited a quantity is pre-selected for the quantity input, e.g. 2.
I know there is an add to cart url for it:
http://yourdomain.com/?add-to-cart=47&quantity=2
But was wondering if it was possible to just input the quantity on the product page with a similar URL.
Thanks for any ideas on the matter.
you can do it like this:
add_filter( 'woocommerce_quantity_input_args', 'custom_woocommerce_quantity_input_args' ); // Simple products
add_filter( 'woocommerce_available_variation', 'custom_woocommerce_quantity_input_args' ); // Variations
function custom_woocommerce_quantity_input_args( $args ) {
if ( isset( $_GET['qty'] ) && is_numeric($_GET['qty']) ) {
$args['input_value'] = $_GET['qty'];
}
return $args;
}
with that you can then do http://yourdomain.com/product/ship-your-idea/?qty=10.

disable postcode validation in WooCommerce checkout page

Does anybody know how to disable the Postcode validation on the checkout page in WooCommerce?
I want to input 'text content' in "billing_postcode" field, but the shop says it's an invalid postcode.
Does anyone know how I can disable Postcode validation or allow text characters?
Just do this:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields', 99 );
function custom_override_checkout_fields( $fields ) {
unset($fields['billing']['billing_postcode']['validate']);
unset($fields['shipping']['shipping_postcode']['validate']);
return $fields;
}
You can put this pretty much anywhere, but preferably in a custom plugin or in your theme's functions.php
You could try this:
add_filter( 'woocommerce_default_address_fields', 'custom_override_address_fields', 999, 1 );
function custom_override_address_fields( $address_fields ) {
// set as not required
$address_fields['postcode']['required'] = false;
// remove validation
unset( $address_fields['postcode']['validate'] );
return $address_fields;
}

woocommerce get product id when product is add to cart

I need to do extra step in the backend when a product is add to cart.
I need to get the product ID juste after it's add to the cart.
I use the woocommerce hook woocommerce_add_to_cart
add_action('woocommerce_add_to_cart', 'attach_item');
function attach_item() {
// I need to have the product id here.
}
I tried many way to get the ID but nothing work.
Any idea ...
Today I also get the same problem but I got the solution.
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart',10,2 );
function woo_custom_add_to_cart( $cart_item_data,$productId ) {
var_dump($productId);
}
At the time of writing this, the hook is actually called from WooCommerce like so: (source)
do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
Add your custom method like so:
add_action('woocommerce_add_to_cart', function($cartItemKey, $productId) {
// do sth with $productId
}, 10, 2);

WooCommerce Delete all products from cart and add current product to cart

I am new to WooCommerce and I need to be able to only add one single product in the cart. I want to clear all products and add the current product to the cart when I click the "Add to cart" button.
How can I do that ?
I have got an exact solution for this.
Try following code.
add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10, 3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id )
{
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return $cart_item_data;
}
The above filter is defined in class-wc-cart.php within function add_to_cart().
http://docs.woothemes.com/wc-apidocs/source-class-WC_Cart.html#774-905
Thus, when add to cart button is pressed, it empties the cart and then add the product.
Try this,
//For removing all the items from the cart
global $woocommerce;
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart($product_id,$qty);
class file is wp-content/plugins/woocommerce/classes/class-wc-cart.php
Hope its helps..

Resources