Select2 auto closes on click in WooCommerce as variation custom field - woocommerce

I am trying to use Select2 4.0 in the product admin of WooCommerce 3.1.0.
I have added a custom field to variations called Color Groups, like so:
add_action( 'woocommerce_variation_options', 'add_to_variations_metabox', 10, 3 );
function add_to_variations_metabox( $loop, $variation_data, $variation ) {
<div>
<p class="form-row">
<label for="dipi_color_groups<?php echo $loop; ?>"><?php echo __( 'Color Groups', 'dipi' ); ?></label>
<select multiple="true" id="dipi_color_groups<?php echo $loop; ?>" class="color_groups" name="dipi_color_groups[<?php echo $loop; ?>]">
<option>Red</option>
<option>Green</option>
<option>Blue</option>
<option>Beige</option>
</select>
</p>
</div> <?php
}
Here is my JS that makes the select a Select2.
$( '#variable_product_options_inner' ).on( 'click', '.woocommerce_variation', function( e ) {
$( "select.color_groups", $( this ) ).select2({
tags: true,
placeholder: "Add color groups...",
width: "100%",
createTag: function ( params ) {
return {
id: params.term,
text: params.term,
newOption: true
}
}
});
});
The field displays just fine.
But when I click on it the Select2 automatically unfocuses / closes. I think it's a collision with WooCommerce's handling of Select2's, but I can't figure out how to duplicate their functionality within WC edit product.
What's the deal? Any help greatly appreciated!

I figured it out. The problem was with my click event attached to any single variations wrapping container. When clicking on the Select2 the .woocommerce_variation click event would fire and reset the Select2—automatically losing focus.
So I scoped the click event to the variation heading title, the <h3>, and only when closed. Also, I check to see if the Select2 has already been applied to avoid wasted resources or resetting the element.
$( '#variable_product_options_inner' ).on( 'click', '.woocommerce_variation.closed h3', function( e ) {
// get current ddl
$ddl = $( "select.color_groups", $( this ).parent() );
// already a select2?
if ( $ddl.hasClass("select2-hidden-accessible" ) ) return; // get out...
// select2-ify dropdown
$ddl.select2({
tags: true,
placeholder: "Add color groups...",
width: "100%",
createTag: function ( params ) {
return {
id: params.term,
text: params.term,
newOption: true
}
}
});
});

Related

twenty Seventeen mobile menu

I have WordPress website with twenty seventeen theme. Menu work well on desktop but when website load on mobile the menu it automatically brings the menu up after loading when scroll website menu close into single toggle. When I click a menu item, the page redirect on new page with same problem.
Pic attached for reference.
I'll be very grateful if some assist me who to solve this.
maybe this is the code:
// Set the active submenu initial state.
container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );
container.find( '.dropdown-toggle' ).on( 'click', function( e ) {
var _this = $( this ),
screenReaderSpan = _this.find( '.screen-reader-text' );
e.preventDefault();
_this.toggleClass( 'toggled-on' );
_this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );
_this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
screenReaderSpan.text( screenReaderSpan.text() === twentyseventeenScreenReaderText.expand ? twentyseventeenScreenReaderText.collapse : twentyseventeenScreenReaderText.expand );
});
}

Conditionally hide checkout field on chosen payment method at checkout page

