echo product quantity in cart woocommerce - php

I want to display product quantity in the cart, on a single product page as text.
This is currently what I'm using in functions.php, I want to display the quantity number between.
add_action('woocommerce_before_add_to_cart_form', 'gripsquantity', 5);
function gripsquantity(){
echo 'Choose ';
echo 'Grips';
}
So it will be "choose 3 grips" or "choose 1 grip" depending on the product quantity in the cart

Related

Woocommerce Cart Subtotal Display Wrong Amount

I use cart Widget as minicart details in top menu. In Mini cart details page display wrong cart subtotal (Product Amount without Discount). but checkout page display correct total amount how to fix it.
if ( WC()->cart->get_cart_contents_count() > 0 ) { echo '<span class="circle" id="mini-cart-details">' . WC()->cart->get_cart_contents_count() . ' items - '.WC()->cart->get_cart_total().'</span>'; } else { echo '<span class="circle" id="mini-cart-details"> items - '.WC()->cart->get_cart_total().'</span>'; }
I have the same issue with taxes. $order->get_formatted_order_total() returns incorrect and different value than $order->get_total().

Check if WooCommerce product (simple or variations) are in stock and display label as shortcode

I would like to have a shortcode that I can add to a WooCommerce product page. The shortcode would simply check the product stock and say "In stock" if it has any. And then "Out of stock" if the product (or any of the variations) don't have inventory.
This shortcode should work for both Simple products and Variable products. With Simple products, it would just check the stock quantity. For Variable products, it would check the stock quantity of ALL of the variations (since the variable product inventory is managed at the inventory level). If ANY of the variations have stock, then it would still return as "In stock".
It would only say "Out of stock" if ALL of the variations of a particular product were 0.
I plan to use this shortcode within a product page template I am building.
For now I've created this by:
/* Create stock checker of overall product */
add_shortcode( 'fs-product-stock-status', 'fs_product_stock_status_shortcode' );
function fs_product_stock_status_shortcode( $atts ) {
// begin output buffering
ob_start();
$stockstatus = get_post_meta( get_the_ID(), '_stock_status', true );
if ($stockstatus == 'outofstock') {
echo '<p class="stock out-of-stock">Out of stock</p>';
}
elseif ($stockstatus == 'instock') {
echo '<p class="stock in-stock">In stock</p>';
}
// end output buffering, grab the buffer contents, and empty the buffer
return ob_get_clean();
}
But I'm open to other solutions that may be better. This seems to be working though.

When price is 0 change add to cart button to "request quote" [duplicate]

This question already has answers here:
Replace product zero displayed price with a custom text in Woocommerce 3
(1 answer)
Customize "Add to cart" button for a specific product category in WooCommerce
(2 answers)
Closed 4 years ago.
I have a product X in several sizes (added as variation products of the main product X). For small sizes the price is available and customers can shop it online. However, for large sizes of this product X, it is not shopable online. Therefore for some variations of this product X I would like to remove the add to cart button and change it to a button who goes to my "request a quote" page.
My idea was that if I set the variable product price to 0 that the add to cart button goes away and the "request a quote" button appears.
Any ideas on how to do this in woocommerce (php)?
put this function in your function.php
add_filter('woocommerce_get_price_html', 'requestQuote', 10, 2);
function requestQuote($price, $product) {
if ( $price == wc_price( 0.00 ) ){
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_add_to_cart', 30 );
return 'Request Quote';
}
else{
return $price;}
}
you just need to make some condition with if function
$price = 2000;
$response = 'Add To Cart;
if(size = XL)
{
$price = 0;
$response = "You can Request another Size";
}

Adding Custom price with woocomerce product price in Cart & Checkout

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?

Woocommerce - Change price display in cart and checkout page

I'm creating a webshop with WooCommerce in WordPress, using swedish krona (SEK) as currency. It should be displayed after the price, i.e. "2999 kr", and not "kr2999", but I can't get it to work on the cart and the checkout page, and not cart in the header.
I've found this filter for the cart total price, which is working but only on the total price in checkout:
functions.php
add_filter('woocommerce_cart_totals_order_total_html', 'total_price');
function total_price() {
$price_html = substr(WC()->cart->get_total(),33);
$replace_price = substr_replace($price_html, ' kr', 49);
$value = '<strong>' . $replace_price . '</strong> ';
return $value;
}
How do I do this for every price display in cart and checkout and cart in header? It should be similar to the code above?

Categories