Disable Chrome Autocomplete/Dropdown Suggestions on WooCommerce Checkout - woocommerce

I'm trying to disable Chromes Autofill/Dropdown suggestions for my customers when checking out. I have an address validation tool, but customers are still using Chromes drop down suggestions, and at times, street names and other detail is left out, so I'm having to contact customers manually for the information.
I've tried the following code for removing autocomplete for billing address, since text is the input type without success.
add_filter( 'woocommerce_form_field', 'change_autofill', 1, 1 );
function change_autofill( $field) {
$agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($agent, 'Firefox') !== false) {
$field = str_replace('autocomplete="text"', 'autocomplete="off"', $field);
return $field;
}
else {
$field = str_replace('autocomplete="text"', 'autocomplete="none"', $field);
return $field;
}
}

Solution below using the attributes provided here
https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete
/* Disable autofill address */
add_filter( 'woocommerce_form_field', 'change_autofill', 1, 1 );
function change_autofill( $field) {
$agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($agent, 'Firefox') !== false) {
$field = str_replace('autocomplete="address-line1"', 'autocomplete="off"', $field);
return $field;
}
else {
$field = str_replace('autocomplete="address-line1"', 'autocomplete="none"', $field);
return $field;
}
}

Related

Wordpress save_post creates duplicate content in 1 entry

I am encountering issue whereby my post content got duplicated. I want to be able to modify the content only on first publish. I tried to use wp_insert_post_data filter but it keeps crashing. So i use save_post. Here is what I want to achieve.
Example:
This is my post and I [bbsmall]create it using[/bbsmall] save_post
Filter:
Remove tag and use only simple html tags and do many other modification.
Ideal Output:
This is my post and I create it using save_post
However, my current output is:
This is my post and I create it using save_post
This is my post and I create it using save_post
This is my code
add_action('save_post', 'my_modified_content');
function my_modified_content($post_id) {
$post_content = get_post( $post_id );
if( $post_content->post_type != "post" ) {
return;
}else{
if (isset($post_content->post_status) && 'auto-draft' == $post_content->post_status ) {
return;
}
else{
$add_extra_tag = $myweb_settings['myoption_set'];
$post_checked = get_post_meta( $post_id, 'my_post_meta_style', true );
$alreadydone = get_post_meta( $post_id, 'check_post_style', true );
if ($alreadydone!=1){
if (isset($add_extra_tag) and $add_extra_tag=="Yes"){
remove_action('save_post', 'my_modified_content');
$content = add_simple_tags($post_content->post_content);
$title =add_simple_tags($post_content->post_title);
update_post_meta($post_id, 'check_post_style', '1');
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = $title;
$my_post['post_content'] = $content;
$my_post['post_status'] = 'publish';
wp_update_post($my_post);
add_action('save_post', 'my_modified_content');
}else{
if (isset($post_checked ) ) {
remove_action('save_post', 'my_modified_content');
$content = add_simple_tags($post_content->post_content);
$title =add_simple_tags($post_content->post_title);
update_post_meta($post_id, 'check_post_style', '1');
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = $title;
$my_post['post_content'] = $content;
$my_post['post_status'] = 'publish';
wp_update_post($my_post);
add_action('save_post', 'my_modified_content');
}else{
return ;
}
}
}else{
return ;
}
}
}
}
Okay so I am able to fix the issue.
Instead of using save_post, I use wp_insert_post_data filter.
Previously I failed to use the filter due to the wrong format of coding and I did not return the $data.
So these are the modifications I did. Hope this is helpful for others who are facing the same issue.
add_filter('wp_insert_post_data', 'my_modified_content',10,3);
function my_modified_content($data, $postarr, $unsanitized_postarr) {
if( $data['post_type'] != "post" ) {
return;
}else{
if (isset($data['post_status']) && 'auto-draft' == $data['post_status'] ) {
return;
}
else{
$add_extra_tag = $myweb_settings['myoption_set'];
$post_checked = $postarr['my_post_meta_style'];
//Do other checks and modifications
}
}
return $data;
}

Remove Selected Checkout Fields with Woocommerce Depending on Shipping Method Redux