I am using the following code for hiding credit card number field for chosen payment method. It's hiding the field perfectly but when the field is hidden and I click on Place Order button, it still shows 'Credit Card Number is required'.
// show hide credit card fields start
add_action( 'wp_footer', 'conditionally_show_hide_card_number_field' );
function conditionally_show_hide_card_number_field(){
// Only on checkout page
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script>
jQuery(function($){
var a = 'input[name="payment_method"]',
b = a + ':checked',
c = '#credit_card_number_field'; // The checkout field <p> container selector
// Function that shows or hide checkout fields
function showHide( selector = '', action = 'show' )
{
if( action == 'show' )
$(selector).show( 200, function(){
$(this).addClass("validate-required");
});
else
$(selector).hide( 200, function(){
$(this).removeClass("validate-required");
});
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Initialising: Hide if choosen payment method is "credit_card"
if( $(b).val() == 'elavon_converge_credit_card' )
showHide( c, 'hide' );
else
showHide( c );
// Live event (When payment method is changed): Show or Hide based on "credit_card"
$( 'form.checkout' ).on( 'change', a, function() {
if( $(b).val() == 'elavon_converge_credit_card' )
showHide( c, 'hide' );
else
showHide( c );
});
});
</script>
<?php
endif;
}
// show hide credit card fields ends
Please suggest the change in code how can I overcome this problem.
You cannot overcome this unless you make the CC number field optional which is a bad idea because then you're agreeing to accept CC payments without a CC number, too; and those payments will fail. This becomes a usability nightmare. WooCommerce, by default, has it's own way of handling this and no manual effort is needed by you (entire section is hidden and non-mandatory when choosing another payment method). What have you changed, or what are you trying to accomplish which has removed this built-in feature? I'm thinking that you might just be asking the wrong question.

Cut the text and add a read more button in woocommerce categories

I have very long category descriptions in my Woocommerce and I would like to truncate the text and add a Read More button.
I tried to upload a plugin to add an editor in the category section to use the read more button but I can use the button or put the tag directly nothing happens at the front.
It would probably be easier to modify the file directly but I block... can you help me?
You could use some jQuery to shrink the height of the description container and add a 'Read more' button that will resize the container back to its original height.
add_action( 'woocommerce_after_main_content', 'woocommerce_after_main_content', 10 );
function woocommerce_after_main_content() {
if ( !is_product_category() ) return;
?>
<script>
jQuery( function( $ ) {
$(document).ready(function() {
//Get current height of the description container
let curHeight = $("header .term-description").height();
//Set heigth of the description container
$("header .term-description").css({
"height": "100px",
"overflow": "hidden",
});
//Add 'Read more' button
$("header .term-description").after( "<button class='read-more' style='margin:15px 0;'>Read more</button>" );
//Set description container back to its original height
$( "header .read-more" ).on( "click", function() {
$("header .term-description").animate({height: curHeight});
});
});
});
</script>
<?php
}
This code snippet should be added to the functions.php of your child theme or via a plugin like Code Snippets.

woocommerce how show loading icon when click add to cart button

I want to show a spinner icon while the 'Add To Cart' button is processing.
I'm using this code:
jQuery('a.add_to_cart_button').click(function(){
jQuery(this).append('<img src="/spinner.gif" width="20px" height="20px"/>')});
It works well after user clicks the 'Add to Cart' button, but I want to remove this icon after successful execution.
It's no need to use your own loader or spinner icon because WooCommerce itself provides its own loader in front end. Here is the flow to use the code of loader.
As soon as add to cart button is clicked. Add this line in your click function, it adds loader in a particular section
block( $( 'div.summary.entry-summary' ) );
Now when the product is successfully added to cart and your ajax response is success add this line to remove the loader. It removes the loader from that section.
unblock( $( 'div.summary.entry-summary' ) );
This code blocks and unblocks a particular section, if you want you can increase its scope by specifying the element you want.
These are the functions responsible for applying and removing the loader.
var is_blocked = function( $node ) {
return $node.is( '.processing' ) || $node.parents( '.processing' ).length;
};
var block = function( $node ) {
if ( ! is_blocked( $node ) ) {
$node.addClass( 'processing' ).block( {
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
} );
}
};
var unblock = function( $node ) {
$node.removeClass( 'processing' ).unblock();
};

Wordpress custom role can't upload images using media on front end

I have created a custom template with plugin that allows user (Role: Customer) to upload pictures from front end but when I try to upload a picture it says "You don't have permission to attach files to this post."
I can upload pictures through /wp-admin > Media - Add New but not from the front end.
Here is my code;
`function CA_enqueue_scripts() {
wp_enqueue_media();
wp_enqueue_script(
'some-script', get_template_directory_uri() .
'/custom/assets/js/edit_profile.js', array('jquery'), '2017-09-27'
);
}
/**
* This filter insures users only see their own media
*/
function CA_filter_media($query) {
// admins get to see everything
if (!current_user_can('manage_options'))
$query['author'] = get_current_user_id();
return $query;
}
add_action('wp_enqueue_scripts', 'CA_enqueue_scripts');
add_filter('ajax_query_attachments_args', 'CA_filter_media');`
JS:
(function($) {
$(document).ready( function() {
var file_frame;
$( '#upload_image' ).on( 'click', function( event ) {
event.preventDefault();
if ( file_frame ) {
file_frame.open();
return;
}
file_frame = wp.media.frames.file_frame = wp.media({
title: $( this ).data( 'uploader_title' ),
button: {
text: $( this ).data( 'uploader_button_text' ),
},
multiple: false // set this to true for multiple file selection
});
file_frame.on( 'select', function() {
attachment = file_frame.state().get('selection').first().toJSON();
console.log(attachment);
});
file_frame.open();
});
});
})(jQuery);
I have set granted 'upload, edit post' permissions to role using using User Role Editor plugin.
Thanks!
I resolved this issues installing https://wordpress.org/plugins/user-role-editor/,
then I realized that my CPT, the one that I created to be modified by this Role(Customer) was with capabilities of page. I modified on the plugin option..
Hope that helps you.

Resources