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;
}
}
`
Related
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 );
I am building an eCommerce website by WordPress and stuck in one place. I want to set a function on my website. When buyer buys $30+ product then I want to make shipping cost zero. So I cannot understand how to do it. Please, someone, help me.
Something like this should do the trick.
add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates', 10, 2 );
function woocommerce_package_rates( $rates, $package ) {
if ( WC()->cart->subtotal > 30 ) {
foreach($rates as $key => $rate ) {
$rates[$key]->cost = 0;
}
}
return $rates;
}
You can find more examples about shipping fee here:
http://reigelgallarde.me/programming/woocommerce-shipping-fee-changes-condition-met/
For a client, I need to let backorders for products but they don't want to charge for those. So, I need to update the price dynamically if they stock is 0 or the stock is less than the quantity on the cart.
This is my code:
add_filter( 'woocommerce_cart_product_subtotal', 'modify_cart_product_subtotal', 10, 4 );
function modify_cart_product_subtotal( $product_subtotal, $product, $quantity, $cart ) {
if ( $product->get_stock_quantity() < $quantity ) {
return $product->get_stock_quantity() * $product->get_price();
}
return $product_subtotal;
}
In case of 0 stock, it works like a charm. In case of a quantity greater than the stock, it charges only for the available products, but the tax keeps increasing even for the rest. How can I exclude those from taxes calculations as well?
EDIT:
If I just update the price of the product, it does it for all of them, not only those above the available stock.
function zero_price_for_backorders( $cart_object ) {
global $isProcessed;
if( !WC()->session->__isset( "reload_checkout" )) {
foreach ( $cart_object->get_cart() as $key => $value ) {
if ( $value['data']->get_stock_quantity() < $value['quantity']) {
$value['data']->set_price(0);
}
}
$isProcessed = true;
}
}
add_action( 'woocommerce_before_calculate_totals', 'zero_price_for_backorders', 99 );
You should use before_calculate_total and change product price using set_price function so that you don't have to modify subtotal separately. The rest of the flow will fall in place automatically.
You can have a look at this link on how to change it dynamically:
Change product price dynamically woocommerce
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...
I need to add discount according to number of product in cart and this discount will apply on total of cart. Is there any other option without use of coupons?
I prefer this way, cleaner I think
// Hook before calculate fees
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
/**
* Add custom fee if more than three article
* #param WC_Cart $cart
*/
function add_custom_fees( WC_Cart $cart ){
if( $cart->cart_contents_count < 3 ){
return;
}
// Calculate the amount to reduce
$discount = $cart->subtotal * 0.1;
$cart->add_fee( 'You have more than 3 items in your cart, a 10% discount has been added.', -$discount);
}
This code should work:
add_action('woocommerce_before_cart_table', 'discount_when_produts_in_cart');
function discount_when_produts_in_cart( ) {
global $woocommerce;
if( $woocommerce->cart->cart_contents_count > 3 ) {
$coupon_code = 'maryscode';
if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
$woocommerce->show_messages();
}
echo '<div class="woocommerce_message"><strong>You have more than 3 items in your cart, a 10% discount has been added.</strong></div>';
}
}
The above will apply the coupon "maryscode" to the cart if there are 4 or more products in the customers cart.
EDIT: Add the following to your css
.coupon {
display: none !important;
}