How to display Country Phone Code in wordpress? - wordpress

I am not able to edit woocommerce checkout page.
I want to add my country phone number code like ( +88 ) but I am not able to do it.
Update:
I tried this code from this referece and added below code to my main theme ( woodmart) function.php.
wp_enqueue_style( 'int-tel-phone-style', 'https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/6.4.1/css/intlTelInput.css' );
wp_enqueue_script('int-tel-phone-js','https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/6.4.1/js/intlTelInput.min.js');
Finaly added this below code script in footer.php
<script type="text/javascript">
$("#billing_phone").intlTelInput();
</script>
But it still not working.
Any help please?

Just add follows codes snippets in your active theme's functions.php to do the job -
add_action( 'wp_footer', 'callback_wp_footer' );
function callback_wp_footer(){
?>
<script type="text/javascript">
( function( $ ) {
$( document.body ).on( 'updated_checkout', function(data) {
var ajax_url = "<?php echo admin_url('admin-ajax.php'); ?>",
country_code = $('#billing_country').val();
var ajax_data = {
action: 'append_country_prefix_in_billing_phone',
country_code: $('#billing_country').val()
};
$.post( ajax_url, ajax_data, function( response ) {
$('#billing_phone').val(response);
});
} );
} )( jQuery );
</script>
<?php
}
add_action( 'wp_ajax_nopriv_append_country_prefix_in_billing_phone', 'country_prefix_in_billing_phone' );
add_action( 'wp_ajax_append_country_prefix_in_billing_phone', 'country_prefix_in_billing_phone' );
function country_prefix_in_billing_phone() {
$calling_code = '';
$country_code = isset( $_POST['country_code'] ) ? $_POST['country_code'] : '';
if( $country_code ){
$calling_code = WC()->countries->get_country_calling_code( $country_code );
$calling_code = is_array( $calling_code ) ? $calling_code[0] : $calling_code;
}
echo $calling_code;
die();
}

Related

Validating custom Woo checkout field via regex

I'm trying to validate a custom checkout field against a regex expression so that it is a very specifically formatted date.
Here's my code:
add_action('woocommerce_after_checkout_validation', array($this, 'bkf_dd_checkout_validation'), 10, 2 );
add_action('wp_footer', array($this, 'bkf_dd_checkout_validation_js'));
function bkf_dd_checkout_validation( $fields, $errors ){
if( ! preg_match( '/[a-zA-Z]{6,9}\,\ \d{1,2}\ [a-zA-Z]{3,9}\ \d{4}/', $fields['delivery_date'] ) ){
$errors->add( 'validation', 'Please select a <strong>Delivery Date</strong> via the datepicker.' );
}
}
function bkf_dd_checkout_validation_js(){
if( ! is_checkout() ) {
return;
}
?>
<script>
jQuery(function($){
$( 'body' ).on( 'blur change', '#delivery_date', function(){
const wrapper = $(this).closest( '.form-row' );
if( ! /[a-zA-Z]{6,9}\,\ \d{1,2}\ [a-zA-Z]{3,9}\ \d{4}/.test( $(this).val() ) ) {
wrapper.addClass( 'woocommerce-invalid' );
wrapper.removeClass( 'woocommerce-validated' );
} else {
wrapper.addClass( 'woocommerce-validated' );
}
});
});
</script>
<?php
}
The JS is working, but the error is being set regardless of the value, ie. this displays even if field is valid (and set as green by the JS)
(the date format: Friday, 30 December 2022)
Any ideas?
Solution as provided by Vijay Hardaha
Replace $fields['delivery_date'] with $_POST['delivery_date']

To delete a class name 'page' from the body

I have created a Page Templates in WordPress. The body comes in the class as the following image. I want to remove 'page' class.
You can add or remove class from body tag with the help of filter. try below script.
function remove_body_classes( $wp_classes ) {
$blacklist = array( 'page' );
$wp_classes = array_diff( $wp_classes, $blacklist );
return $wp_classes;
}
add_filter( 'body_class', 'remove_body_classes', 10, 2 );
You should add this script at the end of the Page Template that you have created .
<script type="text/javascript">
$=jQuery;
$(document).ready(function(){
$( "body" ).removeClass( "page" )
});
</script>
Filter body_class using array_search as follow works for me:
add_filter( 'body_class', function( $classes ) {
unset($classes[array_search('page', $classes)]);
return $classes;
});
add_filter( 'body_class', function( $classes ) {
if ( isset( $classes['page'] ) ) {
unset( $classes['page'] );
}
return $classes;
});
Sorry, I wanted to do the particular for the one page.I have manually adopted the solution which is as follows.The directory path in my condition was wrong
function remove_body_classes( $wp_classes ) {
if ( is_page_template( 'inc/home-template-page.php' ) ) {
$page = array( 'page' );
$wp_classes = array_diff( $wp_classes, $page );
}
return $wp_classes;
}
add_filter( 'body_class', 'remove_body_classes', 10, 2 );

Multiple select product using select2 for wordpress plugin option page

