Hide order review based on cart items total in WooCommerce - wordpress

how Hide order review based on cart items total in WooCommerce ?
i found this code, but i need hide review based on cart items
add_filter( 'woocommerce_cart_needs_shipping', 'show_hide_shipping_methods' );
function show_hide_shipping_methods( $needs_shipping ) {
$cart_items_total = WC()->cart->get_cart_contents_total();
if ( $cart_items_total < 40 ) {
$needs_shipping = false;
// Optional: Enable shipping address form
add_filter('woocommerce_cart_needs_shipping_address', '__return_true' );
}
return $needs_shipping;
}

Do you mean this?
// Remove on checkout page
function remove_checkout_totals() {
$cart_items_total = WC()->cart->get_cart_contents_total();
if ( $cart_items_total < 40 ) {
// Remove cart totals block
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
}
}
add_action( 'woocommerce_checkout_order_review', 'remove_checkout_totals', 1 );

Related

Hide specific class when cart subtotal is above a certain amount

I am trying to hide a specific class when cart subtotal on Woocommerce is above a certain amount.
I have used the code below but have not achieved anything yet. Thank you in advance.
add_filter( 'woocommerce_package_rates', 'hide_specific_class', 10, 2 );
function hide_specific_class($classes, $package ) {
$cart_subtotal = WC()->cart->get_subtotal();
if ( $cart_subtotal > 20 ) {
unset($classes[array_search('inactive-shipping', $classes)]);
return $classes;
}
}
`

WooCommerce Total Tax Amount Update in Checkout Page

I want to change total tax amount in the checkout page and my code is below.
function change_tax_for_checkout_page( $cart ) {
$cart->set_total_tax(20);//20USD
$check = $cart->calculate_totals();
return $cart;
}
add_action( 'woocommerce_before_checkout_form', 'change_tax_for_checkout_page' );
But it's not working.

WooCommerce different product in cart on different page

I have two WooCommerce products and I want one of them loaded into the cart automatically when page A loads, and the other one loaded into the cart automatically when page B loads. My code checks the page ID and adds a specific product to the cart by product ID (after emptying the cart). Checkout is added via shortcodes to each page.
My problem is that regardless of whether I load page A or page B, when I get to the checkout I see the same product; the first product.
add_action( 'wp', 'bbloomer_add_product_to_cart_on_page_id_load' );
function bbloomer_add_product_to_cart_on_page_id_load() {
// product ID to add to cart
$product_id = 1150;
$product_id2 = 4792 ;
if ( is_page( 3337 ) ) {
WC()->cart->empty_cart();
WC()->cart->add_to_cart( $product_id );
}
else if ( is_page( 4232 ) ) {
WC()->cart->empty_cart();
WC()->cart->add_to_cart( $product_id2 );
}
}
add_action( 'wp', 'bbloomer_add_product_to_cart_on_page_id_load' );
function bbloomer_add_product_to_cart_on_page_id_load() {
// product ID to add to cart
$product_id = 1150;
$product_id_1 = 4792 ;
if ( is_page( 3337 ) ) {
WC()->cart->empty_cart();
WC()->cart->add_to_cart( $product_id );
}
else if ( is_page( 4232 ) ) {
WC()->cart->empty_cart();
WC()->cart->add_to_cart( $product_id_1 );
}
}

Woocommerce Applying Coupon on Specific product in Cart

I want to apply the discount coupon to the specific product in cart from checkout page.
Inside the cart:
product1 w/ upgrades
product2 w/o upgrades
when I apply the discount coupon I want that the discount will take effect only in product2's price. I tried to search in some forums but the coupon will take effect in the whole cart items.
You can try this piece of code:
function calculate_variation_addon_price( $cart_object ) {
global $isProcessed;
// Loop for all products in cart
foreach ( $cart_object->cart_contents as $key => $value ) {
// Your condition here for product specific i.e. id == 25
if((!isset($isProcessed) || empty($isProcessed))) {
$orgPrice = floatval( $value['data']->get_price() );
$cart_object->cart_contents[$key]['data']->set_price( $orgPrice * 0.5 );
}
}
$isProcessed = 1;
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_variation_addon_price', 2, 1 );
While adding coupon from admin panel, find a tab there, called 'Usage Restriction'.
There you can add products or product categories or even exclude some for the coupon code to be applied.
You can so that using product id, sku or name
function filter_woocommerce_coupon_get_discount_amount( $discounting_amount, $price_to_discount , $cart_item, $single, $coupon ) {
// On backorder
if ( $cart_item['data']->get_id() !== 'product2_id' ) {
$discounting_amount = 0;
}
return $discounting_amount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
You can use in_array instead of !== for multiple products
You also replace get_id() by other properties or use custom attributs...

woocommerce cart page display products order by product price

I have develop shoping cart in wordpress using Woocommerce plugin. I need to display products in the cart order by product price please help me to do this
thanks
To order products low to high or high to low price in the Woocommerce cart, try adding the following to your functions.php file (or plugin):
function 12345_cart_updated() {
$products_in_cart = array();
// Assign each product's price to its cart item key (to be used again later)
foreach ( WC()->cart->cart_contents as $key => $item ) {
$product = wc_get_product( $item['product_id'] );
$products_in_cart[ $key ] = $product->get_price();
}
// SORTING - use one or the other two following lines:
asort( $products_in_cart ); // sort low to high
// arsort( $products_in_cart ); // sort high to low
// Put sorted items back in cart
$cart_contents = array();
foreach ( $products_in_cart as $cart_key => $price ) {
$cart_contents[ $cart_key ] = WC()->cart->cart_contents[ $cart_key ];
}
WC()->cart->cart_contents = $cart_contents;
}
add_action( 'woocommerce_cart_loaded_from_session', '12345_cart_updated' );
This function is similar and derived from one seen at https://businessbloomer.com/woocommerce-sort-cart-items-alphabetically-az/ which is nearly identical to this earlier-posted function: https://gist.github.com/maxrice/6541634
In most cases, for manipulating data on the WordPress ecosystem, the answer will be wp filter, no wp action.
In addition, WC_car.cart_contents array, hold the product object it's self on $cart_contents['data']; //WC_Product_Object, So we didn't need to get product again.
Simple filter to order by price:
PHP 7.4+
add_filter( 'woocommerce_get_cart_contents', 'prefix_cart_items_order' );
function prefix_cart_items_order( $cart_contents ) {
uasort($cart_contents,
fn($a, $b) =>
$a['data']->get_price() < $b['data']->get_price() ? -1 : 1
);
return $cart_contents;
}
PHP < 7
add_filter( 'woocommerce_get_cart_contents', 'prefix_cart_items_order' );
function prefix_cmp ($a, $b) {
return $a['data']->get_price() < $b['data']->get_price() ? -1 : 1;
}
function prefix_cart_items_order( $cart_contents ) {
uasort($cart_contents, 'prefix_cmp');
return $cart_contents;
}
mmmm, Is right there on the Woo admin page!!
Woocommerce - > Adjustments - > Catalogue - > Default Products Ordering

Resources