In reference to this Remove selected checkout fields with woocommerce depending on shipping method
How can I adapt the codes in order remove different fields based on different shipping methods selected?
I've tried modifying the beginnging part to below, but it just stops working...
add_filter( 'woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields' );
function xa_remove_billing_checkout_fields( $fields ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
// uncomment below line and reload checkout page to check current $chosen_methods
// print_r($chosen_methods);
$chosen_shipping = $chosen_methods[0];
if ($chosen_shipping == 'flat_rate:13') {
$shipping_method = 'flat_rate:13';
$hide_fields = array( 'billing_address_2', 'billing_city', 'billing_state', 'billing_postcode' );
}
if ($chosen_shipping =='flat_rate:7') {
$shipping_method = 'flat_rate:7';
$hide_fields = array( 'billing_city', 'billing_state', 'billing_postcode' );
}
foreach($hide_fields as $field ) {
if ($chosen_shipping == $shipping_method) {
$fields['billing'][$field]['required'] = false;
$fields['billing'][$field]['class'][] = 'hide';
}
$fields['billing'][$field]['class'][] = 'billing-dynamic';
}
return $fields;
}
The other part looks like this, which I copied and modified from another answer
add_action( 'wp_footer', 'cart_update_script', 999 );
function cart_update_script() {
if (is_checkout()) :
?>
<style>
.hide {display: none!important;}
</style>
<script>
jQuery( function( $ ) {
// woocommerce_params is required to continue, ensure the object exists
if ( typeof woocommerce_params === 'undefined' ) {
return false;
}
var show = ['flar_rate:7', 'flat_rate:13'];
$(document).on( 'change', '#shipping_method input[type="radio"]', function() {
// console.log($.inArray($(this).val(), show));
if ($.inArray($(this).val(), show) > -1) { // >-1 if found in array
$('.billing-dynamic').removeClass('hide');
// console.log('show');
} else {
$('.billing-dynamic').addClass('hide');
// console.log('hide');
}
});
});
</script>
<?php
endif;
}
Also, if I change the country, which changes also the shipping zone, the fields ain't reverted until I select the shipping method again.
Thanks in advance.

Rename comments labels in WP admin

