We are selecting the number of products to insert in cart. We are using woocommerce subscription plugin for variation on yearly and monthly basis.
if We are selecting 5 products to insert in cart we are using the while loop for getting variation product related to monthly and yearly with count 5 and using code to insert in cart as.
WC()->cart->add_to_cart( $parent_id, $quantity = 1, $variation_id );
it is not working with this code as we want 5 different products to add in cart. but is use the code with variation_id it is working but the product remains the same.
WC()->cart->add_to_cart( $parent_id, $quantity = 1, $variation_id = 2918 );
this code is working but with the same product 5 times.
Need help for it Thanks
Related
I have a woocommerce subscription product with two variations (Pay once and Pay over 12 months) in my shop/product pages the displayed price is the one of the variation (Pay over 12 months which is for ex. "From $19.9/month"). I want to show the price of the other variation (Pay once) which is the full price of the product. How can i do that?
I've tried this but it's not working
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
$price = '';
$price .= woocommerce_price($product->max_variation_price);
return $price;
}
FYI : i have other variable products without subscription, i just want to do that only for the subscription product.
This question already has answers here:
Remove add to cart button for specific product categories in WooCommerce 3
(2 answers)
Closed 4 years ago.
I am developing an ecommerce website using woocommerce. I have two product categories:
Juices
Cleans
And i intend to sell subscription from the website.
Scenario:
When a user selects a product category and fills in the number of days of subscription and relevant details then the user is redirected to that product category page to be able to add products to cart.
Problem:
I need a way to disable add to cart button for other category products like if Juices is selected the cleans should be disabled and vice versa. so that user can select only one type of subscription in a purchase to keep the system hassle free.
What is the hook or way in woocommerce to do it?
You can achieve this task using following code in your theme's functions.php file.
//Remove add to cart button
function remove_add_to_cart_button()
{
global $product;
global $woocommerce;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values )
{
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' ); // Get all assigned categories
$_categoryslug='';
foreach ($terms as $term)
{
$_categoryslug[] = $term->slug; // Get category slug to compare
}
}
// Check if cart item is added for one category then restrict all other categories product
if ( !has_term( $_categoryslug, 'product_cat', $product->id ) )
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action('wp','remove_add_to_cart_button');
This code is working for me so I hope it will helpful to you as well.
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.
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 );
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?