Displaying by default shipping fields on checkout page - php

On my WooCommerce checkout page, I am selling only one product. The checkout page is showing this product and there is ckeckbox next to it, to show the shipping details, when clicked. This is on the default form-checkout.php WooCommerce template.
I would like to have this ckeckbox always selected, to show billing details by default.
I have tried to set:
$checkout = 1;
With no luck so far. The radio button is displayed by the following hook:
<?php do_action( 'woocommerce_checkout_before_customer_details'); ?>
Any help letting me know how I can achieve this will be very helpful.
Thanks.

If you look to the code of checkout/form-shipping.php WooCommerce template, inside the <h3> tag, where the checkbox code is located, there is woocommerce_ship_to_different_address_checked hook that you can use to achieve that. Here is an extract of this code template, that shows it:
?>
<div class="woocommerce-shipping-fields">
<?php if ( true === WC()->cart->needs_shipping_address() ) : ?>
<h3 id="ship-to-different-address">
<label for="ship-to-different-address-checkbox" class="checkbox"><?php _e( 'Ship to a different address?', 'woocommerce' ); ?></label>
<input id="ship-to-different-address-checkbox" class="input-checkbox" <?php checked( apply_filters( 'woocommerce_ship_to_different_address_checked', 'shipping' === get_option( 'woocommerce_ship_to_destination' ) ? 1 : 0 ), 1 ); ?> type="checkbox" name="ship_to_different_address" value="1" />
</h3>
<div class="shipping_address">
So you can use this hook this way, to get this checkbox always selected and displays billing details:
add_filter( 'woocommerce_ship_to_different_address_checked', '__return_true');
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and fully functional.

Related

Add tooltip to "conditionnal fee" in Woocommerce

I want to make a tooltip for the "conditionnal fees" option in the cart.
I need to put the "Question mark" icon next to "Fee" and when I click on it, a prompt should tooltip with a description of the fee terms.
I'm trying to add the icon via the function.php file of my child theme.
function action_woocommerce_after_fee( $method, $index ) {
if( $method->get_class() == 'fee' ) {
echo '<th>Example CSS Tooltip <span data-tooltip="Tooltips are used to provide information about an element on a web page.">i</span></th>';
}
}
add_action( 'woocommerce_after_fee', 'action_woocommerce_after_fee', 1 );
I think my problem that the element is in an array and can't reach the "th" tag
<tr class="fee">
<th>Service</th>
<td data-title="Service">
<span class="woocommerce-Price-amount amount">
<bdi>4,82 <span class="woocommerce-Price-currencySymbol">€</span></bdi>
</span>
</td>
</tr>
Tell me how this icon can be added to the "conditionnal fees" option using hooks in the Functions.php file? I just don't want to add this code to WooCommerce templates. And how can you make this code work in the cart and on the checkout page?
I will be glad for your help!

How to make popup on the WooCommerce listing page

I am making a WooCommerce website where I want to have a pop-up when someone clicks on apply now button. There I want to have two fields 1) All the class(custom field) that product has. 2) Kids that the current logged in user has.
And there would be Add to Cart button which would add the item inside the cart.
I have successfully been able to get those on my product page. I am using toolset https://toolset.com to create my product listing page and single product page.
This is my code for a single product list (Template View)
<div class="school">
<div class="top">
<div class="image">
[types field="logo"][/types]
</div>
<div class="city">
<img src="link_to_img">
[types field="city"][/types]
</div>
</div>
<div class="middle">
<p>[wpv-post-title]</p>
</div>
<div class="bottom">
[wpv-post-read-more]
<button class="applynow" href="#">Apply Now</button>
</div>
</div>
Also this is the code to render those fields 1 & 2 in the popup. I am using this in my single product page.
function iconic_output_engraving_field() {
global $product;
$classes = get_post_meta($product->get_id(),'wpcf-classes-opened-for-admission',array('show_name' => 'true'));
print_r("<label>Select Class</label>");
print_r("<select name='class'>");
print_r("<option disabled selected value> -- Select a Class -- </option>");
foreach ($classes as $class) {
print_r( "<option value='".$class[0]."'>".$class[0]."</option>");
}
print_r("</select>");
$args = array(
'id' => 1538,
);
echo render_view( $args );
}
add_action( 'woocommerce_before_add_to_cart_button', 'iconic_output_engraving_field', 10 );
How can I pass the id through the button, so that I can create a template which loads up with correct data and Add to Cart button.
https://wordpress.org/plugins/easy-login-woocommerce/
This plugin does what you're looking for, BUT it uses wp_user_login or some other function that you would need to change.
Basically, download, check the code of the plugin, your answer should be there.
Plugin is to create a popup when a button is clicked, plugin uses it to login/register, but you could edit to do whatever.

