I want to remove the price range that is shown for variable products in the WooCommerce site I'm working on. On non-product pages I want to replace it with "from: [lowest price]" and on the product page I'd like to replace it simply with the price of the selected variation.
Any ideas how I can get it working that way?
Thanks,
Darren
The best possible way to do what you want is that you change it on non-product page and remove it from product page. On product page when you select a variaion, its price and remaining stock is shown below the dropdown. So there is no need for you to show it above. If you still want to do it you will need a js solution.
Try this, this is tested solution. I have commented the cases for better guidance and changing if you want.
function sr_change_variable_price($price, $product)
{
if ( $product->is_type( 'variable' ) && !is_product() )
{
return 'From: '.$product->get_variation_price( 'min' ); // if variable product and non-product page
} else if ( $product->is_type( 'variable' ) && is_product() )
{
return ''; // if variable product and product page
} else
{
return $price; // other cases
}
}
add_filter( 'woocommerce_get_price_html', 'sr_change_variable_price', 10, 2 );
Related
In WooCommerce, I understand well that woocommerce_get_order_item_totals filter kook is used to customize order total rows like reordering them.
add_filter( 'woocommerce_get_order_item_totals', 'custom_order_of_from_order_table', 10, 2 );
function woocommerce_get_order_item_totals( $total_rows, $order ) {
// code here
return $total_rows;
}
I have tried to reorder the subtotal over the total, and the payment method below the total without success on WooCommerce ThankYou page. My PHP knowledge is very limited and I appreciate any help.
How to customize total rows from order table, reordering them on WooCommerce thankyou page?
The following will reorder items totals as desired on Woocommerce thankyou (order received) page only:
add_filter( 'woocommerce_get_order_item_totals', 'reordering_order_item_totals', 10, 3 );
function reordering_order_item_totals( $total_rows, $order, $tax_display = '' ){
// Only on "order received" thankyou page
if ( ! is_wc_endpoint_url('order-received') )
return $total_rows;
$sorted_items_end = array('cart_subtotal', 'order_total', 'payment_method');
$sorted_total_rows = array(); // Initializing
// Loop through sorted totals item keys
foreach( $sorted_items_end as $item_key ) {
if( isset($total_rows[$item_key]) ) {
$sorted_total_rows[$item_key] = $total_rows[$item_key]; // Save sorted data in a new array
unset($total_rows[$item_key]); // Remove sorted data from default array
}
}
return array_merge( $total_rows, $sorted_total_rows); // merge arrays
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
To make that work everywhere for customer orders and email notifications, just remove:
// Only on "order received" thankyou page
if ( ! is_wc_endpoint_url('order-received') )
return $total_rows;
I am trying to set the price of all related products on the products page to 10% less = 0.9
The goal is to provide a discount of all related products on the product page but when viewed as a product, give the normal price.
Overall, the idea is to provide incentive that generates up-selling of related products.
I am asking for two things here. ONE: change the product price for related products on the product page (10% off) and TWO: carry that discounted price into the cart and checkout when the related discounted product is added to cart from the product page.
I almost got the first part down, but the code I'm trying to get working is giving me an error saying:
Warning: A non-numeric value encountered
My code so far:
add_filter( 'woocommerce_get_price_html', 'related_product_price_discount', 100, 2 );
function related_product_price_discount( $price, $product ) {
global $woocommerce_loop;
// make sure this is a related product on product page
if( is_product() && $woocommerce_loop['name'] == 'related' ){
$price = $price * 0.9;
}
// return related product price with discount
return apply_filters( 'woocommerce_get_price', $price );
}
The rule in StackOverFlow is one question at the time, so I will answer only your first question, which is related to your code issue...
Note that the hook woocommerce_get_price_html is related to the displayed formatted price.
To avoid the error "Warning: A non-numeric value encountered", you will use the following:
add_filter( 'woocommerce_get_price_html', 'related_product_price_discount', 100, 2 );
function related_product_price_discount( $price_html, $product ) {
global $woocommerce_loop;
// make sure this is a related product on product page
if( is_product() && $woocommerce_loop['name'] == 'related' ){
$price_html = wc_price( wc_get_price_to_display( $product ) * 0.9 );
}
// return related product displayed discounted formatted price
return $price_html;
}
Code goes in functions.php file of your active child theme (active theme). Tested and works.
I need functionality for "friend" customers, who can get products with better price. User role based solution is not what I'm looking for and customer wants it to work with coupons. They give coupon code to their customers and they get for example 8€ off from REGULAR price on certain products. The problem is, by default WooCommerce coupons calculate it from smallest price. If sale price is set, then this calculation is wrong and the customer gets it too cheap. Basically, I want coupon to give certain products fixed "friend" price.
I've been googling around and can't find a ready solution for it. Any suggestions how to solve this problem are welcome.
I have checked multiple plugins, but none of them has filled my requirements.
Add to functions.php. Based off of this code https://brilliantinfo.net/apply-coupon-discount-on-regular-price-in-woocommerce/
Modified for coupon type of fixed_product. If you need this to only work for a specific coupon then I can modify for that. (As it stands this will apply to any fixed_product coupons that are used)
add_action( 'woocommerce_before_calculate_totals', 'adjust_cart_coupon', 10, 1);
function adjust_cart_coupon( $cart_object) {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
return;
}
$coupon = False;
if ($coupons = WC()->cart->get_applied_coupons() == False ) {
$coupon = False;
} else {
foreach ( WC()->cart->get_applied_coupons() as $code ) {
$coupons1 = new WC_Coupon( $code );
if ($coupons1->type == 'fixed_product'){
$coupon = True;
}
}
}
if ($coupon == True){
foreach ( $cart_object->get_cart() as $cart_item ){
$price = $cart_item['data']->regular_price;
//sets cart item to use regular price and not sale price
$cart_item['data']->set_price( $price );
}
}
}
The Discount Rules for WooCommerce plugin might fit your requirements.
With it you can apply a coupon, or any discount rule, to specific customers.
Also there is a setting to Apply discount based on Sale price or Regular price.
I am trying to find a way to edit a product url in WooCommerce so that when visited a quantity is pre-selected for the quantity input, e.g. 2.
I know there is an add to cart url for it:
http://yourdomain.com/?add-to-cart=47&quantity=2
But was wondering if it was possible to just input the quantity on the product page with a similar URL.
Thanks for any ideas on the matter.
you can do it like this:
add_filter( 'woocommerce_quantity_input_args', 'custom_woocommerce_quantity_input_args' ); // Simple products
add_filter( 'woocommerce_available_variation', 'custom_woocommerce_quantity_input_args' ); // Variations
function custom_woocommerce_quantity_input_args( $args ) {
if ( isset( $_GET['qty'] ) && is_numeric($_GET['qty']) ) {
$args['input_value'] = $_GET['qty'];
}
return $args;
}
with that you can then do http://yourdomain.com/product/ship-your-idea/?qty=10.
I've seen many examples of adding an item to the WC cart with a customer price, but none dynamically. I am trying to do in a shortcode function that receives a POST variables....
if (isset($_POST['wmnf_add_donation'])) {
global $woocommerce;
$cart_object = $woocommerce->cart;
$custom_price = ($_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0);
$target_product_id = 65986;
$cart_object->add_to_cart($target_product_id, "1");
foreach ( $cart_object->cart_contents as $key => $value ) {
if ( $value['product_id'] == $target_product_id ) {
$value['data']->price = $custom_price;
}
}
}
This adds the item to the cart of course, but the price is zero and I realize I need to somehow save this array back to the WC cart data. Is this method even possible or can it only be done via a filter or action hook? If so, how can I save the changed array back to the cart contents or make it work to add the one item with its posted price? Any guidance greatly appreciated.
Thanks for that answer doublesharp, I was not able to get it to work as described because the form was posting to the page with my shortcode, which has my form, instead of posting directly to the cart. The $_POST was not being seen and the product ended up zero. I did find another approach, but having a problem using wp_redirect. I changed the above shortcode to this:
if (isset($_POST['wmnf_add_donation'])) {
global $woocommerce;
$custom_price = ($_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0);
$target_product_id = 65986;
$_SESSION['donation_amount'] = $custom_price;
$woocommerce->cart->add_to_cart($target_product_id, "1");
wp_redirect( site_url() . '/gifts/swag-bag/');
}
Then I added the following filters to functions.php:
add_filter('woocommerce_get_price','donation_price', 10, 2);
add_filter('woocommerce_get_regular_price','donation_price', 10, 2);
add_filter('woocommerce_get_sale_price','donation_price', 10, 2);
function donation_price($price, $productd){
if($productd->id == '65986'){
$price = $_SESSION['donation_amount'];
}
return $price;
}
This does not work except when wp_redirect is commented out, hence, not redirecting. The above redirects to the cart, but its empty. If I comment out the wp_redirect line and then manually go to the cart, the product is there with my custom price. Actually, I would like to apply a custom price and redirect directly to the checkout page instead of the cart, if possible?
You can use the woocommerce_before_calculate_totals action hook to modify the contents of the cart, including the product prices.
add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals' );
function before_calculate_totals( $_cart ){
// loop through the cart_contents
foreach ( $_cart->cart_contents as $cart_item_key => &$item ) {
// you will need to determine the product id you want to modify, only when the "donation_amount" is passed
if ( $item['id'] == 65986 && isset( $_POST['donation_amount'] ) ){
// custom price from POST
$custom_price = $_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0;
// save to the cart data
$item['data']->price = $custom_price;
// new versions of WooCommerce may require (instead of line above)...
// $item['data']->set_price($custom_price);
}
}
}