Skip to content

Commit 7d0623b

Browse files
committed
update to 1.0.10
1 parent 5536583 commit 7d0623b

5 files changed

+84
-68
lines changed

includes/class-product-brand-meta.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public function load_media_files()
2222

2323
public function save_brand_image_and_weight($term_id)
2424
{
25-
if (isset($_POST['product_brand_image'])) {
26-
$image_id = $_POST['product_brand_image'];
25+
if (isset($_POST['product_brand_logo'])) {
26+
$image_id = $_POST['product_brand_logo'];
2727
if (!empty($image_id)) {
28-
update_term_meta($term_id, 'product_brand_image', $image_id);
28+
update_term_meta($term_id, 'product_brand_logo', $image_id);
2929
}
3030
}
3131
if (isset($_POST['product_brand_weight'])) {
@@ -35,10 +35,10 @@ public function save_brand_image_and_weight($term_id)
3535
}
3636
}
3737
// Upload image and set as term meta
38-
if (!empty($_FILES['product_brand_image_file']['name'])) {
39-
$uploaded_image = media_handle_upload('product_brand_image_file', $term_id);
38+
if (!empty($_FILES['product_brand_logo_file']['name'])) {
39+
$uploaded_image = media_handle_upload('product_brand_logo_file', $term_id);
4040
if (!is_wp_error($uploaded_image)) {
41-
update_term_meta($term_id, 'product_brand_image', $uploaded_image);
41+
update_term_meta($term_id, 'product_brand_logo', $uploaded_image);
4242
}
4343
}
4444
}
@@ -48,8 +48,8 @@ public function add_brand_fields()
4848
// Add the image and weight fields to the product_brand taxonomy form
4949
?>
5050
<div class="form-field term-group">
51-
<label for="product_brand_image"><?php esc_html_e('Brand Image', 'text-domain'); ?></label>
52-
<input type="hidden" id="product_brand_image" name="product_brand_image" value="">
51+
<label for="product_brand_logo"><?php esc_html_e('Brand Logo', 'text-domain'); ?></label>
52+
<input type="hidden" id="product_brand_logo" name="product_brand_logo" value="">
5353
<img id="brand-image-preview" src="" style="max-width: 100%; display: none;">
5454
<input type="button" id="brand-image-upload-button" class="button" value="<?php esc_attr_e('Upload Image', 'text-domain'); ?>">
5555
<script>
@@ -75,7 +75,7 @@ public function add_brand_fields()
7575
mediaUploader.on('select', function() {
7676
var attachment = mediaUploader.state().get('selection').first().toJSON();
7777
$('#brand-image-preview').attr('src', attachment.url);
78-
$('#product_brand_image').val(attachment.id);
78+
$('#product_brand_logo').val(attachment.id);
7979
$('#brand-image-preview').show();
8080
});
8181
// Open the uploader dialog
@@ -94,19 +94,19 @@ public function add_brand_fields()
9494
public function edit_brand_fields($term)
9595
{
9696
// Retrieve current values of fields
97-
$image_id = get_term_meta($term->term_id, 'product_brand_image', true);
97+
$image_id = get_term_meta($term->term_id, 'product_brand_logo', true);
9898
$weight = get_term_meta($term->term_id, 'product_brand_weight', true);
9999

100100
// Output the fields
101101
?>
102102
<tr class="form-field">
103-
<th scope="row" valign="top"><label for="product_brand_image"><?php _e('Brand Image'); ?></label></th>
103+
<th scope="row" valign="top"><label for="product_brand_logo"><?php _e('Brand Image'); ?></label></th>
104104
<td>
105105
<?php if (!empty($image_id)) { ?>
106106
<img src="<?php echo wp_get_attachment_url($image_id); ?>" style="max-width: 200px;" />
107107
<br /><br />
108108
<?php } ?>
109-
<input type="hidden" id="product_brand_image" name="product_brand_image" value="<?php echo esc_attr($image_id); ?>" />
109+
<input type="hidden" id="product_brand_logo" name="product_brand_logo" value="<?php echo esc_attr($image_id); ?>" />
110110
<button id="upload_image_button" class="button"><?php _e('Upload Image'); ?></button>
111111
<script>
112112
jQuery(document).ready(function($) {
@@ -126,7 +126,7 @@ public function edit_brand_fields($term)
126126
});
127127
custom_uploader.on('select', function() {
128128
var attachment = custom_uploader.state().get('selection').first().toJSON();
129-
$('#product_brand_image').val(attachment.id);
129+
$('#product_brand_logo').val(attachment.id);
130130
$('img').attr('src', attachment.url);
131131
});
132132
custom_uploader.open();

includes/class-product-brand-shortcode.php

+38-30
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
<?php
22

3-
class WooCommerce_Product_Brand_Shortcode extends WooCommerce_Product_Brand {
4-
5-
public function __construct() {
3+
class WooCommerce_Product_Brand_Shortcode extends WooCommerce_Product_Brand
4+
{
5+
6+
public function __construct()
7+
{
68
parent::__construct();
79
add_shortcode('product_brand_list', array($this, 'product_brand_list_shortcode'));
810
}
911

10-
public function product_brand_list_shortcode($atts) {
12+
public function product_brand_list_shortcode($atts)
13+
{
1114
$atts = shortcode_atts(array(
1215
'orderby' => 'product_brand_weight',
13-
'order' => 'asc'
16+
'order' => 'asc',
17+
'scroll' => 'true',
18+
'limit' => '12'
1419
), $atts, 'product_brand_list');
1520

1621
$brands = get_terms('product_brand', array(
22+
'number' => $atts['limit'],
1723
'orderby' => $atts['orderby'],
1824
'order' => $atts['order'],
1925
'hide_empty' => false,
2026
'meta_query' => array(
2127
array(
22-
'key' => 'product_brand_image',
28+
'key' => 'product_brand_logo',
2329
'compare' => 'EXISTS'
2430
)
2531
)
2632
));
2733

28-
$flickityOptions = array(
29-
'groupCells' => true,
30-
'contain' => true,
31-
'draggable' => true,
32-
);
33-
3434
$output = '';
3535

3636
if (!empty($brands)) {
@@ -40,7 +40,7 @@ public function product_brand_list_shortcode($atts) {
4040
$brand_id = $brand->term_id;
4141
$brand_name = $brand->name;
4242
$brand_slug = $brand->slug;
43-
$brand_image_id = get_term_meta($brand_id, 'product_brand_image', true);
43+
$brand_image_id = get_term_meta($brand_id, 'product_brand_logo', true);
4444
$brand_image_url = wp_get_attachment_url($brand_image_id);
4545

4646
$output .= '<div class="brand-item carousel-cell">';
@@ -52,18 +52,25 @@ public function product_brand_list_shortcode($atts) {
5252

5353
$output .= '</div>';
5454

55-
// Add the Flickity scripts and styles
56-
$output .= '<script src="' . plugin_dir_url(__DIR__) . 'public/js/flickity.pkgd.min.js"></script>';
57-
$output .= '<link rel="stylesheet" href="' . plugin_dir_url(__DIR__) . 'public/css/flickity.css" />';
58-
$output .= '<link rel="stylesheet" href="' . plugin_dir_url(__DIR__) . 'public/css/product-brands.css" />';
59-
$output .= '<script>jQuery(".brand-gallery").flickity(' . json_encode($flickityOptions) . ');</script>';
60-
61-
}
55+
if ($atts['scroll'] === 'true') {
56+
// Add the Flickity scripts and styles
57+
58+
$flickityOptions['groupCells'] = true;
59+
$flickityOptions['contain'] = true;
60+
$flickityOptions['draggable'] = true;
61+
62+
$output .= '<script src="' . plugin_dir_url(__DIR__) . 'public/js/flickity.pkgd.min.js"></script>';
63+
$output .= '<link rel="stylesheet" href="' . plugin_dir_url(__DIR__) . 'public/css/flickity.css" />';
64+
$output .= '<link rel="stylesheet" href="' . plugin_dir_url(__DIR__) . 'public/css/product-brands.css" />';
65+
$output .= '<script>jQuery(".brand-gallery").flickity(' . json_encode($flickityOptions) . ');</script>';
66+
}
67+
}
6268

6369
return $output;
6470
}
6571

66-
private function get_total_quantity_by_term($term_id) {
72+
private function get_total_quantity_by_term($term_id)
73+
{
6774
$args = array(
6875
'post_type' => 'product',
6976
'posts_per_page' => -1,
@@ -91,7 +98,8 @@ private function get_total_quantity_by_term($term_id) {
9198
return $total_quantity;
9299
}
93100

94-
private function get_total_retail_value_by_term($term_id) {
101+
private function get_total_retail_value_by_term($term_id)
102+
{
95103
$args = array(
96104
'post_type' => 'product',
97105
'posts_per_page' => -1,
@@ -107,16 +115,16 @@ private function get_total_retail_value_by_term($term_id) {
107115
$products = new WP_Query($args);
108116
$total_value = 0;
109117
if ($products->have_posts()) {
110-
while ($products->have_posts()) {
111-
$products->the_post();
112-
$product = wc_get_product(get_the_ID());
113-
$total_value += $product->get_price();
114-
}
115-
wp_reset_postdata();
118+
while ($products->have_posts()) {
119+
$products->the_post();
120+
$product = wc_get_product(get_the_ID());
121+
$total_value += $product->get_price();
116122
}
117-
118-
return $total_value;
123+
wp_reset_postdata();
119124
}
125+
126+
return $total_value;
127+
}
120128
}
121129

122130
// In this class, we first define the `__construct()` method, which calls the parent constructor and adds the `product_brand_list` shortcode with the `product_brand_list_shortcode()` method as its callback.

includes/class-product-brand.php

+31-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
3+
// Initialize the class and add Brand taxonomy into WooCommerce
44

55
class WooCommerce_Product_Brand
66
{
@@ -15,17 +15,17 @@ public function register_product_brand_taxonomy()
1515
$args = array(
1616
'hierarchical' => true,
1717
'labels' => array(
18-
'name' => __('Product Brands', 'woocommerce'),
19-
'singular_name' => __('Product Brand', 'woocommerce'),
20-
'search_items' => __('Search Product Brands', 'woocommerce'),
21-
'all_items' => __('All Product Brands', 'woocommerce'),
22-
'parent_item' => __('Parent Product Brand', 'woocommerce'),
23-
'parent_item_colon' => __('Parent Product Brand:', 'woocommerce'),
24-
'edit_item' => __('Edit Product Brand', 'woocommerce'),
25-
'update_item' => __('Update Product Brand', 'woocommerce'),
26-
'add_new_item' => __('Add New Product Brand', 'woocommerce'),
27-
'new_item_name' => __('New Product Brand Name', 'woocommerce'),
28-
'menu_name' => __('Product Brands', 'woocommerce'),
18+
'name' => __('Brands', 'woocommerce'),
19+
'singular_name' => __('Brand', 'woocommerce'),
20+
'search_items' => __('Search Brands', 'woocommerce'),
21+
'all_items' => __('All Brands', 'woocommerce'),
22+
'parent_item' => __('Parent Brand', 'woocommerce'),
23+
'parent_item_colon' => __('Parent Brand:', 'woocommerce'),
24+
'edit_item' => __('Edit Brand', 'woocommerce'),
25+
'update_item' => __('Update Brand', 'woocommerce'),
26+
'add_new_item' => __('Add New Brand', 'woocommerce'),
27+
'new_item_name' => __('New Brand Name', 'woocommerce'),
28+
'menu_name' => __('Brands', 'woocommerce'),
2929
),
3030
'public' => true,
3131
'hierarchical' => false,
@@ -48,7 +48,7 @@ public function register_product_brand_taxonomy()
4848
public function product_brand_columns($columns)
4949
{
5050
$columns['product_brand_weight'] = __('Weight', 'woocommerce');
51-
$columns['product_brand_image'] = __('Logo', 'woocommerce');
51+
$columns['product_brand_logo'] = __('Logo', 'woocommerce');
5252
return $columns;
5353
}
5454

@@ -59,18 +59,18 @@ public function product_brand_column_content($content, $column_name, $term_id)
5959
if (!empty($weight)) {
6060
$content = esc_html($weight);
6161
} else {
62-
$content = '-';
62+
$content = '';
6363
}
6464
}
6565

66-
if ($column_name === 'product_brand_image') {
67-
$image_id = get_term_meta($term_id, 'product_brand_image', true);
66+
if ($column_name === 'product_brand_logo') {
67+
$image_id = get_term_meta($term_id, 'product_brand_logo', true);
6868
$size = array('75','50');
6969
$image = wp_get_attachment_image($image_id, $size);
7070
if (!empty($image)) {
7171
$content = $image;
7272
} else {
73-
$content = '-';
73+
$content = '';
7474
}
7575
}
7676

@@ -80,18 +80,25 @@ public function product_brand_column_content($content, $column_name, $term_id)
8080
public function product_brand_sortable_columns($columns)
8181
{
8282
$columns['product_brand_weight'] = 'product_brand_weight';
83+
$columns['product_brand_logo'] = 'product_brand_logo';
8384
return $columns;
8485
}
8586

8687
public function product_brand_sort_request($vars)
8788
{
88-
if (isset($vars['orderby']) && $vars['orderby'] === 'product_brand_weight') {
89-
$vars = array_merge($vars, array(
90-
'meta_key' => 'product_brand_weight',
91-
'orderby' => 'meta_value_num'
92-
));
93-
}
94-
return $vars;
89+
if (isset($vars['orderby']) && 'product_brand_weight' == $vars['orderby']) {
90+
$vars = array_merge($vars, array(
91+
'meta_key' => 'product_brand_weight',
92+
'orderby' => 'meta_value',
93+
));
94+
} elseif (isset($vars['orderby']) && 'product_brand_logo' == $vars['orderby']) {
95+
$vars = array_merge($vars, array(
96+
'meta_key' => 'product_brand_logo',
97+
'orderby' => 'meta_value',
98+
));
99+
}
100+
101+
return $vars;
95102
}
96103

97104
public function term_link($url, $term)

product-brands.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: WooCommerce Product Brand
44
Plugin URI: https://frydigital.com/plugins/fd-product-brands/
55
Description: A WooCommerce plugin that adds a custom taxonomy 'product_brand' to products and allows the attachment of images to the product_brand. Each image links to a search result for that taxonomy.
6-
Version: 1.0.9
6+
Version: 1.0.10
77
Author: Fry Digital
88
Author URI: https://frydigital.com/
99
License: GPLv3

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ To use this shortcode, you can simply add `[product_brand_list]` to any post or
1717
* 1.0.7 - shortcode url link correction, link to slug instead of brand name.
1818
* 1.0.8 - Bug Fix - class WooCommerce_Product_Brand does not have a method "save_product_brand_meta_data"
1919
* 1.0.9 - Updated CSS rules to isolate styling to product brands only.
20+
* 1.0.10 - Added shortcode attributes for limit and scroll. Scroll=0 disables flickity functionality & scripts. Limit=12 sets the number of brands to display (default 12).
2021

2122

2223
# Future Development

0 commit comments

Comments
 (0)