Skip to content

Instantly share code, notes, and snippets.

@andrewreal
Created February 24, 2021 08:09
Show Gist options
  • Save andrewreal/532408b1a6852a003cbc469dc04b70a0 to your computer and use it in GitHub Desktop.
Save andrewreal/532408b1a6852a003cbc469dc04b70a0 to your computer and use it in GitHub Desktop.
Example of adding an option to a WooCommerce attribute
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
add_action( 'woocommerce_after_edit_attribute_fields', 'blz_attr_filter_option', 50 );
add_action( 'woocommerce_after_add_attribute_fields', 'blz_attr_filter_option', 50 );
add_action( 'admin_init' , 'blz_attr_save', 99 );
/**
* Display the populated checkbox for the Create Filter Widget option
*
* @return void
*/
function blz_attr_filter_option(){
$attribute_filter_widget = false;
if( isset( $_GET['edit'] ) ){
$attr_id = $_GET['edit'];
global $wpdb;
$attribute_to_edit = $wpdb->get_row(
$wpdb->prepare(
"
SELECT attribute_type, attribute_label, attribute_name, attribute_orderby, attribute_public
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d
",
$attr_id
)
);
$attribute_name = $attribute_to_edit->attribute_name;
$attribute_filter_widget = get_option( 'attribute_filter_widget_' . $attribute_name );
}
?>
<tr>
<th scope="row" valign="top">
<label for="attribute_filter_widget"><?php esc_html_e( 'Create Filter Widget', 'blz' ); ?></label>
</th>
<td>
<input name="attribute_filter_widget" id="attribute_filter_widget" type="checkbox" value="1" <?php checked( $attribute_filter_widget, 1 ); ?> />
<p class="description"><?php esc_html_e( 'Enable this if you want to create a filter widget on the category page.', 'blz' ); ?></p>
</td>
</tr>
<?php
}
/**
* Save the Filter Widget option
*/
function blz_attr_save( ){
if( isset( $_POST['attribute_name'] ) && isset( $_POST['save_attribute'] ) && $_POST['save_attribute'] == 'Update' ){
// Update existing attribute
$attribute_filter_widget = isset($_POST['attribute_filter_widget']) ? wc_clean( wp_unslash( $_POST['attribute_filter_widget'] ) ) : '';
$attribute_name = wc_clean( wp_unslash( $_POST['attribute_name'] ) );
$success = update_option( 'attribute_filter_widget_' . $attribute_name, $attribute_filter_widget );
return;
}
if ( isset( $_POST['add_new_attribute']) && $_POST['add_new_attribute'] == 'Add attribute' ){
// Add new attribute
$attribute_filter_widget = isset($_POST['attribute_filter_widget']) ? wc_clean( wp_unslash( $_POST['attribute_filter_widget'] ) ) : '';
if( isset( $_POST['attribute_name'] ) && ! empty( $_POST['attribute_name'] ) ){
$attribute_name = wc_clean( wp_unslash( $_POST['attribute_name'] ) );
$success = update_option( 'attribute_filter_widget_' . $attribute_name, $attribute_filter_widget );
} elseif ( isset( $_POST['attribute_label'] ) && ! empty( $_POST['attribute_label'] ) ){
$label = wc_clean( wp_unslash( $_POST['attribute_label'] ) );
$attribute_name = sanitize_title_with_dashes( $label ); // Likely, not guaranteed to be correct.
$success = update_option( 'attribute_filter_widget_' . $attribute_name, $attribute_filter_widget );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment