Có nghĩa là khi khách hàng click vào nút add to cart thì ngay lập tức số lượng và tổng giá trị đơn hàng được hiển thị trên giỏ hàng mini mà không cần refresh trang
Code làm việt này:
1 2 3 4 5 6 7 8 9 10 |
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cartplus_fragment', '1'); function woocommerce_header_add_to_cartplus_fragment( $fragments ) { global $woocommerce; ob_start(); ?> <a href="<?php echo WC()->cart->get_cart_url(); ?>" title="Xem giỏ hàng" rel="nofollow" class ='my-cart-total'><?php echo sprintf(_n('%d item', '%d sản phẩm', WC()->cart->cart_contents_count, 'woothemes'), WC()->cart->cart_contents_count);?> - <?php echo WC()->cart->get_cart_total(); ?> </a> <?php $fragments['a.my-cart-total'] = ob_get_clean(); return $fragments; } |
Khi khách hàng click add to cart thì anh chị thấy rằng có xuất hiện một icon quay tròn, để chỉ trạng thái sản phẩm được đang được thêm vào, tuy nhiên khi ngoài trang chủ class woocommerce không được add vào thẻ body nên chúng ta sẽ không thấy điều đó, bây giờ chúng ta phải đi add nó vào
1 2 3 4 5 6 7 |
add_filter( 'body_class', 'tung_class_names' ); function tung_class_names( $classes ) { // add 'class-name' to the $classes array $classes[] = 'woocommerce'; // return the $classes array return $classes; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
<?php class lastest_products extends WP_Widget { function lastest_products(){ $widget_ops = array('description' => 'Lastest products'); $control_ops = array('width' => 300, 'height' => 300); parent::WP_Widget(false,$name='Lastest products',$widget_ops,$control_ops); } function form($instance){ global $wpdb; //Defaults $instance = wp_parse_args( (array) $instance, array('title'=>'','numbershow'=>'') ); $title = htmlspecialchars($instance['title']); $numbershow = htmlspecialchars($instance['numbershow']); ?> <p><label for="<?php echo $this->get_field_id('title'); ?>">Title </label> <input type="text" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" style="width:100%" value="<?php echo $title; ?>"> </p> <p><label for="<?php echo $this->get_field_id('numbershow'); ?>">Show number: </label> <input type="text" name="<?php echo $this->get_field_name('numbershow'); ?>" id="<?php echo $this->get_field_id('numbershow'); ?>" style="width:100%" value="<?php echo $numbershow; ?>"> </p> <?php } /*Saves the settings. */ function update($new_instance, $old_instance){ $instance = $old_instance; $instance['title'] = stripslashes($new_instance['title']); $instance['numbershow'] = stripslashes($new_instance['numbershow']); return $instance; } function widget($args, $instance) { global $wpdb,$post, $product; extract($args); $title= empty($instance['title']) ? '' : $instance['title']; $numbershow= empty($instance['numbershow']) ? '' : $instance['numbershow']; echo $before_widget; if ( ! empty( $instance['title'] ) ) { echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title']; } ?> <div class="lastest-product-list"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => $numbershow, 'post_status' => 'publish' ); $wp_query = new WP_Query( $args ); if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <div class="cutom-woocommerce-product-item"> <div class="cutom-woocommerce-product-item-featured-image"> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <?php echo get_the_post_thumbnail(get_the_ID(), 'cumtom-featured-image-for-shop' ); ?> </a> </div> <div class="cutom-woocommerce-product-item-title-and-price"> <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2> <p><?php woocommerce_get_template( 'loop/price.php' ); ?></p> <?php $product = wc_get_product( get_the_ID() ); $add_to_cart_link = sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" class=" add_to_cart_button single_add_to_cart_button button product_type_%s">%s</a>', esc_url( $product->add_to_cart_url() ), $product->id, esc_attr( $product->product_type ), 'Mua ngay' ); echo $add_to_cart_link; ?> </div> </div> <?php endwhile; wp_reset_postdata(); endif; ?> </div> <?php echo $after_widget; ?> <?php } }// end class register_widget('lastest_products'); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
<?php class products_featured extends WP_Widget { function products_featured(){ $widget_ops = array('description' => 'Products featured'); $control_ops = array('width' => 300, 'height' => 300); parent::WP_Widget(false,$name='Products featured',$widget_ops,$control_ops); } function form($instance){ global $wpdb; //Defaults $instance = wp_parse_args( (array) $instance, array('title'=>'','numbershow'=>'') ); $title = htmlspecialchars($instance['title']); $numbershow = htmlspecialchars($instance['numbershow']); ?> <p><label for="<?php echo $this->get_field_id('title'); ?>">Title </label> <input type="text" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" style="width:100%" value="<?php echo $title; ?>"> </p> <p><label for="<?php echo $this->get_field_id('numbershow'); ?>">Show number: </label> <input type="text" name="<?php echo $this->get_field_name('numbershow'); ?>" id="<?php echo $this->get_field_id('numbershow'); ?>" style="width:100%" value="<?php echo $numbershow; ?>"> </p> <?php } /*Saves the settings. */ function update($new_instance, $old_instance){ $instance = $old_instance; $instance['title'] = stripslashes($new_instance['title']); $instance['numbershow'] = stripslashes($new_instance['numbershow']); return $instance; } function widget($args, $instance) { global $wpdb,$post, $product; extract($args); $title= empty($instance['title']) ? '' : $instance['title']; $numbershow= empty($instance['numbershow']) ? '' : $instance['numbershow']; echo $before_widget; if ( ! empty( $instance['title'] ) ) { echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title']; } ?> <div class="lastest-product-list"> <?php $args = array( 'post_type' => 'product', 'meta_key' => '_featured', 'meta_value' => 'yes', 'meta_compare' => '=', 'posts_per_page' => $numbershow, 'post_status' => 'publish' ); $wp_query = new WP_Query( $args ); if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <div class="cutom-woocommerce-product-item"> <div class="cutom-woocommerce-product-item-featured-image"> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <?php echo get_the_post_thumbnail(get_the_ID(), 'cumtom-featured-image-for-shop' ); ?> </a> </div> <div class="cutom-woocommerce-product-item-title-and-price"> <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2> <p><?php woocommerce_get_template( 'loop/price.php' ); ?></p> <?php $product = wc_get_product( get_the_ID() ); $add_to_cart_link = sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" class=" add_to_cart_button single_add_to_cart_button button product_type_%s">%s</a>', esc_url( $product->add_to_cart_url() ), $product->id, esc_attr( $product->product_type ), 'Mua ngay' ); echo $add_to_cart_link; ?> </div> </div> <?php endwhile; wp_reset_postdata(); endif; ?> </div> <?php echo $after_widget; ?> <?php } }// end class register_widget('products_featured'); ?> |
Mọi sự sao chép xin ghi rõ nguồn là fcwordpress.net
Chuyên trang về wordpress: hướng dẫn thiết kế theme, plugin, thủ thuật wordpress