Is it possible to change default comments in wordpress admin?
I`d like to rename comments into testimonials everywhere in admin.
If you want to change all WordPress uses of 'Comment', then you need to hook into the translation filter
is_admin() && add_filter('gettext', function ($translation, $text, $domain) {
if (strpos($translation, 'comment') !== FALSE) {
return str_replace('comment', 'testimonial', $translation);
}
if (strpos($translation, 'Comment') !== FALSE) {
return str_replace('Comment', 'Testimonial', $translation);
}
return $translation;
}, 10, 3);
But that changes EVERYTHING that runs through gettext.
If you just wanted to change the admin section title you would do this:
add_action('admin_head', function () {
global $wp_meta_boxes;
if (!empty($wp_meta_boxes)) {
foreach ($wp_meta_boxes as $page => &$positions) {
foreach ($positions as $context => &$priorities) {
foreach ($priorities as $priority => &$boxes) {
foreach ($boxes as $id => &$box) {
if ($id === 'commentsdiv') {
$box['title'] = 'Testimonials';
break;
}
}
}
}
}
}
});
Beyond that, you'd have to find every instance of 'Comment' and see if there was an associated hook for that situation. I'm not sure if that is feasible.

Uncaught Error: Call to a member function get_cart() on null

On Woocommerce, I have an error message on my function but i don't unserstand why it happens.
Uncaught Error: Call to a member function get_cart() on null in ...
The function checks id a category product is in the cart.
The error is displayed in the order detail in backend
It's used in functions.php , executed on checkout page,
called by these hooks:
woocommerce_before_order_notes
woocommerce_checkout_process
woocommerce_checkout_update_order_meta
woocommerce_admin_order_data_after_billing_address
woocommerce_email_order_meta_keys
https://i.stack.imgur.com/v7Atp.png
function is_in_the_cart() {
$found=false;
// Find if product is in the cart price <=40
foreach( WC()->cart->get_cart() as $cart_item_key => $values )
{
$cart_product = $values['data'];
$price = accessProtected($values['data'], 'changes')['price'];
if ($price >=40 && $cart_product->id=='969') {$found=true;}
}
if ( $found ) {
return true;
} else {
return false;
}
}
add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
if (is_in_the_cart())
{
global $woocommerce;
// Check if set, if its not set add an error.
if ($_POST['dvd'] == "blank")
wc_add_notice( '<strong>Merci de séléctionner un DVD</strong>', 'error' );
}
}
//* Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');
function wps_select_checkout_field_update_order_meta( $order_id ) {
if (is_in_the_cart())
{
if ($_POST['dvd']) update_post_meta( $order_id, 'dvd', esc_attr($_POST['dvd']));
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'wps_select_checkout_field_display_admin_order_meta', 10, 1 );
function wps_select_checkout_field_display_admin_order_meta($order){
if (is_in_the_cart())
{
echo '<p><strong>DVD: </strong> ' . get_post_meta( $order->id, 'dvd', true ) . '</p>';
}
}
//* Add selection field value to emails
add_filter('woocommerce_email_order_meta_keys', 'wps_select_order_meta_keys');
function wps_select_order_meta_keys( $keys ) {
if (is_in_the_cart())
{
$keys['Dvd:'] = 'dvd';
return $keys;
}
}
I think you want to check to make sure the cart isn't empty before your function.
Also... You can access price by using get_price() from the product object which you are retrieving with $values['data']
function is_in_the_cart(){
// Make sure it's only on front end
if (is_admin()) return false;
$found = false;
// If cart is empty - bail and return false
if (empty (WC()->cart->get_cart())) {
return false;
} else {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = $values[ 'data' ];
// Find if product is in the cart price <=40
$price = floatval($cart_product->get_price());
if ( $price >= 40 && $cart_product->id == '969' ) {
$found = true;
}
}
if ( $found ) {
return true;
} else {
return false;
}
}
}
UPDATED - added is_admin to prevent from running on backend.

How to check that we are not in a Woocommerce endpoint

since WooCommerce 2.1 pages such as order-received has been removed and replaced with WC endpoints. My checkout page had a custom page template (page-checkout.php) and now all checkout endpoints are also using this custom page template.
I need to modify my header and footer only when my customers are in the /checkout/ page, but I want to show different content when they are in a checkout endpoints. I have found this conditional:
if(is_wc_endpoint_url("order-received")) echo "yes";
It works when we are in the "order-received" checkout endpoint. But I am looking for a conditional logic that tells me when we are not in an endpoint, something like:
if(!is_wc_endpoint()) echo "yes";
Thank you.
The questions seemed to be answered and bit old. But i found a better solution, which may help somebody else.
you can use the following function. Documentation
is_wc_endpoint_url()
if used without a parameter, it will check current url against all endpoints. If an endpoint is specified like
is_wc_endpoint_url('edit-account');
it will check either the url is of specific endpoint or not.
Try following function:
function is_wc_endpoint() {
if ( empty( $_SERVER['REQUEST_URI'] ) ) return false;
$url = parse_url( $_SERVER['REQUEST_URI'] );
if ( empty( $url['query'] ) ) return false;
global $wpdb;
$all_woocommerce_endpoints = array();
$results = $wpdb->get_results( "SELECT option_name, option_value FROM {$wpdb->prefix}options WHERE option_name LIKE 'woocommerce_%_endpoint'", 'ARRAY_A' );
foreach ( $results as $result ) {
$all_woocommerce_endpoints[$result['option_name']] = $result['option_value'];
}
foreach ( $all_woocommerce_endpoints as $woocommerce_endpoint ) {
if ( strpos( $url['query'], $woocommerce_endpoint ) !== false ) {
return true;
}
}
return false;
}
Hope it'll give you result you are expecting.
This is a never version of the is_wc_endpoint_url function that will be included in the future woocommerce version. So just give it a different name and put into your functions.php for example.
function is_wc_endpoint_url( $endpoint = false ) {
global $wp;
$wc_endpoints = WC()->query->get_query_vars();
if ( $endpoint ) {
if ( ! isset( $wc_endpoints[ $endpoint ] ) ) {
return false;
} else {
$endpoint_var = $wc_endpoints[ $endpoint ];
}
return isset( $wp->query_vars[ $endpoint_var ] );
} else {
foreach ( $wc_endpoints as $key => $value ) {
if ( isset( $wp->query_vars[ $key ] ) ) {
return true;
}
}
return false;
}
}
I tried the default WC function:
is_wc_endpoint_url('my-custom-endpoint');
but it always returned false for me. So I created my own function:
function yourtheme_is_wc_endpoint($endpoint) {
// Use the default WC function if the $endpoint is not provided
if (empty($endpoint)) return is_wc_endpoint_url();
// Query vars check
global $wp;
if (empty($wp->query_vars)) return false;
$queryVars = $wp->query_vars;
if (
!empty($queryVars['pagename'])
// Check if we are on the Woocommerce my-account page
&& $queryVars['pagename'] == 'my-account'
) {
// Endpoint matched i.e. we are on the endpoint page
if (isset($queryVars[$endpoint])) return true;
// Dashboard my-account page special check - check whether the url ends with "my-account"
if ($endpoint == 'dashboard') {
$requestParts = explode('/', trim($wp->request, ' \/'));
if (end($requestParts) == 'my-account') return true;
}
}
return false;
}
Example:
yourtheme_is_wc_endpoint('my-custom-endpoint');
Or:
yourtheme_is_wc_endpoint('edit-account');

Resources