I'm using in the wc_braintree_cart_payment_gateways filter hook, this code to remove the "proceed to checkout" button in WooCommerce:
add_filter( 'wc_braintree_cart_payment_gateways', function ( $gateways ) {
$categories = [aaa,bbb];
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Check for product categories
if ( ! has_term($categories, 'product_cat', $cart_item['product_id'] ) ) {
$gateways = [];
break;
}else{
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
return $gateways;
} );
The first condition works as intented. The remove_action doesn't take affect. I'm not sure it's correctly placed? Any advice?
I don't use the plugin you are talking about but adding a (lower) priorty number to the hook can provide the solution.
function filter_wc_braintree_cart_payment_gateways ( $gateways ) {
// Remove proceed to checkout button
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
return $gateways;
}
add_filter( 'wc_braintree_cart_payment_gateways', 'filter_wc_braintree_cart_payment_gateways', 10, 1 );
If this works, you can extend your code
Related
we are using WooCommerce Subscription and we want, that after a subscription is added to the cart redirect the customer to the checkout page. In the past I think there was a setting to set for this but I'm not able to find it. However, now I try to do it via code.
We use AJAX in order to add to the cart. But I think this will not work. And I have to disable ajax add to cart only for some products? Like check for a category?
So I have to disable ajax add to cart only for specific products and then redirect the customer when adding such a product to the cart to checkout. The code below works if I deactivate ajax add to cart for all products and also all product will redirect to the checkout. However, we only need it for specific products.
// redirect customer to checkout page
add_filter( 'woocommerce_add_to_cart_redirect', 'custom_skip_cart_redirect_checkout' );
function custom_skip_cart_redirect_checkout( $url ) {
return wc_get_checkout_url();
}
// Fix for “Sold Individually” Products
add_filter( 'woocommerce_product_add_to_cart_url', 'custom_fix_for_individual_products', 10, 2 );
function custom_fix_for_individual_products( $add_to_cart_url, $product ){
if( $product->get_sold_individually() // if individual product
&& WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->id ) ) // if in the cart
&& $product->is_purchasable() // we also need these two conditions
&& $product->is_in_stock() ) {
$add_to_cart_url = wc_get_checkout_url();
}
return $add_to_cart_url;
}
// Remove “The product has been added to your cart” message
add_filter( 'wc_add_to_cart_message_html', 'custom_remove_add_to_cart_message' );
function custom_remove_add_to_cart_message( $message ){
return '';
}
Maybe it could help:
add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_to_checkout_if_product_is_subscription', 99, 1 );
function redirect_to_checkout_if_product_is_subscription( $url ) {
if ( ! isset( $_REQUEST['add-to-cart'] ) ) {
return $url;
}
$product_id = intval( $_REQUEST['add-to-cart'] );
if ( $product_id <= 0 ) {
return $url;
}
$product = wc_get_product( $product_id );
if ( WC_Subscriptions_Product::is_subscription( $product ) ) {
$url = wc_get_checkout_url();
}
return $url;
}
I have changed the main WooCommerce cart page using CSS to hide the products on this page.
The reason for the change, I am attempting to dedicate the cart page to only show the product "Thank You For The Tip" if it is in the cart, without changing any of the WooCommerce product settings to hidden.
All other products that have been added to the cart need to be hidden on the main WooCommerce cart page.
I see that I can not achieve this with CSS as the changes I made to the CSS will hide all products that have been added to the cart.
The closest I have come to finding a PHP solution is the following snippet:
add_filter( 'woocommerce_cart_item_visible', 'bbloomer_hide_hidden_product_from_cart' , 10, 3 );
add_filter( 'woocommerce_widget_cart_item_visible', 'bbloomer_hide_hidden_product_from_cart', 10, 3 );
add_filter( 'woocommerce_checkout_cart_item_visible', 'bbloomer_hide_hidden_product_from_cart', 10, 3 );
add_filter( 'woocommerce_order_item_visible', 'bbloomer_hide_hidden_product_from_order_woo333', 10, 2 );
function bbloomer_hide_hidden_product_from_cart( $visible, $cart_item, $cart_item_key ) {
$product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
if ( $product->get_catalog_visibility() == 'hidden' ) {
$visible = false;
}
return $visible;
}
function bbloomer_hide_hidden_product_from_order_woo333( $visible, $order_item ) {
$product = $order_item->get_product();
if ( $product->get_catalog_visibility() == 'hidden' ) {
$visible = false;
}
return $visible;
}
However, this does not give the desired result. Any advice?
To hide certain WooCommerce products only on the cart page and nowhere else, it suffices to just use the woocommerce_cart_item_visible filter hook.
In the $targeted_ids array you can indicate which productIDs should remain visible. This also works for variationIDs
function filter_woocommerce_cart_item_visible( $true, $cart_item, $cart_item_key ) {
// The targeted product ids
$targeted_ids = array( 30, 53 );
// Computes the intersection of arrays
if ( ! array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$true = false;
}
return $true;
}
add_filter( 'woocommerce_cart_item_visible', 'filter_woocommerce_cart_item_visible', 10, 3 );
To apply the reverse, and hide the productIDs that occur in the array
Replace
if ( ! array_intersect(..
With
if ( array_intersect(..
I am trying to remove the proceed to checkout button and restrict access to the checkout page until a customer fills out the 'Calculate shipping' option on the basket page.
I created a local shipping method that's restricted to multiple post/zipcodes. I then went and added this to my functions.php file.
function disable_checkout_button_no_shipping() {
$package_counts = array();
// get shipping packages and their rate counts
$packages = WC()->shipping->get_packages();
foreach( $packages as $key => $pkg )
$package_counts[ $key ] = count( $pkg[ 'rates' ] );
// remove button if any packages are missing shipping options
if( in_array( 0, $package_counts ) )
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button_no_shipping', 1 );
However when I test the site in different browsers and in incognito modes the 'Proceed to checkout' button is there.
If I click on the 'Calculate shipping' link and not fill out the form but update it the button disappears. I basically want the button to appear when a customer fills out the 'Calculate shipping' form on the cart page (and have one of the postcodes in my shipping method) before being able to proceed to the checkout page.
You should better try with the "Chosen shipping method" from WooCommerce session like:
function disable_checkout_button_no_shipping() {
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
// remove button if there is no chosen shipping method
if( empty( $chosen_shipping_methods ) ) {
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button_no_shipping', 1 );
Or in a different way using woocommerce_check_cart_items action hook:
add_action( 'woocommerce_check_cart_items', 'required_chosen_shipping_methods' );
function required_chosen_shipping_methods() {
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
if( is_array( $chosen_shipping_methods ) && count( $chosen_shipping_methods ) > 0 ) {
// Display an error message
wc_add_notice( __("A shipping method is required in order to proceed to checkout."), 'error' );
}
}
It should better work.
Currently i have some custom calculation of product price based on different situation. When customer added a product in to cart then the custom price is set in session data , cart_item_data['my-price'] and i implemented using add_filter( 'woocommerce_add_cart_item') function and everything seems to working now .
Now the price in view cart page, checkout page is correct with my cart_item_data['my-price'].
But the only problem i am facing is the price is not updated in woocommerce mini cart that is appeared in the menu ,How can i change this ?
When i google i see a filter
add_filter('woocommerce_cart_item_price');
but i can't understand how to use this i do the following
add_filter('woocommerce_cart_item_price','modify_cart_product_price',10,3);
function modify_cart_product_price( $price, $cart_item, $cart_item_key){
if($cart_item['my-price']!==0){
$price =$cart_item['my-price'];
}
return $price;
//exit;
}
Here individual price is getting correct , but total price is wrong
Updated (october 2021)
For testing this successfully (and as I don't know how you make calculations), I have added a custom hidden field in product add to cart form with the following:
// The hidden product custom field
add_action( 'woocommerce_before_add_to_cart_button', 'add_gift_wrap_field' );
function add_gift_wrap_field() {
global $product;
// The fake calculated price
?>
<input type="hidden" id="my-price" name="my-price" value="115">
<?php
}
When product is added to cart, this my-price custom field is also submitted (posted). To set this value in cart object I use the following function:
add_filter( 'woocommerce_add_cart_item', 'custom_cart_item_prices', 20, 2 );
function custom_cart_item_prices( $cart_item_data, $cart_item_key ) {
// Get and set your price calculation
if( isset( $_POST['my-price'] ) ){
$cart_item_data['my-price'] = $_POST['my-price'];
// Every add to cart action is set as a unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
Now to apply (set) the new calculated price my-price to the cart item, I use this last function:
// For mini cart *(cart item displayed price)*
add_action( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 2 );
function filter_cart_item_price( $price, $cart_item ) {
if ( ! is_checkout() && isset($cart_item['my-price']) ) {
$args = array( 'price' => floatval( $cart_item['my-price'] ) );
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'], $args );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
}
return wc_price( $product_price );
}
return $price;
}
add_action( 'woocommerce_before_calculate_totals', 'set_calculated_cart_item_price', 20, 1 );
function set_calculated_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ){
if( isset( $cart_item['my-price'] ) && ! empty( $cart_item['my-price'] ) || $cart_item['my-price'] != 0 ){
// Set the calculated item price (if there is one)
$cart_item['data']->set_price( $cart_item['my-price'] );
}
}
}
All code goes in function.php file of your active child theme (or active theme).
Tested and works
I have some code that is blocking the price from being shown on all products, if the user is not logged in.
My issue is that I have 1 product that is free, and I need the price to be shown, if the user is not logged in.
Can someone help me target that single product by his id and show this particular price even if the user is not logged in.
Here is my original php code in funcions.php which blocks the price from being shown, when a user is not logged in:
// Hide prices on public woocommerce (not logged in)
add_action('after_setup_theme','activate_filter') ;
function activate_filter(){
add_filter('woocommerce_get_price_html', 'show_price_logged');
}
function show_price_logged($price){
if(is_user_logged_in()){
return $price;
}
else
{
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
return 'Call for pricing';
}
}
Thanks.
Cleaned up a bit and abstracted your conditional test to a custom function called so_42075748_hide_price() which tests if the user is logged in and tests if the product's price is greater than zero. I've also filtered woocommerce_is_purchasable to make these products completely not purchasable to someone who may know that you can simply do ?add-to-cart=99 to add a product to the cart.
// Switch the Price HTML
add_filter('woocommerce_get_price_html', 'so_42075748_hide_price_logged', 10, 2 );
function so_42075748_hide_price_logged( $price, $product ){
if( so_42075748_hide_price( $product ) ){
// Not the ideal permalink in my opinion, but copying from original question.
$price = '' . __( 'Call for pricing', 'your-textdomain' ) . '';
}
return $price;
}
// Make products completely unpurchasable
add_filter( 'woocommerce_is_purchasable', 'so_42075748_is_purchasable', 10, 2 );
function so_42075748_is_purchasable( $purchasable, $product ){
if( so_42075748_hide_price( $product ) ){
$purchasable = false;
}
return $purchasable;
}
// Hide add to cart buttons
add_action( 'woocommerce_before_shop_loop_item', 'so_42075748_hide_prices' );
add_action( 'woocommerce_before_single_product', 'so_42075748_hide_prices' );
function so_42075748_hide_prices(){
global $product;
if( so_42075748_hide_price( $product ) ){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
Edited: Made a more complete conditional test that also tests if the price is null.
// Your condition for hiding products
function so_42075748_hide_price( $product = null ){
if( ! is_object( $product ) ){
return true;
}
if( ! is_user_logged_in() ){
$price = $product->get_price();
if( ! is_null( $price ) && $price > 0 ){
return true;
}
}
return false;
}