Add order customer note to YITH Woocommerce PDF Invoice

In Woocommerce I am using a plugin called YITH WooCommerce PDF Invoice and Shipping List and I would like to add customer note to the PDF invoice.
I would like to add it after the first span line in the code below:
<span class="notes-title"><?php _e( "Notes", "yith-woocommerce-pdf-invoice" ); ?></span>
<div class="notes">
<span><?php echo nl2br( $notes ); ?></span>
<?php do_action( 'yith_ywpi_after_document_notes', $document );?>
</div>
</div>
<?php
But i can't figure out how to get the customer note from $document variable.
I have tried to use this answer thread: "Display Customer order comments (customer note) in Woocommerce" which looks pretty much like the same problem but still could'nt figure it out as $document->order->customer_message; doesn't work.
Any help is appreciated.
Since Woocommerce 3 you can't access anymore properties From the WC_Order object. You need to use the WC_Order method [get_customer_note()][1].
So from the $document (YITH global object) you will use:
$document->order->get_customer_note();
To add customer notes to YITH invoice you can choose between 2 ways:
1) Using the available yith_ywpi_after_document_notes action hook:
add_action( 'yith_ywpi_invoice_template_products_list', 'add_customer_notes_after_document_notes', 5 );
function add_customer_notes_after_document_notes( $document ) {
?><span><?php echo $document->order->get_customer_note(); ?></span><?php
}
Code goes in function.php file of your active child theme (or active theme). Untested (as I dont have the premium version of the plugin) but it should work normally (depending on the plugin settings).
2) Overriding templates (in your provided code):
<span class="notes-title"><?php _e( "Notes", "yith-woocommerce-pdf-invoice" ); ?></span>
<div class="notes">
<span><?php echo nl2br( $notes ); ?></span>
<span><?php echo $document->order->get_customer_note(); ?></span>
<?php do_action( 'yith_ywpi_after_document_notes', $document );?>
</div>
</div>
<?php
It should work.
For the free plugin version
There is no available hooks (like in the provided code)…
The YITH PDF global object need to be called and it's not $document.
So you will be able to use the following code in templates/invoice/invoice-footer.php template:
<?php global $ywpi_document; echo $ywpi_document->order->get_customer_note(); ?>

Stop the redirection after WooCommerce add to cart

