if short: i have product with 0 price and i don't want it to be added to the cart.
if longer: i'm creating composite product (i can't use 'grouped product' option, because one of my items is variative). Every item of this product should be sold separately. But i have to set regular price for product in order to display it. And this regular price, of course, adds to the cart. So i decided to set price to 0 and now i have normal total price in the cart and one needless position..
I think it could be resolved by adding some simple condition for price before loop in cart function, but i'm almost zero in php :(
Possible solution:
function wpa_109409_is_purchasable( $purchasable, $product ){
if( $product->get_price() == 0 )
$purchasable = false;
return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'wpa_109409_is_purchasable', 10, 2 );
Related
I need to set a threshold on a per product basis for stock inventory.
Example : If the product has a threshold of 10 and there's 10 or less in stock, the product shows the out of stock text. If the threshold is set at 10 but there's 15 in stock, 5 is shown in stock.
So what i need to do is offset the real inventory levels on a per product basis using a value.
This is the code i'm trying to get working
add_filter( 'woocommerce_get_availability', 'override_get_availability', 10, 2);
function override_get_availability( $availability, $product ) {
$stock_threshold = get_post_meta( $product->get_id(), '_stock_threshold', true );
if ( $product->managing_stock() && ! empty( $stock_threshold ) ) {
$availability['availability'] = $product->get_stock_quantity() - $stock_threshold;
return $availability;
}
}
This code doesn't show the Out of Stock text on the single product page if the inventory is calculated at 0 or less than zero.
Example : When the stock level is set at 2 and the offset is set at 2, it = 0 so should show Out of Stock but doesn't.
I think this gets you some of the way there;
add_filter( 'woocommerce_get_availability', 'override_get_availability', 10, 2);
function override_get_availability( $availability, $product ) {
$stock_threshold = $product->get_low_stock_amount();
$stock_quantity = $product->get_stock_quantity();
if ( $product->managing_stock() && !empty($stock_threshold) ) {
$avail_stock = ($stock_quantity - $stock_threshold);
if ($avail_stock <= 0){
$availability['availability'] = __('Out of Stock', 'woocommerce');
}
return $availability;
}
}
Basically, we're getting the low stock amount (threshold) and subtracting the stock quantity. If it's 0 or less, then set the availability to Out of Stock.
This works without a problem.
However if the product has 15 in stock and the threshold at 10, it still shows 15 as the out of stock is NOT 0.
I think the best approach is a combination of the above, and then shortcode to display the quantity minus the threshold rather than using the code inside WooCommerce.
Maybe someone else can build off what I have posted above as I couldn't get the quantity number to reduce on products NOT out of stock.
I want to build the following:
A landingspage with a Apply Coupon field.
If the coupon is valid it needs to add a specific product to the cart.
So basically the coupon needs to be attached to a specific product. (in this case product_id 99.
I found this somewhere:
function wc_ninja_apply_coupon( $coupon_code ) {
if ( 'JLSDFO' === $coupon_code ) {
$product_id = 99;
WC()->cart->add_to_cart( $product_id );
}
}
add_action( 'woocommerce_applied_coupon', 'wc_ninja_apply_coupon' );
But this is for one coupon specific. I want to add for example 1000 coupons which are merged with this product. The customer can select this product on the back-end while adding this coupons. So when the user enters one of the 1000 coupons it understands to add for example product_id 99 to the cart.
How would we go about disabling the single product page for specific products?
For example, we have some products that have product variations. In this case we are using the single product page. But for the products that do not have variations we simply use an add to cart link on the landing pages and skip the single product page that just adds an extra step for the customer.
I found this post which outlines how to disable ALL single product pages. But I would like to target the pages that are disabled. Either by product number or perhaps product listing type ie. variable, non variable.
What is the best way to go about doing this, without breaking WooCommerce or causing SEO issues?
To clarify: by disabled I mean remove the link to the page from areas like the shopping cart etc.
In the following functions, you will have to define one or many product IDs inside the code.
This first hooked function will remove the product from product catalog:
add_filter( 'woocommerce_product_is_visible', 'filter_product_is_visible', 20, 2 );
function filter_product_is_visible( $is_visible, $product_id ){
// HERE define your products IDs (or variation IDs) to be set as not visible in the array
$targeted_ids = array(37, 43, 51);
if( in_array( $product_id, $targeted_ids ) )
$is_visible = false;
return $is_visible;
}
To remove the link from cart items in cart page, you can use the following
add_filter( 'woocommerce_cart_item_name', 'filter_cart_item_name', 20, 3 );
function filter_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
// HERE define your products IDs (or variation IDs) to be set as not visible in the array
$targeted_ids = array(37, 43, 51);
if( in_array( $cart_item['data']->get_id(), $targeted_ids ) && is_cart() )
return $cart_item['data']->get_name();
return $product_name;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Is also possible to redirect the target products pages to the main shop.
You can add if statement around return false. You can either check for product (page) id or I would add tag (or category or custom field) to these and then in if statement you can check for this tag and if its there you return false;
In woocommerce, I am trying to find a way to only allow a product to be added a to cart only when a specific cart total amount is reached.
Example: We want to sell a bumper sticker for $1, but only if a user already has $25 worth of other products already in the cart. This is similar to Amazon's "add on" feature. However I can't find a similar WooCommerce plugin or function.
I have tried yet some code without success… Any help will be appreciated.
Can be done with a custom function hooked in woocommerce_add_to_cart_validation filter hook, where you will define:
a product Id (or multiple product Ids).
the threshold cart amount to be reached.
It will avoid for those defined products to be added to cart (displaying a custom notice) until that specific cart amount is reached.
The code:
add_filter( 'woocommerce_add_to_cart_validation', 'wc_add_on_feature', 20, 3 );
function wc_add_on_feature( $passed, $product_id, $quantity ) {
// HERE define one or many products IDs in this array
$products_ids = array( 37, 27 );
// HERE define the minimal cart amount that need to be reached
$amount_threshold = 25;
// Total amount of items in the cart after discounts
$cart_amount = WC()->cart->get_cart_contents_total();
// The condition
if( $cart_amount < $amount_threshold && in_array( $product_id, $products_ids ) ){
$passed = false;
$text_notice = __( "Cart amount need to be up to $25 in order to add this product", "woocommerce" );
wc_add_notice( $text_notice, 'error' );
}
return $passed;
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.
I have added a custom field in the woocommerce which has extra price of frames for pictures, now if picture price is 10$ and user selects a frame it will add up 5$ let say and total will be 15$.
Now if i add another product it's selected custom frame price should get added.
e.g Product 1 price is: 10$ and the frame slected on it is:frame1 whos price is 5$ so total will be 15$ for that product and if Product 2 is added with price 10$ and selected frame2 with its price is 6$ total of that product will be 16$ however grandtotal will be 31$
The solution which is near to what i'm trying to do is:
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values )
{
$price += $_SESSION['framed_price'];
return $price;
}
}
I'm storing the frame value in session and it gets updated every time user click on the frame i'm using ajax on that and till that everything is working fine. i'm getting values as well.
This function is basically iterating over the added products and adding the last frame price to every product in the cart.
How do we add the product price with it's custom frame price?
I found an answer to this, that's solve the issue:
// Change the line total price
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values ) {
// You have all your data on $values;
$price += 10;
return $price;
}
// wc_price => format the price with your own currency
function display_discounted_price( $values, $item ) {
return wc_price( $item[ 'line_total' ] );
}
Reference: woocommerce, how can i add additional cost in cart product total price?