I want to reset an input field (clear the form) after the button Add To Cart is clicked in a WooCommerce single product page.
Any clue?
In your mytheme/woocommerce/single-product/add-to-cart/simple.php
Modify the "input_value" of woocommerce_quantity_input() by removing the use quantity from POST data.
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => $product->get_min_purchase_quantity(),
) );
Before:
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(),
After:
'input_value' => $product->get_min_purchase_quantity(),
Related
I am displaying my categories on a page. Now, when I have more than x posts, I want to show a pagination to navigate to the next page with the posts.
This is my code:
<?php
$cat_id = get_query_var('cat');
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array(
'cat' => $cat_id,
'posts_per_page' => 5,
'paged' => $paged
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
//Post data
echo get_the_post_thumbnail(get_the_ID());
?>
<li>With some posts</li>
<?php
endwhile;
?>
And then I try to spit out my navigation like this:
<?php
function pagination_nav() {
global $wp_query;
if ( $wp_query->max_num_pages > 1 ) { ?>
<nav class="pagination" role="navigation">
<div class="nav-previous"><?php next_posts_link( '← Older posts' ); ?></div>
<div class="nav-next"><?php previous_posts_link( 'Newer posts →' ); ?></div>
</nav>
<?php }
}
?>
<?php echo pagination_nav();
?>
But whatever I do, I can't return it. How exactly should I do this? $paged should work, right? I have more than 20 posts, but I can't see my navigation/paging button anywhere.
Lastly, I tried the code below, yet this does not go to the second page of my query. The URL says /category/categoryname/page/2, but it just sends me to a blank page...
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
Alright, so let me answer my own question. I solved it by using the same code, but changing the settings (!) in my Wordpress page. When enabling the pagination setting, try to change the default "paginate by 50 posts" to something like "10" or so. That triggers the code to work.
I have the following code which adds a custom meta box to wordpress pages but for some reason it wont save the selected option. Any ideas whats wrong ?
Thanks.
// Add Header Select Option MetaBox
function alfie_header_select_meta() {
add_meta_box( 'alfie_header_select_meta', __( 'Header Style', 'alfie' ), 'alfie_header_select_meta_callback', 'page', 'side' );
}
add_action( 'add_meta_boxes', 'alfie_header_select_meta' );
function alfie_header_select_meta_callback( $post ) {
$values = get_post_custom( $post->ID );
$selected = isset( $values['alfie_selected_header'] ) ? esc_attr( $values['alfie_selected_header'][0] ) : ”;
wp_nonce_field( 'alfie_header_select_meta_nonce', 'alfie_header_select_meta_nonce' );
?>
<p>
<select name="alfie_selected_header" id="alfie_selected_header">
<option value="alfie-header-default" <?php selected( $selected, 'default' ); ?>>Default</option>
<option value="alfie-header-style-minimal" <?php selected( $selected, 'minimal' ); ?>>Minimal</option>
<option value="alfie-header-test" <?php selected( $selected, 'test' ); ?>>Just a test option</option>
</select>
</p>
<?php
}
function alfie_meta_save( $post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'alfie_header_select_meta_nonce' ] ) && wp_verify_nonce( $_POST[ 'alfie_header_select_meta_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
if( isset( $_POST[ 'alfie_selected_header' ] ) ) {
update_post_meta( $post_id, 'alfie_selected_header', sanitize_text_field( $_POST[ 'alfie_selected_header' ] ) );
}
}
add_action( 'save_post', 'alfie_meta_save' );
Here is the working debugged code.
The post meta was being saved. But it wasnt displaying because of a difference in the string values and how you got the meta. Just use get_post_meta for a single field.
// Add Header Select Option MetaBox
function alfie_header_select_meta() {
add_meta_box( 'alfie_header_select_meta', __( 'Header Style', 'alfie' ), 'alfie_header_select_meta_callback', 'page', 'side' );
}
add_action( 'add_meta_boxes', 'alfie_header_select_meta' );
function alfie_header_select_meta_callback( $post ) {
$selected = get_post_meta( $post->ID,'alfie_selected_header',true);
wp_nonce_field( 'alfie_header_select_meta_nonce', 'alfie_header_select_meta_nonce' );
?>
<p>
<select name="alfie_selected_header" id="alfie_selected_header">
<option value="alfie-header-default" <?php selected( $selected, 'alfie-header-default' ); ?>>Default</option>
<option value="alfie-header-style-minimal" <?php selected( $selected, 'alfie-header-style-minimal' ); ?>>Minimal</option>
<option value="alfie-header-test" <?php selected( $selected, 'alfie-header-test' ); ?>>Just a test option</option>
</select>
</p>
<?php
}
function alfie_meta_save( $post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'alfie_header_select_meta_nonce' ] ) && wp_verify_nonce( $_POST[ 'alfie_header_select_meta_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
if( isset( $_POST[ 'alfie_selected_header' ] ) ) {
update_post_meta( $post_id, 'alfie_selected_header', sanitize_text_field( $_POST[ 'alfie_selected_header' ] ) );
}
}
add_action( 'save_post', 'alfie_meta_save' );
I'm having an issue with WP_Customize_Cropped_Image_Control in the Customizer. The default image that I have set doesn't show up in the side pane of Customizer where you upload and when I upload a new image, it just outputs a number, presumingly the image ID. The moment I change WP_Customize_Cropped_Image_Control to WP_Customize_Image_Control, everything works. The default image and uploaded images both display in the customizer and preview window.
Is there a different way of setting/displaying the default image for a default cropped image in the customizer?
This is the code I have in my customizer.php:
$wp_customize->add_setting( 'bio_image', array(
'default' => get_template_directory_uri() . '/images/default.jpg',
'transport' => 'postMessage'
) );
$wp_customize->add_control( new WP_Customize_Cropped_Image_Control( $wp_customize, 'bio_image', array(
'label' => __( 'Image', 'myTheme' ),
'flex_width' => false,
'flex_height' => false,
'width' => 330,
'height' => 330,
'settings' => 'bio_image'
) ) );
This is the code I have in my customizer.js:
wp.customize( 'bio_image', function( value ) {
value.bind( function( newval ) {
$('#bio-image').attr( 'src', newval );
} );
} );
This is the code I have in my template file:
<img id="bio-image" src="<?php echo get_theme_mod( 'bio_image' , get_template_directory_uri().'/images/default.jpg' ); ?>">
Currently, when using new WP_Customize_Cropped_Image_Control
$wp_customize -> add_setting ( 'Your_option_name', array( 'default' => 'this_has_to_be_a_number_that_matches_an_ID_for_to_work', 'sanitize_callback' => 'absint', ) );
I just haven't figured out how to set the images as ID's yet... instead of URLs
I have been trying to get infinite scroll to work but can not seem to get it to work.
I have three files the post loop content-post-ft.php, the functions.php and then my front page where the posts are loading frontpage.php
Functions.php
add_theme_support( 'infinite-scroll', array(
'container' => 'main-content',
'type' => 'scroll',
'footer' => 'foot',
'render' => 'infinite_scroll_render'
));
function infinite_scroll_render() {
get_template_part( 'content-post-ft', 'standard' );
}
content-post-ft.php
<div class="hub-cont">
<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
?>
<div id="newsitem">
<div id="newsimg" style="background-image: url('<?php echo $thumb_url ?>')">
</div>
<div id="newsname"> <?php the_title(); ?> </div>
<div id="newstext"><?php $text = $post->post_content; $trimmed = wp_trim_words( $text, 40, null ); echo $trimmed; ?></div>
<div class="clear"></div>
</div>
<?php
// endwhile;
// endif;
wp_reset_postdata();?>
</div>
Frontpage.php
<div id="main-content">
<?php
$myargs = array (
//'showposts' => 2,
'post_type' => 'post',
'category_name' => 'News'
);
$myquery = new WP_Query($myargs);
if($myquery->have_posts() ) :
while($myquery->have_posts() ) : $myquery->the_post();
get_template_part( 'content-post-ft', get_post_format() );
endwhile;
endif;
?>
</div>
I have tried looking at so many tutorials and other people having similar problems but nothing seems to be working.
The page is showing 10 posts because of the loop but its just adds the 10 and does not "scroll"
Getting this fixed would really help my stress levels!
Solved it. I had to make Jetpack allow the infinite scroll to be used on Custom fields and pages. By default it only works on the original posts page.
Functions file:
function tweakjp_custom_is_support() {
return true;
$supported = current_theme_supports( 'infinite-scroll' ) && ( is_home() || is_archive() || is_front_page() );
return $supported;
}
add_filter( 'infinite_scroll_archive_supported', 'tweakjp_custom_is_support' );
function mm_infinite_scroll_render() {
while ( have_posts() ) : the_post();
get_template_part( 'content' );
endwhile;
}
function mm_infinite_scroll_query_args($args) {
$new_args = array(
'posts_per_page' => $args['posts_per_page'],
'paged' => $args['paged'],
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
);
return $new_args;
}
function mm_infinite_scroll_posts_where($clause) {
return "";
}
add_filter( 'infinite_scroll_posts_where', 'mm_infinite_scroll_posts_where' );
add_filter( 'infinite_scroll_query_args', 'mm_infinite_scroll_query_args', 9999 );
add_theme_support( 'infinite-scroll', array(
'container' => 'main-content',
'render' => 'mm_infinite_scroll_render'
) );
Then the content.php file has the rendering code. Mine looks like this to help those who are unsure.
<div class="news-list">
<ul class="promo-list-items">
<?php
// Fetch all posts relating to a certain tag then display 4 of them
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 720,405 ), false, '' );
?>
<div id="promolink"></div><li class="news-list-item" style="background-image: url(' <?php echo $src[0];?> '); background-repeat: no-repeat; background-size: cover;"> <a class="gdbnewslink" href="<?php echo get_permalink();?>" >
<?php the_title();?>
</a>
<?php wp_reset_postdata(); ?>
</li>
<div class="clear"></div>
</ul>
</div>
Hope this helps someone with the same problem as me.
I am trying display different wordpress menus depending on the device/screen width.
<script>
$(function() {
if ($(window).width() > 959) {
<?php wp_nav_menu( array( 'menu' => 'primary', 'container' => '', 'menu_id' => 'menu' ) ); ?>
} else {
<?php wp_nav_menu( array( 'theme_location' => 'mobile' ) ); ?>
}
});
</script>
I've tried using this code, as well as several other variations, but nothing seems to be working. Any ideas?
Thanks in advance
Willem
As far as I know, wp_nav_menu just output the menu html, so on the client side, you would get this:
<script>
$(function() {
if ($(window).width() > 959) {
<div>....menu html....</div>
} else {
<div>....menu html....</div>
}
});
</script>
I think there should be javascript errors there.
Try this:
<div id="menu_a" style="display:none">
<?php wp_nav_menu( array( 'menu' => 'primary', 'container' => '', 'menu_id' => 'menu' ) ); ?>
</div>
<div id="menu_b" style="display:none">
<?php wp_nav_menu( array( 'theme_location' => 'mobile' ) ); ?>
</div>
<script>
$(function() {
if ($(window).width() > 959) {
$("#menu_a").show();
} else {
$("#menu_b").show();
}
});
</script>
Or in response design style, no Javascript involved:
#media all and (max-width: 958px) { #menu_a{display:none};}
#media all and (min-width: 959px) { #menu_b{display:none};}
I ended up using this:
<?php
/* USER-AGENTS
================================================== */
function check_user_agent ( $type = NULL ) {
$user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
if ( $type == 'bot' ) {
// matches popular bots
if ( preg_match ( "/googlebot|adsbot|yahooseeker|yahoobot|msnbot|watchmouse|pingdom\.com|feedfetcher-google/", $user_agent ) ) {
return true;
// watchmouse|pingdom\.com are "uptime services"
}
} else if ( $type == 'browser' ) {
// matches core browser types
if ( preg_match ( "/mozilla\/|opera\//", $user_agent ) ) {
return true;
}
} else if ( $type == 'mobile' ) {
// matches popular mobile devices that have small screens and/or touch inputs
// mobile devices have regional trends; some of these will have varying popularity in Europe, Asia, and America
// detailed demographics are unknown, and South America, the Pacific Islands, and Africa trends might not be represented, here
if ( preg_match ( "/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {
// these are the most common
return true;
} else if ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent ) ) {
// these are less common, and might not be worth checking
return true;
}
}
return false;
}
?>
and then:
<?php
$ismobile = check_user_agent('mobile');
if($ismobile) {
wp_nav_menu( array( 'theme_location' => 'mobile' ) );
} else {
wp_nav_menu( array( 'menu' => 'primary', 'container' => '', 'menu_id' => 'menu' ) );
}
?>
This doesn't work on the device size but rather whether or not the site is being accessed from a mobile device.
Source: Detect mobile browser
Thanks
Willem