Một số code liên quan tới phần 5:
Tạo widget best selling 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 best_selling_product extends WP_Widget
{
function best_selling_product(){
$widget_ops = array('description' => 'Best selling products');
$control_ops = array('width' => 300, 'height' => 300);
parent::WP_Widget(false,$name='Best selling 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',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'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('best_selling_product');
?>
|
0 nhận xét: