woocommerce how to hook into checkout page - woocommerce

I would like to add additional information / update the checkout table in woocommerce. This is the table i mean:
I would like to edit the quantity and the title of the product.
I have had a look into woocommerce hooks and came across the following:
woocommerce_checkout_cart_item_quantit
So i made a test function to have a play:
function add_meta_on_checkout_order_review_item( $quantity , $cart_item , $cart_item_key ) {
echo $cart_item[ 'quantity' ] + 5;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'add_meta_on_checkout_order_review_item', 1, 3 );
This however has no effect. Am i hooking into the right woocommerce hook?

Try changing this: add_filter( 'woocommerce_checkout_cart_item_quantity', 'add_meta_on_checkout_order_review_item', 1, 3 );
To this : add_filter( 'woocommerce_checkout_cart_item_quantity', 'add_meta_on_checkout_order_review_item', 10, 3 );
You can also try to return instead of echo the quantity.

Maybe I am little late here but you can try this:-
add_filter('woocommerce_cart_item_name','add_meta_on_checkout_order_review_item',10,3);
And this would surely help to add the meta data under the Title of the product.
Thanks,

Related

Add a shortcode above the WooCommerce product short description

I am trying to add a shortcode to all my woocommerce single product pages. I want the shortcode to appear above the short description.
I have found this code and added it to functions.php, but I can see that it adds the shortcode below the short description (and above the add to cart button).
add_filter('woocommerce_short_description','ts_add_text_short_descr');
function ts_add_text_short_descr($description){
$text = do_shortcode('[my-shortcode]');
return $description.$text;
}
I would be very gratefull for some help :)
function customShortcodeBeforeShortDescription( $post_excerpt )
{
echo do_shortcode('[wpqr-code]');
}
add_filter('woocommerce_short_description','customShortcodeBeforeShortDescription', 10, 1);
You can use the woocommerce_single_product_summary action hook. try the below code. The code will go in your active theme functions.php file.
function add_shortcode_before_short_description(){
echo do_shortcode( '[wpqr-code]' );
}
add_action( 'woocommerce_single_product_summary', 'add_shortcode_before_short_description', 15, 1 );
Tested and works

URL to product page with quantity inputed - WooCommerce

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.

Automatic discount conditionated on checkout field: Woocommerce

I want to apply a discount if a custom checkout field is filled.
That's the code i use to generate the discount:
function custom_wc_add_discount() {
$0tax = WC()->cart->subtotal * -0.22;
WC()->cart->add_fee( '0% tax', $0tax );
}
add_action( 'woocommerce_cart_calculate_fees','custom_wc_add_discount' );
And I want to condition it with something like this:
if ( $_POST[billing_vat]){
Edit: I solved the condition problem creating a coupon like this
add_action( 'woocommerce_checkout_process', 'apply_tax_coupon' );
function apply_tax_coupon() {
global $woocommerce;
$coupon_code = '0%val';
$woocommerce->cart->remove_coupon( $coupon_code );
if ( $_POST[billing_vat]){
$woocommerce->cart->add_discount( $coupon_code );
}
wc_print_notices();
}
But now in checkout page it doesn't show the discounted total.
Any help is appreciated.
You have some problems :
You can't start php var with a number
you missed the quote arroung billing_vat in the condition
Try resolve this and we will see if there is other things to do.

woocommerce get product id when product is add to cart

I need to do extra step in the backend when a product is add to cart.
I need to get the product ID juste after it's add to the cart.
I use the woocommerce hook woocommerce_add_to_cart
add_action('woocommerce_add_to_cart', 'attach_item');
function attach_item() {
// I need to have the product id here.
}
I tried many way to get the ID but nothing work.
Any idea ...
Today I also get the same problem but I got the solution.
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart',10,2 );
function woo_custom_add_to_cart( $cart_item_data,$productId ) {
var_dump($productId);
}
At the time of writing this, the hook is actually called from WooCommerce like so: (source)
do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
Add your custom method like so:
add_action('woocommerce_add_to_cart', function($cartItemKey, $productId) {
// do sth with $productId
}, 10, 2);

hook into woocommerce_variation_price_html wordpress

I am working on a plugin for woocommerce and came across the hook "woocommerce_variation_price_html" which allows you to hook into the variation prices drop down on single products. So created a quick function to test and have a play:
add_action( 'woocommerce_variation_price_html' , 'wholesale_variation_price' );
function wholesale_variation_price($term){
return '';
}
The above works nicely and removes all data. However i am trying to add custom meta_data to replace the default values.
So i then did the following:
add_action( 'woocommerce_variation_price_html' , 'wholesale_variation_price' );
function wholesale_variation_price($term){
$var_price = get_post_meta( get_the_id(), '_my_price', true );
return $var_price;
}
This for some reason does not work? Has anyone had any experience working with this hook in woocommerce? There is not much documentation on that hook anywhere.
Any help would be greatly appreciated!
As already indicated that you can read here about filters for html.
Maybe you have Sale price set for variation? Then in link you can see that there are total of 4 filters. For: price, sale price, free and no price.
This code works
add_filter( 'woocommerce_variation_sale_price_html', 'my_html', 10, 2);
add_filter( 'woocommerce_variation_price_html', 'my_html', 10, 2);
function my_html( $price, $variation ) {
return woocommerce_price(5);
}
Also this one works too. This allows you to modify whole variation. Read in code here
add_filter( 'woocommerce_available_variation', 'my_variation', 10, 3);
function my_variation( $data, $product, $variation ) {
$data['price_html'] = woocommerce_price(6);
return $data;
}
Screenshots:
First example
Second example
Works like magic!
P.S. you can see prices like this in screenshots because I have design that show them like that.
EDIT 2016-09-10
Version 2.6+
As woocommerce_price is deprecated in 2.6 then use wc_price for newer versions.
// Display Price For Variable Product With Same Variations Prices
add_filter('woocommerce_available_variation', function ($value, $object = null, $variation = null) {
if ($value['price_html'] == '') {
$value['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
}
return $value;}, 10, 3);
By looking at WooCommerce source code, I see that woocommerce_variation_price_html is a filter, not an action. I don't know how you managed to get your sample code to work like that... weird.
Try this:
function wholesale_variation_price( $price, $product ) {
$var_price = get_post_meta( $product->variation_id, '_my_price', true );
return $var_price;
}
add_filter( 'woocommerce_variation_price_html', 'wholesale_variation_price', 10, 2 )
I used this to modify the variation prices to show the price for each variation product excluding tax before and including tax after :)
add_filter( 'woocommerce_available_variation', 'my_variation', 10, 3);
function my_variation( $data, $product, $variation ) {
$data['price_html'] = "<span class='ex-vat-price'>ex. " . woocommerce_price($variation->get_price_excluding_tax()) . "</span><br>";
$data['price_html'] .= "<span class='inc-vat-price'>inc. " . woocommerce_price($variation->get_price_including_tax()) . "</span>";
return $data;
}

Resources