I am trying to implement multiple select product using select2 . the list come up using getproductsearch this ajax action i did earlier but i failed to save it. I did this feature before for post-meta and product-category but failed for plugin option page. I am not sure what i am doing wrong.
please help.
class FeatureSale {
private $feature_sale_options;
public function __construct() {
add_action( 'admin_menu', array( $this, 'feature_sale_add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'feature_sale_page_init' ) );
}
public function feature_sale_add_plugin_page() {
add_submenu_page(
'exclutips-settings',
'Feature & Sale', // page_title
'Feature & Sale', // menu_title
'manage_options', // capability
'feature-sale', // menu_slug
array( $this, 'feature_sale_create_admin_page' ) // function
);
}
public function feature_sale_create_admin_page() {
$this->feature_sale_options = get_option( 'feature_sale_option_name' ); ?>
<div class="wrap">
<div class="catbox-area-admin" style="width: 500px;background: #fff;padding: 27px 50px;">
<h2>Feature & Sale</h2>
<p></p>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'feature_sale_option_group' );
do_settings_sections( 'feature-sale-admin' );
submit_button();
?>
</form>
</div>
</div>
<?php }
public function feature_sale_page_init() {
register_setting(
'feature_sale_option_group', // option_group
'feature_sale_option_name', // option_name
array( $this, 'feature_sale_sanitize' ) // sanitize_callback
);
add_settings_section(
'feature_sale_setting_section', // id
'', // title
array( $this, 'feature_sale_section_info' ), // callback
'feature-sale-admin' // page
);
add_settings_field(
'vpm_sale_product', // id
'VPM Sale Product', // title
array( $this, 'vpm_sale_product_callback' ), // callback
'feature-sale-admin', // page
'feature_sale_setting_section' // section
);
add_settings_field(
'vpm_featured_product', // id
'VPM Featured Product', // title
array( $this, 'vpm_featured_product_callback' ), // callback
'feature-sale-admin', // page
'feature_sale_setting_section' // section
);
}
public function feature_sale_sanitize($input) {
$sanitary_values = array();
if ( isset( $input['vpm_sale_product'] ) ) {
$sanitary_values['vpm_sale_product'] = $input['vpm_sale_product'];
}
if ( isset( $input['vpm_featured_product'] ) ) {
$sanitary_values['vpm_featured_product'] = $input['vpm_featured_product'];
}
return $sanitary_values;
}
public function feature_sale_section_info() {
}
//Output the HTML for the metabox.
public function vpm_sale_product_callback() {
global $post;
// Nonce field to validate form request came from current site
wp_nonce_field( basename( __FILE__ ), 'vpm_sale_product_nonce' );
$html = '';
// always array because we have added [] to our <select> name attribute
$feature_sale_options = get_option( 'feature_sale_option_name' ); // Array of All Options
$vpm_sale_product = $feature_sale_options['vpm_sale_product'];
$html .= '<p><select id="vpm_sale_product" name="vpm_sale_product[]" multiple="multiple" style="width:99%;max-width:25em;">';
if( $vpm_sale_product ) {
foreach( $vpm_sale_product as $post_id ) {
$title = get_the_title( $post_id );
// if the post title is too long, truncate it and add "..." at the end
$title = ( mb_strlen( $title ) > 50 ) ? mb_substr( $title, 0, 49 ) . '...' : $title;
$html .= '<option value="' . $post_id . '" selected="selected">' . $title . '</option>';
}
}
$html .= '</select></p>';
echo $html;
//==========================================
}
//* Output the HTML for the metabox.
public function vpm_featured_product_callback() {
global $post;
// Nonce field to validate form request came from current site
wp_nonce_field( basename( __FILE__ ), 'vpm_featured_product_nonce' );
$html = '';
// always array because we have added [] to our <select> name attribute
$feature_sale_options = get_option( 'feature_sale_option_name' ); // Array of All Options
$vpm_featured_product = $feature_sale_options['vpm_featured_product'];
$html .= '<p><select id="vpm_featured_product" name="vpm_featured_product[]" multiple="multiple" style="width:99%;max-width:25em;">';
if( $vpm_featured_product ) {
foreach( $vpm_featured_product as $post_id ) {
$title = get_the_title( $post_id );
// if the post title is too long, truncate it and add "..." at the end
$title = ( mb_strlen( $title ) > 50 ) ? mb_substr( $title, 0, 49 ) . '...' : $title;
$html .= '<option value="' . $post_id . '" selected="selected">' . $title . '</option>';
}
}
$html .= '</select></p>';
echo $html;
echo $vpm_featured_product;
//==========================================
?>
<script>
(function ($) {
'use strict';
$(function () {
//--------------------------------------------------------------------------
// multiple select with AJAX search
$('#vpm_featured_product,#vpm_sale_product').select2({
ajax: {
url: ajaxurl, // AJAX URL is predefined in WordPress admin
dataType: 'json',
delay: 250, // delay in ms while typing when to perform a AJAX search
data: function (params) {
return {
q: params.term, // search query
action: 'getproductsearch' // AJAX action for admin-ajax.php
};
},
processResults: function( data ) {
var options = [];
if ( data ) {
// data is the array of arrays, and each of them contains ID and the Label of the option
$.each( data, function( index, text ) { // do not forget that "index" is just auto incremented value
options.push( { id: text[0], text: text[1] } );
});
}
return {
results: options
};
},
cache: true
},
minimumInputLength: 3 // the minimum of symbols to input before perform a search
});
//----------------------------------------------------------------------------------------
});
})(jQuery);
</script>
<?php
}
}
if ( is_admin() )
$feature_sale = new FeatureSale();
The problem is that these select tags are not using the correct name value:
<select id="vpm_sale_product" name="vpm_sale_product[]"...>
<select id="vpm_featured_product" name="vpm_featured_product[]"...>
And the proper name values are: (based on your register_setting() call)
<select id="vpm_sale_product" name="feature_sale_option_name[vpm_sale_product][]"...>
<select id="vpm_featured_product" name="feature_sale_option_name[vpm_featured_product][]"...>
And although after correcting the above issue, the form data would be saved properly, the fifth parameter (i.e. the menu/page slug) passed to add_submenu_page() should match the fourth parameter passed to add_settings_section() and add_settings_field(), and also the one passed to do_settings_sections(). In your add_submenu_page() call, the menu/page slug is feature-sale, but with the other three functions, you set it to feature-sale-admin.
And using wp_nonce_field() is recommended, but in your case, it's not necessary since you're submitting to wp-admin/options.php. Unless of course, you want to check that prior to WordPress saving the options. But even so, I believe one nonce field is good. :)

Getting an error when trying to remove a cart item from the WooCommerce checkout

On the checkout page I've a button to selected the shipping method. When my cart contains a product which in in a category, I want remove all products from the cart which are in the same category.
<button onclick="clear_product_cart()">Check possibility</button>
My script :
function clear_product_cart(){
jQuery.post(
ajaxurl,
{
'action': 'clear_cart'
},
function(response){
alert( ' product removed!');
});
}
In my function.php
add_action( 'wp_ajax_clear_cart', 'clear_cart' );
add_action( 'wp_ajax_nopriv_clear_cart', 'clear_cart' );
function clear_cart(){
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( !has_term( 'my_cat', 'product_cat', $cart_item['product_id'] ) ) {
WC()->cart->remove_cart_item( $cart_item_key );
}
}
die();
}
I don't no why, but i have an error 500 after the first product was removed.
Have you a suggestion? thank's
Your JavaScript function could be the problem. Please remove it and put this into your functions.php file of your child theme:
add_action( 'wp_head', 'head_data' );
function head_data() { ?>
<script>
function clear_product_cart() {
let a = {action: "clear_cart"};
jQuery.post("<?php echo admin_url( 'admin-ajax.php' ); ?>", a, function () {
}).success(function () {
alert('Product removed!');
}).fail(function () {
alert('Error!');
});
}
</script>
<?php }
Try it again and tell me if it works. I can't test it at the moment.

Wordpress Facebook Instant Articles not working

I'm trying to install the wordpress plugin "Instant Articles for WP" (https://es.wordpress.org/plugins/fb-instant-articles/), I finish the process but when I get to the point to send 5 articles to review for FB the plugin show me to check all(5) the posts to solve the warnings on it, I tried to edit the post but the box that contain the Instant Articles is loading and does not solve anything, in the Chrome console I get "Uncaught ReferenceError: instant_articles_load_meta_box is not defined". I tried to move the jquery declaration to the top but the error persists. Any ideas? ):
instant-articles-meta-box.js
function instant_articles_force_submit ( post_ID ) {
var data = {
'action': 'instant_articles_force_submit',
'post_ID': post_ID,
'force': jQuery( '#instant_articles_force_submit' ).is( ':checked' ),
'security': jQuery( '#instant_articles_force_submit' ).attr( 'data-security' )
};
jQuery.post( ajaxurl, data, function(response) {
instant_articles_load_meta_box( post_ID );
});
}
function instant_articles_load_meta_box ( post_ID ) {
jQuery( document ).ready( function( $ ) {
var data = {
'action': 'instant_articles_meta_box',
'post_ID': post_ID
};
jQuery.post( ajaxurl, data, function(response) {
jQuery( '#instant_article_meta_box .inside' ).html( response );
jQuery( '#instant_articles_force_submit').click( function () {
instant_articles_force_submit( post_ID );
} );
}, 'html' );
jQuery( '#instant_article_meta_box' ).delegate( '.instant-articles-toggle-debug', 'click', function () {
jQuery( '#instant_article_meta_box' ).toggleClass( 'instant-articles-show-debug' );
return false;
} );
});
}
meta-box-loader-template.php
<span class="instant_articles_spinner" ></span>
<script>
instant_articles_load_meta_box( <?php echo absint( $post->ID ); ?> );
</script>
WP version 4.7.3
PHP version 5.4.17
Plugin version 3.3.3
SOLVED: Just replacing this on meta-box-loader-template.php:
<script>
instant_articles_load_meta_box( <?php echo absint( $post->ID ); ?> );
</script>
With this:
<script>
jQuery.noConflict();
jQuery( document ).ready(function( $ ) {
instant_articles_load_meta_box( <?php echo absint( $post->ID ); ?> );
});
</script>

Resources