Skip to content

Instantly share code, notes, and snippets.

View andykeith's full-sized avatar

Andy Keith andykeith

View GitHub Profile
<?php
add_filter( 'wc_product_table_query_args', function( $args, WC_Product_Table_Query $wpt_query ) {
// The 'args' property - type WC_Product_Table_Args - contains all the shortcode options used to create the table.
// Example checking the category option...
if ( 'something' === $wpt_query->args->category ) {
// Do something with the query args for this table
}
@andykeith
andykeith / old-plugin-license-setup.php
Last active August 12, 2019 15:07
An example (from WooCommerce Quick View Pro) of our current plugin license setup, including adding the license setting to the settings page. This is to be replaced by a new license system currently in development.
<?php
final class Quick_View_Plugin {
const NAME = 'WooCommerce Quick View Pro'; // should match download name in EDD
const VERSION = '1.1';
const FILE = __FILE__;
private static $BASENAME = null;
/* The plugin license manager. */
<?php
class My_WooCommerce_Plugin implements Registerable {
const ITEM_ID = 1234; // The Download ID in EDD
const NAME = 'My Cool Plugin';
const VERSION = '1.0';
const FILE = __FILE__;
private $license_controller;
@andykeith
andykeith / jquery-serialize-form.js
Created February 4, 2019 16:44
Serialize a form in jQuery, handling radio/checkbox arrays.
/**
* Parse a form to an object in the format: { input1: 'value', input2: 'some value', etc }.
*
* @param {jQuery} $form The form to parse
* @returns {Object} The serialized form
*/
function serializeForm( $form ) {
var serialized = $form.serializeArray(),
s = '', name = '', prefix = '', index = 0,
data = { }, arrayNames = { };
@andykeith
andykeith / product-table-preload-fonts.php
Last active February 4, 2019 16:45
Preload icon font used in WooCommerce Product Table
<?php
add_action( 'wp_head', function() {
if ( class_exists( 'WC_Product_Table_Plugin' ) ) {
echo '<link rel="preload" href="' . plugins_url( 'assets/fonts/fa-solid-900.woff2', WC_Product_Table_Plugin::FILE ) . '" as="font" type="font/woff2" crossorigin="anonymous">' . "\n";
}
} );
@andykeith
andykeith / custom-add-to-cart-hide-text-shop-page.php
Last active February 4, 2019 16:47
WooCommerce Custom Add to Cart - hide add to cart text on shop page only.
<?php
add_filter( 'body_class', function( $classes ) {
if ( function_exists( 'is_shop' ) && is_shop() ) {
$classes = array_diff( $classes, array( 'wc-add-to-cart-no-text' ) );
}
return $classes;
}, 20 );
@andykeith
andykeith / product-table-new-tab-homepage.php
Last active February 4, 2019 16:46
WooCommerce Product Table - open products in new tab on home page only
<?php
add_action( 'template_redirect', function() {
if ( ! is_front_page() ) {
add_filter( 'wc_product_table_open_products_in_new_tab', '__return_true' );
}
} );
@andykeith
andykeith / product-table-format-woocommerce-price.php
Last active February 4, 2019 16:47
WooCommerce Product Table - add currency to regular price custom field in WooCommerce.
<?php
// Assuming you have cf:_regular_price as a column in your table...
add_filter( 'wc_product_table_data_custom_field__regular_price, function( $price, $product ) {
return '€' . $price;
} );
@andykeith
andykeith / product-table-replace-regular-with-sale-price.php
Last active February 4, 2019 16:47
WPT - replace sale price with regular price if no sale price set for product
<?php
// Assuming you have cf:_sale_price as a column in your table...
add_filter( 'wc_product_table_data_custom_field__sale_price, function( $price, $product ) {
if ( empty( $price ) ) {
$price = get_post_meta( $product->get_id(), '_regular_price', true );
}
return $price;
} );