Right now, I am bringing users to a Contact 7 Form, and adding to cart using the href link, http://example.com/checkout/?add-to-cart=1000
I have a unique case where if there is 1 item left of product 1000, and the user chooses 2 in the dropdown box, then I should increase product 1000's quantity by 2 and allow the user to checkout normal.
I have looked at the woocommerce_add_to_cart and woocommerce_add_to_cart_validation hooks, but couldn't figure it out.
I just need to understand, how I can make the modification before the user is directed to the checkout page and are not shown the "You cannot add that amount to the cart" message.
add_filter( 'woocommerce_product_get_stock_quantity', 'hook_get_stock_quantity', 10, 2 );
add_filter( 'woocommerce_product_variation_get_stock_quantity', 'hook_get_stock_quantity', 10, 2 );
function hook_get_stock_quantity( $value, $product ) {
if($value < 2) $value = 10;
return $value;
}
Related
I need help from an experienced programmer from here... I have a site that sells Montessori PDF's, Courses, a blog, and more - made from scratch by me. Can be checked at: https://insideoutmontessori.ro/
My issue is Add to Cart Button - I want to show different text depends on conditions - and is not :) Let me explain...
I use a snippet to modify Add to cart Button Text (CUMPARA - means Buy in english) - this is already done by the following code:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );
// To change add to cart text on single product page
function woocommerce_custom_single_add_to_cart_text() {
if ( ! is_admin()) {
return __( 'Cumpără', 'woocommerce' );
}
}
// To change add to cart text on product archives(Collection) page
function woocommerce_custom_product_add_to_cart_text() {
if ( ! is_admin()) {
return __( 'Cumpără', 'woocommerce' );
}
}
I use to put a product without price, to announce next PDF - is like a teaser. Now is not active because o don't work for the moment to something new. For this i manage to alter price to be "In lucru" - means Work in progress in English - but i do not manage to change the button text from CUMPARA to VEZI DETALII (means MORE DETAILS in English). This product is not purchasable and I don't manage to use this condition to change the button text...
Also - for products already added to cart i want to change the Add to cart Button text to ADAUGAT IN COS - means Added to Cart in English - for the moment when the user came back to all products page and browse thru them. I manage before to do this (code below works but...) but i receive some errors (Critical ones) and after change the code i receive only something with JSON message in a red box, in Editor, when i try to save a page witch contains woocommerce shortcodes... I deactivate this snippet to lose the error.
The code is below:
add_filter('woocommerce_product_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
add_filter('woocommerce_product_single_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
function wc_product_add_to_cart_text( $text, $product ){
if ( ! is_admin()) {
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
$text = "Adăugat în coș";
}
return $text;
}
}
So - the bottom line is that I need a snippet that changes the Add to Cart Button text depending on these three conditions, simultaneously:
if the product is in the cart - the button will show: Adaugat in cos (Already in the cart)
if the product is without a price - the button will show: Vezi detalii (See more details)
otherwise - button will show: Cumpara (Buy)
I think this is the correct approach - i mean this needs to be done with one function that alters button text depends on that three conditions.
Thanks!
This question already has an answer here:
Shipping cost based on cart total weight in Woocommerce 3
(1 answer)
Closed 2 years ago.
I'm trying to custom code the shopping cart in Woocommerce by changing the price of the delivery when a condition is met.
For example, if the product weighs 25kg, then simply add another price on top.
Here's my code in functions.php but it doesn't seem to work when i refresh the shopping cart.
$chosen_shipping_state = WC()->customer->get_shipping_postcode();
$chosen_state = $chosen_shipping_state;
// Get the selected shipping price
$chosen_shipping_method_price = WC()->session->get('cart_totals')['shipping_total'];
if ($cartweight >= 25 && $cartweight <= 50) {
WC()->session->set('shipping_total', '100');
do_action( 'woocommerce_cart_updated' );
}
So the current price of the selected courier is $5.50.
If the product weighs over 25 kgs, how do I made the courier price set to $11 and the shopping cart automatically refreshes with this amount?
First, you must choose shipping method ID.
Second, You need to put this code on your functions.php file inside your active theme directory. If your code is not working, please try to clear WooCommerce transient. In order to clear your WooCommerce transient, you can go to WooCommerce -> Status. Then click on Tools tab and you will see WooCommerce Transients. Click on the Clear Transients button.
add_filter( 'woocommerce_package_rates', 'override_ups_rates' );
function override_ups_rates( $rates ) {
foreach( $rates as $rate_key => $rate ){
// Check if the shipping method ID is UPS for example
if( ($rate->method_id == 'flexible_shipping_ups') ) {
// Set cost to zero
$rates[$rate_key]->cost = 5.50;
}
}
return $rates;
}
This question already has an answer here:
Display message based on cart items count in WooCommerce cart
(1 answer)
Closed 3 years ago.
I want to display a message on my WooCommerce Cart Page, that tells my customers, how much they need to purchase to get a free gift.
I already got the following code which works, but I have one problem.
When customers update their cart or the quantity the following code does not update (because the page does not reload).
<?php $e_cart = WC()->cart->cart_contents_total * 1.25;?>
<?php $e_cart_remaining = 300 - $e_cart; ?>
<?php
if ( $e_cart < 300 ) {
echo "Get a free gift, when you purchase for ${e_cart_remaining} more.";
}?>
So the problem is, that if a customers has goods for 250 in his cart the message will say: "Purchase for 50 more to get a free gift". (Because you will get a free gift at 300). But if they change the quantity of one of the products the text still says 50. (Because the page have not updated)
How do i trigger this script or block of code every time the cart is updated?
Thank you very much.
To display a custom message on cart page based on cart amount, use the following:
// On cart page only
add_action( 'woocommerce_check_cart_items', 'custom_total_item_quantity_message' );
function custom_total_item_quantity_message() {
$e_cart = WC()->cart->cart_contents_total * 1.25;
$e_cart_remaining = 300 - $e_cart;
if( is_cart() && $e_cart < 300 ){
wc_print_notice( sprintf( __("Get a free gift, when you purchase for %s more.", "woocommerce"), $e_cart_remaining ), 'notice' );
}
}
Code goes in function.php file of your active child theme (or active theme).
I have a Woocommerce store, I was looking to add another add to cart button named "Try now" to a particular category(not to all the categories). When the user clicks on "try now", it gets redirected to the custom checkout page.
On custom checkout page, try-now products can be added up-to 5 products max, total is fixed as 500 for all the try now products and payment only by card.
I have found the following codes.
Code to replace add to cart button
function remove_loop_button(){
if (in_array( 10, $product->category_ids))
remove_action(‘woocommerce_after_shop_loop_item’,‘woocommerce_template_loop_add_to_cart’, 10);
}
add_action(‘init’,’remove_loop_button’);
add_action(‘woocommerce_after_shop_loop_item’,’replace_add_to_cart’);
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
echo ‘<a href=”‘.esc_attr($link).'”>Try Now</a>’;
}
And also this code to customize checkout page from Make 2 different WooCommerce checkout pages?
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
global $woocommerce;
//assuming only one product in cart
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
}
switch ($_product->id) {
case '666': //product id for product 1
//do stuff with the checkout fields
break;
case '999': //product id for product 2
//do stuff with the checkout fields
break;
}
return $fields;
}
I know it's quite complicated, Can any one please suggest me where to start? Or any hint will be appreciated. Thanks.
Everything I can find on removing the WooComm Add To Cart button will remove not just the add to cart button but also the pricing/variations, aka the whole add to cart area.
My goal is to enable/disable the ability to purchase a product with a checkbox/selector on the product info page. BUT I still have to be able to see the product variation pricing and the variation drop down menu.
This is important. The pricing shown under the product title, for a variation, will be something like $20.00 - $40.00 and not until you select the variation choice will it show the price next to the add to cart button.
So far I have things working wherein I can remove the add to cart area — variations and all — conditionally on my custom field, but I have no idea how to hide/disable click/remove just the add to cart button and allow variations to still be chosen with the variation price displayed.
function remove_add_to_cart(){
if(get_post_meta(get_the_ID(), 'woo_callforinfo', true)) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
} add_action('woocommerce_single_product_summary','remove_add_to_cart');
Here's what I did. The conditional IF statement is because I have a RETAIL shop with variable products that I don't want affected.
function remove_add_to_cart(){
if ( has_term( 'wholesale', 'product_tag' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
}
add_action('woocommerce_single_variation','remove_add_to_cart');
Added this to get rid of the 'Sorry..' message if price is not set.
add_filter( 'gettext', 'customizing_product_variation_message', 10, 3 );
function customizing_product_variation_message( $translated_text,
$untranslated_text, $domain )
{
if ($untranslated_text == 'Sorry, this product is unavailable. Please choose a different combination.') {
$translated_text = __( '-type anything you want here, or leave a space- ', $domain );
}
return $translated_text;
}
Just add the following code in your functions.php and you will find button hidden
I don't know whether my solution is perfect. But it works. Normally if is_purchasable is returned to the filter woocommerce_is_purchasable, the ‘Add to Cart’ button is displayed, and if false is returned the button is hidden.
So, you just need to add the following:
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
// Write code to access custom field value in this function
// let $custom_value be the value from checkbox
return ($custom_value == false ? false : $is_purchasable);
}
No incompatibility issues would creep up.