Skip to content

Instantly share code, notes, and snippets.

@cheh
Created January 5, 2019 11:36
Show Gist options
  • Save cheh/0391770c27101e9190129067a58f8137 to your computer and use it in GitHub Desktop.
Save cheh/0391770c27101e9190129067a58f8137 to your computer and use it in GitHub Desktop.
WooCommerce simple widget based on ACF
<?php
defined( 'ABSPATH' ) || exit;
class BikeRide_Simple_Widget extends WC_Widget {
private $meta_key = '';
private $meta_prefix = '_bike_';
/**
* Constructor.
*/
public function __construct() {
$this->widget_cssclass = 'woocommerce widget_simple';
// $this->widget_description = __( 'Simple widget.', 'bikeride' );
$this->widget_id = 'woocommerce_simple';
$this->widget_name = esc_html__( 'Bikeride: Range Filter for number attributes', 'bikeride' );
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 10, 1 );
add_filter( 'acf/load_field/name=metadata', array( $this, 'load_choices' ) );
parent::__construct();
}
public function widget( $args, $instance ) {
global $wp;
if ( ! is_shop() && ! is_product_taxonomy() ) {
return;
}
if ( ! wc()->query->get_main_query()->post_count ) {
return;
}
if ( empty( $this->meta_key ) ) {
return;
}
$ranges = get_field( 'range', 'widget_' . $args['widget_id'] );
if ( empty( $ranges ) ) {
return;
}
$this->widget_start( $args, $instance );
$title = get_field( 'title', 'widget_' . $args['widget_id'] );
if ( $heading = apply_filters( 'widget_title', empty( $title ) ? '' : $title, $instance, $this->id_base ) ) {
echo $args['before_title'] . $heading . $args['after_title'];
}
$query_arg_key = $this->get_query_arg_key();
$html = '';
// $queried_object_id = get_queried_object_id();
// $current_page_url = get_permalink( $queried_object_id );
$current_page_url = home_url( add_query_arg( array(), $wp->request ) );
foreach ( $ranges as $range ) {
$_from = ( '' === $range['from'] ) ? 'below' : absint( $range['from'] );
$_to = ( '' === $range['to'] ) ? 'above' : absint( $range['to'] );
$url = add_query_arg( array( $query_arg_key => sprintf( '%1$s-%2$s', $_from, $_to ) ), $current_page_url );
$delimiter = '&nbsp;';
if ( is_int( $_from ) && is_int( $_to ) ) {
$delimiter = '&nbsp;&ndash;&nbsp;';
}
$format = '<li><a href="%3$s"><span>%1$s</span>%4$s<span>%2$s</span></a></li>';
if ( ! is_int( $_to ) ) {
$format = '<li><a href="%3$s"><span>%2$s</span>%4$s<span>%1$s</span></a></li>';
}
$html .= sprintf( $format, $_from, $_to, esc_url( $url ), $delimiter );
}
if ( ! empty( $html ) ) {
$html = sprintf( '<ul>%s</ul>', $html );
echo $html;
}
$this->widget_end( $args );
}
/**
* Undocumented function
*
* @param [type] $query
* @link https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/
* @return void
*/
public function pre_get_posts( $query ) {
if ( is_admin() ) {
return;
}
if ( ! $query->is_main_query() ) {
return;
}
// Get meta query
$this->meta_key = get_field( 'metadata', 'widget_' . $this->id );
$query_arg_key = $this->get_query_arg_key();
if ( empty( $_GET[ $query_arg_key ] ) ) {
return;
}
// Get the value for this filter
// eg: http://www.website.com/reviews?foobar=xxx-yyy
$value = explode( '-', $_GET[ $query_arg_key ] );
// New meta query
$new_meta_query = array();
if ( 'below' !== $value[0] ) {
$new_meta_query[] = array(
'key' => $this->meta_key,
'value' => absint( $value[0] ),
'compare' => '>=',
'type' => 'numeric',
);
}
if ( 'above' !== $value[1] ) {
$new_meta_query[] = array(
'key' => $this->meta_key,
'value' => absint( $value[1] ),
'compare' => '<=',
'type' => 'numeric',
);
}
if ( 1 > sizeof( $new_meta_query ) ) {
return;
}
$new_meta_query = array_merge( $new_meta_query, array( 'relation' => 'AND' ) );
$meta_query = $query->get( 'meta_query' );
// Update meta query
$query->set( 'meta_query', array_merge( $meta_query, $new_meta_query ) );
}
public function load_choices( $field ) {
$meta_group_fields = acf_get_fields( 'group_5c1fa9a17c4cf' );
$field['choices'] = wp_list_pluck( $meta_group_fields, 'label', 'name' );
return $field;
}
public function get_meta_key() {
return $this->meta_key;
}
public function get_meta_prefix() {
return $this->meta_prefix;
}
public function get_query_arg_key() {
$query_arg_key = explode( $this->get_meta_prefix(), $this->get_meta_key() );
return $query_arg_key[1];
}
}
{
"key": "group_5c1fc4935d525",
"title": "Simple widget",
"fields": [
{
"key": "field_5c1fc4a5d4281",
"label": "Title:",
"name": "title",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
},
{
"key": "field_5c1ffd6057655",
"label": "Choose metadata:",
"name": "metadata",
"type": "select",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"choices": [],
"default_value": [],
"allow_null": 0,
"multiple": 0,
"ui": 0,
"return_format": "value",
"ajax": 0,
"placeholder": ""
},
{
"key": "field_5c1fc4bdcca1d",
"label": "Range:",
"name": "range",
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"collapsed": "",
"min": 1,
"max": 0,
"layout": "table",
"button_label": "Add new range",
"sub_fields": [
{
"key": "field_5c1fc50dcca1e",
"label": "From",
"name": "from",
"type": "number",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"min": 0,
"max": "",
"step": ""
},
{
"key": "field_5c1fc52acca1f",
"label": "To",
"name": "to",
"type": "number",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"min": 1,
"max": "",
"step": ""
}
]
}
],
"location": [
[
{
"param": "widget",
"operator": "==",
"value": "woocommerce_simple"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": "",
"modified": 1545600742
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment