How to display product price on custom meta filed woocommerce? - wordpress

I want to display product price on single page custom field. That filed is only readable filed users cant option to edit that price. Also on variable product page when product variations change according to that price also need to change. I have added below code for display the the custom field on single product page.
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
// global $product;
echo "test";
echo '<div id="my_custom_checkout_field">';
echo ' <input type="text" name="my_field_name">';
echo '</div>';
}
I want to add variations product price on that filed.

To get Product price without woocommerce hook
define product as global
global $product;
and then get product price
$product->get_price();
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
global $product;
$price = $product->get_price();
echo "test";
echo '<div id="my_custom_checkout_field">';
echo ' <input type="text" name="my_field_name" value=".$price.">';
echo '</div>';
}

Related

Custom button appearing on all product pages in Woocommerce

I'm currently using this code with a hook to a specific product but the button seems to be appearing for all other products on my website as well.
add_action('woocommerce_after_add_to_cart_button','additional_single_product_button');
function additional_single_product_button() {
$product_ids = is_single(871);
echo "<br>";
echo "<br>";
echo '<div>';
echo '<a data-type="inline" class="button alt" href="/contact-us/">Pay at Spa # $218 nett</a>';
echo '</div>';}
Here's my website: http://offers.elements.com.sg/
Please advise how I can just add this button for a specific product. Thank you.

ACF field value display on woocommerce category archive page

I am working on woocommerce based webiste. I have create field through ACF called Add Sub title for woocommerce product post type . I want to display that field value after Main Title on woocommerce category archive page.
add_action('woocommerce_after_shop_loop_item_title', 'display_sub_title', 3 );
function display_sub_title() {
global $product;
$id = $product->get_id();
$add_sub_title = get_field('add_sub_title',$id);
if ( $add_sub_title ) { ?>
<h5><?php echo $add_sub_title; ?></h5>
<?php }
}
I have place above code in functions.php file but it's not working. How can I display sub-title under main title on category archive page. ??
I use this and works good
function show_subtitle() {
global $product;
$id = $product->get_id();
$subtitle = get_field('sottotitolo',$id);
if ( $subtitle ) {
echo '<span>'.$subtitle.'</span>';
}
}
add_action('woocommerce_after_shop_loop_item_title', 'show_subtitle', 3 );

Show prices including tax on product page

I am using woocoommerce V3.0.9, and have tax settings enabled. I set Prices entered with tax as Yes, I will enter prices inclusive of tax and Display prices in the shop as Including Tax and Additional tax classes as Reduced Rate Zero Rate.
Also, while adding product i added product prices including tax. But on product detail page the prices are showing without tax. For example i added product price 135.90 while adding product including Tax and on product detail page its showing me price 123.55 excluding Tax but it should show 135.90 as i have set the setting to show prices including tax.
On checkout page, i am getting product price 123.55 + 12.35 Tax = 135.90 as product total price which is working fine.
But i want to show the actual price including tax on product detail page so that customer knows the original price before adding product to cart.
Can anyone help me how i can get this working.
Thanks in advance.
Before check that your WooCommerce Tax general settings match with your needs.
As suggested, you need to copy from woocommerce the templates folder inside your active child theme or theme. Then rename it woocommerce. In this woocommerce templates folder you will find inside single-product subfolder the price.php template to edit related to pricing display in single product pages.
In price.php just after:
global $product;
Replace the code with:
?>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<?php
$simple_product_price = $product->get_price_html(); // price without VAT
$simple_product_total = $product->get_price_including_tax(); // price with included VAT
$simple_product_vat = $simple_product_total - $simple_product_price; // VAT price amount
?>
<p class="price"><?php echo $simple_product_price; /* without VAT */ ?></p> (formatted)
<p class="price-vat"><?php echo $simple_product_vat; /* VAT */ ?></p>
<p class="price-and-vat"><?php echo $simple_product_total; /* With VAT */ ?></p>
<meta itemprop="price" content="<?php echo esc_attr( $product->get_price() ); ?>" />
<meta itemprop="priceCurrency" content="<?php echo esc_attr( get_woocommerce_currency() ); ?>" />
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
</div>
Because the additional prices are unformatted, you may need to mix some other elements with this additionals prices using some woocommerce php functions like:
get_price_suffix( ) // Get the suffix to display after prices > 0.
$currency = esc_attr( get_woocommerce_currency( ) ) // Get the currency code.
get_woocommerce_currency_symbol( $currency ) // Get the currency symbol.
get_tax_class( ) // Returns the tax class.
get_tax_status( ) // Returns the tax status.
Woocommerce Reference Link: https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html

add Custom text field in single product page

I am working on a project in woocommerce.
Can anyone help me to add a custom text field next to "add to cart"?
I tried the following but it is not working.
add_action( 'woocommerce_after_single_product_summary', 'add_custom_field', 0 );
function add_custom_field() {
global $post;
echo "<div class='prod-code'>Product Code: ";
$text= get_field('txt-field', $post);
echo "</div>";
return true;
}
Perhaps try using $post->ID like this:
add_action( 'woocommerce_after_single_product_summary', 'add_custom_field', 0 );
function add_custom_field() {
global $post;
echo "<div class='prod-code'>Product Code: ";
$text= get_field('txt-field', $post->ID);
echo "</div>";
return true;
}
put your html code inside content-product.php (wp-content/themes/YOUR THEME/woocommerce/content-product.php)
content-product.php contains code of list page in wordpress. if you want to edit product page than edit content-single-product.php.
You are using Woocommerce there are two case :
You are reading post ID
You want to display a SKU
You can implement it via single function :
add_action( 'woocommerce_single_product_summary', 'display_productCode', 5 );
function display_productCode(){
global $product;
echo 'SKU: ' . $product->get_sku();
echo 'Product ID:'.$product->get_id();
}

Display product variations on category page

How do I show all product variations on WooCommerce category pages?
I have set up a variable product with three variations. I want each variation (not the variable product itself) to be displayed as a separate product on the product category page. The user should be able to open a variable product and add it to the cart.
Is this possible?
Example : http://bennyhendrikx.be/product-categorie/clothing/page/2/
Add this code in youre functions.php
if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {
function woocommerce_template_loop_add_to_cart() {
global $product;
if ($product->product_type == "variable" && (is_product() || is_product_category() || is_product_tag())) {
woocommerce_variable_add_to_cart();
}
else {
woocommerce_get_template( 'loop/add-to-cart.php' );
}
}
}
add_action('woocommerce_after_shop_loop_item_title','woocommerce_template_single_variation', 5);
function woocommerce_template_single_variation() {
global $product;
if ($product->product_type == "variable" && (is_product_category() || is_product_tag())) {
echo woocommerce_variable_add_to_cart();
}
}
Hi You can just paste this code in your theme functions.php
here is code
/**
* Replace add to cart button in the loop.
*/
function iconic_change_loop_add_to_cart() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
add_action( 'woocommerce_after_shop_loop_item', 'iconic_template_loop_add_to_cart', 10 );
}
add_action( 'init', 'iconic_change_loop_add_to_cart', 10 );
/**
* Use single add to cart button for variable products.
*/
function iconic_template_loop_add_to_cart() {
global $product;
if ( ! $product->is_type( 'variable' ) ) {
woocommerce_template_loop_add_to_cart();
return;
}
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'iconic_loop_variation_add_to_cart_button', 20 );
woocommerce_template_single_add_to_cart();
}
/**
* Customise variable add to cart button for loop.
*
* Remove qty selector and simplify.
*/
function iconic_loop_variation_add_to_cart_button() {
global $product;
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<button type="submit" class="single_add_to_cart_button button"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>
<?php
}
Even after so many years this one is not so simple for woocommerce.
The above examples shows the parent products with available variations below.
You maybe want to show the variations as simple products.
It's better to use a plugin for this. The Woocommerce single variations by weLaunch https://www.welaunch.io/en/product/woocommerce-single-variations/
it's the best i found so far. It gives you the opportunity to even exclude one variation from the category page (Most of the times, your products have 2 variations color/size. You simple want to show only the color variation).
If you want to go with custom code. You have to keep in mind, the images, pagination, filters, product links with variation parameters.
Woocommerce works with parent products on its core.

Resources