I wish to completely remove any redirection after a user click on the ADD TO CART button.
Actually I am not using the products page.
I am using a simple button with the link to the product, like this: ?add-to-cart=492.
My user will click on several "add to cart" buttons on my page, so he cant be redirected to any page after clicking in the first button.
At the end of the page he will find a CHECKOUT button to pay and that is it.
Any ideas how to achieve this?
Thanks
Update:
Your simple html "add-to-cart" button links are actually for example like that (the href value):
<a href="http://my-domain.com/site2/?add-to-cart=492" target="_self" class="button white is-larger carrinho">
<span>ESCOLHER PACOTE</span>
</a>
So they are redirected each time to your home page
2 SOLUTIONS:
1) Use the WooCommerce short code [add-to-cart] this way:**
Without price: [add_to_cart id="492" show_price="false"]
With the price: [add_to_cart id="492"]
2) Html code in your page text editor - To prevent redirections, the href attribute should be:
<a href="?add-to-cart=492" class="button white is-larger carrinho">
<span>ESCOLHER PACOTE</span>
</a>
This time your customers will not be redirected as before…
THE CHECKOUT BUTTON
To finish, here is a custom shortcode that will output the "Proceed to checkout" button:
if( !function_exists('proceed_to_checkout_button') ) {
function proceed_to_checkout_button() {
$checkout_url = wc_get_checkout_url();
$button_txt = __('Proceed to checkout', 'woocommerce');
$output = '<div class="wc-proceed-to-checkout">
<a href="'. $checkout_url .'" class="checkout-button button alt wc-forward">
'. $button_txt .'
</a>
</div>';
return $output;
}
add_shortcode( 'checkout_button', 'proceed_to_checkout_button' );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Usage: just add this to your pages editor: [checkout_button]
Original answer:
First, In WooCommerce settings you need to:
Enable **Ajax on add-to-cart button (Woocommerce > Settings > Products > Display)
Disable the add-to-cart button redirection (Woocommerce > Settings > Products > Display)
Then you can add a custom "Proceed to checkout" button using:
Any classic WordPress menu (Appearance > Menus)
With that custom code on single product pages and product archives:
add_action('woocommerce_after_single_product', 'custom_checkout_button', 100);
add_action('woocommerce_after_shop_loop', 'custom_checkout_button', 100);
function custom_checkout_button() {
$checkout_url = wc_get_checkout_url();
$button_txt = __('Proceed to checkout', 'woocommerce');
?>
<div class="wc-proceed-to-checkout">
<a href="<?php echo $checkout_url; ?>" class="checkout-button button alt wc-forward">
<?php echo $button_txt ?>
</a>
</div>
<?php
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The button "Proceed to checkout" will be show at the bottom of this pages.
If you want to skip the cart page:
add_action('template_redirect', 'skip_cart_page_redirecting_to_checkout');
function skip_cart_page_redirecting_to_checkout() {
// If is cart page and cart is not empty
if( is_cart() && ! WC()->cart->is_empty() )
wp_redirect( wc_get_checkout_url() );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
All code is tested and works.
Like in the woocommerce itself does in the product-code shortcode you could just use the filter for the simple-add to cart button 'woocommerce_add_to_cart_form_action'.
// Change form action to avoid redirect.
add_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );

The title from theme options is not showing up

I want to show dynamic title that means user can add his own title from theme options, but it's not going up. My theme options page is working fine and even my title is showing up in the options page ... while unable to show on page.
Here is mine code for theme options.
<!-- Option 2: Custom Heading for banner -->
<tr valign="top">
<th scope="row"><?php _e( 'Heading for page banner' ); ?></th>
<td>
<input id="theme_settings[heading]" type="text" size="36" name="theme_settings[heading]" value="<?php esc_attr_e( $options['heading'] ); ?>" />
<label for="theme_settings[heading]"><?php _e( 'Enter your desired title here' ); ?></label>
</td>
</tr>
It shows a good title like shown below in image theme options page!
Now the problem is that once I use code in my main page it never shows up, while its saved in WordPress as shown in red rectangle in above image.
Here is the code I am using in my main page, that code is being used in 2 pages, index.php and frontpage.php:
<?php if($options['heading']) { ?>
<h1><?php echo $options['heading']; ?></h1>
<?php } else { ?>
<h1>a fresh start for the family</h1>
<?php } ?>
Can any one point out the mistake?
Image removed as I could not upload ... simply means that my theme options allow me to add title in backend and even saved that title but now my page still shows that static title.
If you are not getting your value from database then you can use get_option to simply fetch your value and then you can proceed.
Here is the example code snippet:
<p><?php echo esc_html( sprintf( __( 'Character set: %s', 'textdomain' ), get_option( 'blog_charset' ) ) ); ?></p>
Here is the full WordPress get_option page reference URL ... https://developer.wordpress.org/reference/functions/get_option/

Categories