Created
September 19, 2017 16:50
-
-
Save EricBusch/b30e411a2442ab94a37c97235fecd80e to your computer and use it in GitHub Desktop.
GearGrabber Custom Plugin - The following code is used on https://www.geargrabber.net/ to further customize various Datafeedr elements. [datafeedr]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: GearGrabber | |
* Plugin URI: https://www.geargrabber.net/ | |
* Description: Custom code for https://www.geargrabber.net/ | |
* Text Domain: geargrabber | |
* Domain Path: /languages | |
* Version: 1.0.12 | |
* | |
* @package geargrabber | |
*/ | |
// Plugin Version | |
define( 'GG_VERSION', '1.0.12' ); | |
/** | |
* Include the custom search functionality. | |
* | |
* This code replaces the default functionality of the WooCommerce search form. If a search is | |
* entered into the WooCommerce search form, this "hijacks" the request and redirects the request | |
* to your custom search page. Then it takes the search query and passes it to the | |
* Datafeedr API. The results are returned and displayed. | |
* | |
* @link https://gist.github.com/EricBusch/f4085d23f21b1922efb58d90f2246638 | |
* @link https://gist.github.com/EricBusch/ebe3491e7895e438cc65e34d278b60cb | |
*/ | |
include_once( 'custom-search.php' ); | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
// WordPress Hooks | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
/** | |
* Enable shortcodes in Site's Tagline. | |
* | |
* This code enables shortcodes in the site's tagline. For example, if you add a shortcode to | |
* the WordPress Admin Area > Settings > General > Tagline field, it will be output properly on your site | |
* in all places that the tagline is displayed. | |
* | |
* @see get_shortcode_regex(), do_shortcode(). | |
* @link https://developer.wordpress.org/reference/functions/bloginfo/#possible-values-for-show | |
* | |
* @param string $text The text we should display. | |
* @param string $show The item of bloginfo we are showing. In this case, 'description'. | |
* | |
* @return string The possibly updated tagline. | |
*/ | |
add_filter( 'bloginfo', 'gg_enable_shortcodes_in_tagline', 10, 2 ); | |
function gg_enable_shortcodes_in_tagline( $text, $show ) { | |
if ( 'description' !== $show ) { | |
return $text; | |
} | |
$pattern = get_shortcode_regex(); | |
if ( preg_match_all( '/' . $pattern . '/s', $text, $matches ) && array_key_exists( 2, $matches ) ) { | |
$text = do_shortcode( $text ); | |
} | |
return $text; | |
} | |
/** | |
* Create the shortcode [gg_total_products] to display total products in store. | |
* | |
* This shortcode can be used anywhere on your site where shortcodes are supported to display | |
* the total number of products in your store. | |
* | |
* @link http://php.net/manual/en/function.number-format.php | |
* | |
* @param array $attributes Array of values to use in number_format() function. See PHP's number_format() function. | |
* | |
* @return string Formatted number. | |
*/ | |
add_shortcode( 'gg_total_products', 'gg_shortcode_total_products' ); | |
function gg_shortcode_total_products( $attributes ) { | |
$defaults = array( | |
'decimals' => 0, | |
'dec_point' => '.', | |
'thousands_sep' => ',', | |
); | |
$args = wp_parse_args( $attributes, $defaults ); | |
$total_products = gg_get_total_products_in_store(); | |
$total_products = number_format( $total_products, $args['decimals'], $args['dec_point'], $args['thousands_sep'] ); | |
return $total_products; | |
} | |
/** | |
* Delete the image and all of its derivatives for a product moved to the trash. | |
* | |
* @param integer $post_id The Post ID of a product to delete the image of. | |
*/ | |
add_action( 'trashed_post', 'gg_delete_attached_thumbnail_for_trashed_product', 20, 1 ); | |
function gg_delete_attached_thumbnail_for_trashed_product( $post_id ) { | |
// Don't do unless post type is 'product'. | |
$post_type = get_post_type( $post_id ); | |
if ( $post_type != 'product' ) { | |
return; | |
} | |
// Get attachment ID to delete. | |
$post_thumbnail_id = get_post_thumbnail_id( $post_id ); | |
// Delete attachment. | |
wp_delete_attachment( $post_thumbnail_id, true ); | |
} | |
/** | |
* Load plugin's style.css file. | |
*/ | |
add_action( 'wp_enqueue_scripts', 'gg_enqueue_scripts', 999 ); | |
function gg_enqueue_scripts() { | |
$handle = 'gg_style'; | |
$src = plugin_dir_url( __FILE__ ) . 'style.css'; | |
$deps = 'storefront-style-css'; // Change depending on theme. | |
$ver = GG_VERSION; | |
$media = 'all'; | |
wp_register_style( $handle, $src, $deps, $ver, $media ); | |
wp_enqueue_style( $handle ); | |
} | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
// WooCommerce Hooks | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
/** | |
* This rewrites WooCommerce category page titles (ie. <h1> tags) | |
* | |
* This rewrites the category page title on WooCommerce category pages and adds the word "sale" | |
* as well as the brand (if that exists). | |
* | |
* @param string $page_title Current page title. | |
* | |
* @return string Updated page title. | |
*/ | |
add_filter( 'woocommerce_page_title', 'gg_woocommerce_category_page_title', 20, 1 ); | |
function gg_woocommerce_category_page_title( $page_title ) { | |
if ( ! is_product_category() ) { | |
return $page_title; | |
} | |
$brand = gg_get_brand_by_filter(); | |
$title = 'SALE ' . $brand . ' ' . $page_title; | |
return gg_remove_extra_spaces( $title ); | |
} | |
/** | |
* Remove short description from WooCommerce product pages. | |
* | |
* This will remove the short description that appears below the product's title on | |
* WooCommerce product pages. | |
* | |
* @param string $description The product's post_excerpt. | |
* | |
* @return string Returns empty string. | |
*/ | |
add_filter( 'woocommerce_short_description', 'gg_remove_woocommerce_short_description' ); | |
function gg_remove_woocommerce_short_description( $description ) { | |
$description = ''; | |
return $description; | |
} | |
/** | |
* Put the Comparison Set immediately below a Product's Name. | |
* | |
* The following 2 actions and 1 function will move a Comparison Set from displaying underneath the WooCommerce | |
* product's short description to below the product's name and above it's price. | |
* | |
* @see remove_action(), dfrcs_wc_compset_priority() | |
*/ | |
add_action( 'woocommerce_single_product_summary', 'dfrcs_wc_single_product_page_compset', 6 ); | |
add_action( 'wp_head', 'gg_remove_compset_from_woocommerce_product_pages' ); | |
function gg_remove_compset_from_woocommerce_product_pages() { | |
remove_action( 'woocommerce_after_single_product_summary', 'dfrcs_wc_single_product_page_compset', 0 ); | |
} | |
/** | |
* Replace SKU with EAN or UPC or SKU. | |
* | |
* This code will replace the default SKU value with either the EAN or UPC or SKU provided | |
* by the merchant for products imported by the Datafeedr Product Sets plugin. | |
* | |
* @param string $sku SKU value. | |
* @param WC_Product $product | |
* | |
* @return string|false Updated SKU value or false if none found. | |
*/ | |
add_filter( 'woocommerce_product_get_sku', 'gg_replace_sku_with_ean_or_upc', 20, 2 ); | |
function gg_replace_sku_with_ean_or_upc( $sku, $product ) { | |
if ( ! is_a( $product, 'WC_Product_External' ) ) { | |
return $sku; | |
} | |
if ( ! dfrpswc_is_dfrpswc_product( $product->get_id() ) ) { | |
return $sku; | |
} | |
$dfrps_product = get_post_meta( $product->get_id(), '_dfrps_product', true ); | |
$code = false; | |
if ( isset( $dfrps_product['ean'] ) ) { | |
$code = $dfrps_product['ean']; | |
} elseif ( isset( $dfrps_product['upc'] ) ) { | |
$code = $dfrps_product['upc']; | |
} elseif ( isset( $dfrps_product['sku'] ) ) { | |
$code = $dfrps_product['sku']; | |
} | |
return $code; | |
} | |
/** | |
* Remove "Buy" button and price. | |
* | |
* This will remove the "Buy" button and the price from a single WooCommerce product page | |
* if the product already has a Comparison Set. | |
*/ | |
add_action( 'woocommerce_before_single_product', 'gg_remove_button_price_from_product_page_if_compset_exists' ); | |
function gg_remove_button_price_from_product_page_if_compset_exists() { | |
$source = dfrcs_wc_get_source_of_product(); | |
$source['context'] = 'wc_single_product_page'; | |
$compset = new Dfrcs( $source ); | |
if ( ! $compset->cached ) { | |
return; | |
} | |
if ( $compset->cache_is_expired ) { | |
return; | |
} | |
if ( ! $compset->meets_min_num_product_requirement() ) { | |
return; | |
} | |
// Remove Price | |
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 ); | |
// Remove Buy Button | |
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 ); | |
} | |
/** | |
* Reorder tabs on single WooCommerce product pages. | |
* | |
* @param array $tabs Tabs information. | |
* | |
* @return array Updated $tabs array. | |
*/ | |
add_filter( 'woocommerce_product_tabs', 'gg_reorder_woocommerce_product_tabs', 98 ); | |
function gg_reorder_woocommerce_product_tabs( $tabs ) { | |
$tabs['additional_information']['priority'] = 5; // Additional information first | |
$tabs['description']['priority'] = 10; // Description second | |
$tabs['reviews']['priority'] = 15; // Reviews last | |
return $tabs; | |
} | |
/** | |
* Remove "reviews" tab on WooCommerce Product pages. | |
* | |
* @return array Updated $tabs array. | |
*/ | |
add_filter( 'woocommerce_product_tabs', 'gg_remove_product_tabs', 98 ); | |
function gg_remove_product_tabs( $tabs ) { | |
unset( $tabs['reviews'] ); | |
return $tabs; | |
} | |
/** | |
* Add "Sort by discount" to sorting option (frontend). | |
* | |
* @link http://docs.woothemes.com/document/custom-sorting-options-ascdesc/ | |
* | |
* @param array $args Ordering args. | |
* | |
* @return array Updated $args array. | |
*/ | |
add_filter( 'woocommerce_get_catalog_ordering_args', 'gg_woocommerce_get_catalog_ordering_args' ); | |
function gg_woocommerce_get_catalog_ordering_args( $args ) { | |
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ); | |
if ( 'discount' == $orderby_value ) { | |
$args['orderby'] = 'meta_value_num'; | |
$args['order'] = 'DESC'; | |
$args['meta_key'] = '_dfrps_salediscount'; | |
} | |
return $args; | |
} | |
/** | |
* Add the text to display on the front end for the Sort by drop down. | |
* | |
* @param array $sortby Sortby texts. | |
* | |
* @return array Updated Sortby texts. | |
*/ | |
add_filter( 'woocommerce_default_catalog_orderby_options', 'gg_woocommerce_catalog_orderby' ); | |
add_filter( 'woocommerce_catalog_orderby', 'gg_woocommerce_catalog_orderby' ); | |
function gg_woocommerce_catalog_orderby( $sortby ) { | |
$sortby['discount'] = 'Sort by discount'; | |
return $sortby; | |
} | |
/** | |
* Display 24 products per page. | |
* | |
* $cols contains the current number of products per page based on the value stored on Options -> Reading | |
* | |
* @link https://docs.woocommerce.com/document/change-number-of-products-displayed-per-page/ | |
* | |
* @param integer $cols Number of products to display. | |
* | |
* @return integer Updated | |
*/ | |
add_filter( 'loop_shop_per_page', 'gg_loop_shop_per_page', 999 ); | |
function gg_loop_shop_per_page( $cols ) { | |
$cols = 24; | |
return $cols; | |
} | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
// Datafeedr Product Sets Hooks | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
/** | |
* This forces the update script to ignore if an update is already running and forces | |
* the Product Set updater to run anyway. | |
* | |
* Made possible by table locking in dfrps_get_existing_post(). | |
*/ | |
add_filter( 'dfrps_doing_update', '__return_false' ); | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
// Datafeedr WooCommerce Importer Hooks | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
/** | |
* Add the custom attribute label "Special Promotion" to a product. | |
* | |
* @param array $attributes An array attributes. | |
* @param array $post An array of post data including ID, post_title, post_status, etc... | |
* @param array $product An array of product data returned from the Datafeedr API. | |
* @param array $set A post array for this Product Set with an array key of postmeta containing all post meta data. | |
* @param string $action The action the Product Set is performing. Value are either "insert" or "update". | |
* | |
* @return array Updated $attributes array. | |
*/ | |
add_filter( 'dfrpswc_product_attributes', 'gg_set_promo_attribute_label', 20, 5 ); | |
function gg_set_promo_attribute_label( $attributes, $post, $product, $set, $action ) { | |
// The label for the product field. | |
$label = 'Special Promotion'; | |
// The product field to import. | |
$field = 'promo'; | |
if ( ! isset( $product[ $field ] ) ) { | |
return $attributes; | |
} | |
if ( isset( $attributes[ sanitize_title( $label ) ] ) ) { | |
return $attributes; | |
} | |
if ( empty( $attributes ) ) { | |
$attributes = array(); | |
} | |
$attributes[ $label ]['name'] = $label; | |
return $attributes; | |
} | |
/** | |
* Set value for "Special Promotion" attribute. | |
* | |
* @param array|string $value The current value of the $attribute for this $post. | |
* @param string $attribute The slug of the attribute. Examples: pa_color or pa_shoe-size | |
* @param array $post An array of post data including ID, post_title, post_status, etc... | |
* @param array $product An array of product data returned from the Datafeedr API. | |
* @param array $set A post array for this Product Set with an array key of postmeta containing all post meta data. | |
* @param string $action The action the Product Set is performing. Value are either "insert" or "update". | |
* | |
* @return array|string The updated attribute's value. | |
*/ | |
add_filter( 'dfrpswc_filter_attribute_value', 'gg_set_promo_attribute_value', 30, 6 ); | |
function gg_set_promo_attribute_value( $value, $attribute, $post, $product, $set, $action ) { | |
// The label for the product field. | |
$label = 'Special Promotion'; | |
// The product field to import. | |
$field = 'promo'; | |
if ( ! isset( $product[ $field ] ) ) { | |
return $value; | |
} | |
if ( $attribute == $label ) { | |
return $product[ $field ]; | |
} | |
return $value; | |
} | |
/** | |
* Normalize brand names. | |
* | |
* The attribute "Brand" with a slug of "brand" must already exist here: | |
* WordPress Admin Area > Products > Attributes. | |
* | |
* @param array|string $value The current value of the $attribute for this $post. | |
* @param string $attribute The slug of the attribute. Examples: pa_color or pa_shoe-size | |
* @param array $post An array of post data including ID, post_title, post_status, etc... | |
* @param array $product An array of product data returned from the Datafeedr API. | |
* @param array $set A post array for this Product Set with an array key of postmeta containing all post meta data. | |
* @param string $action The action the Product Set is performing. Value are either "insert" or "update". | |
* | |
* @return array|string | |
*/ | |
add_filter( 'dfrpswc_filter_attribute_value', 'gg_add_brand_attribute', 20, 6 ); | |
function gg_add_brand_attribute( $value, $attribute, $post, $product, $set, $action ) { | |
if ( $attribute != 'pa_brand' ) { | |
return $value; | |
} | |
if ( isset( $product['brand'] ) ) { | |
return gg_get_brand( $product['brand'], $product['brand'] ); | |
} | |
return $value; | |
} | |
/** | |
* Add text before product description for products that are on sale. Text reads like this: | |
* | |
* "This Therm-A-Rest product is currently ON SALE at The House. SAVE 40% on | |
* this and many other products at The House. Buy now at The House!" | |
* | |
* @param array $post An array of post data including ID, post_title, post_status, etc... | |
* @param array $product An array of product data returned from the Datafeedr API. | |
* @param array $set A post array for this Product Set with an array key of postmeta containing all post meta data. | |
* @param string $action The action the Product Set is performing. Value are either "insert" or "update". | |
* | |
* @return array Updated $post array. | |
*/ | |
add_filter( 'dfrpswc_filter_post_array', 'gg_modify_product_description', 20, 4 ); | |
function gg_modify_product_description( $post, $product, $set, $action ) { | |
if ( ! isset( $product['salediscount'] ) ) { | |
return $post; | |
} | |
$discount = intval( $product['salediscount'] ); | |
$url = dfrapi_url( $product ); | |
$merchant = esc_html( $product['merchant'] ); | |
$brand = isset( $product['brand'] ) ? esc_html( gg_get_brand( $product['brand'], $product['brand'] ) ) : ''; | |
$promo = 'This <strong>' . $brand . '</strong> product is currently <strong>ON SALE!</strong><br /> '; | |
$promo .= ' <strong>SAVE ' . $discount . '%</strong> at <a href="' . $url . '" target="_blank">' . $merchant . '!</a> '; | |
$post['post_content'] = '<p class="promo">' . $promo . '</p><p>' . $post['post_content'] . '</p>'; | |
return $post; | |
} | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
// Datafeedr Comparison Set Hooks | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
/** | |
* Change button from "View" to "Buy" in Comparison Set on product pages. | |
* | |
* @param string $text Button text | |
* @param array $product Array of product information returned from Datafeedr API. | |
* @param Dfrcs $compset | |
* | |
* @return string Updated button text. | |
*/ | |
add_filter( 'dfrcs_link_text', 'gg_change_link_text_on_single_product_pages', 20, 3 ); | |
function gg_change_link_text_on_single_product_pages( $text, $product, $compset ) { | |
if ( ! is_product() ) { | |
return $text; | |
} | |
$text = 'BUY'; | |
return $text; | |
} | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
// WordPress SEO (Yoast) Hooks | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
/** | |
* Modify the title that appears in the <title> tags for WooCommerce products. | |
* | |
* This is STRICTLY a WordPress SEO plugin modification. It does not work | |
* if the WordPress SEO plugin is not installed and activated. | |
* | |
* Additionally, it only works on WooCommerce Product pages. | |
* | |
* You can modify the $title_prefix and $title_suffix to prepend or append | |
* content to the title that gets inserted between the <title></title> tags | |
* on your site. | |
* | |
* This changes titles from this: | |
* | |
* "Men's Patagonia Puff Jacket - GearGrabber" | |
* | |
* To this: | |
* | |
* "25% OFF Patagonia Men's Puff Jacket SALE - GearGrabber" | |
* | |
* @see get_bloginfo(), is_product() | |
* @global WP_Post $post | |
* | |
* @param string $title The current title. | |
* | |
* @return string Updated $title. | |
*/ | |
add_filter( 'wpseo_title', 'gg_wpseo_single_product_page_title', 20, 1 ); | |
function gg_wpseo_single_product_page_title( $title ) { | |
global $post; | |
if ( ! is_product() ) { | |
return $title; | |
} | |
if ( ! dfrpswc_is_dfrpswc_product( $post->ID ) ) { | |
return $title; | |
} | |
$dfrps_product = get_post_meta( $post->ID, '_dfrps_product', true ); | |
if ( empty( $dfrps_product ) || ! $dfrps_product ) { | |
return $title; | |
} | |
$discount = ( isset( $dfrps_product['salediscount'] ) ) ? absint( $dfrps_product['salediscount'] ) : 0; | |
if ( $discount < 1 ) { | |
return $title; | |
} | |
$sep = ' - '; | |
$title_prefix = ''; | |
$title_suffix = ' SALE '; | |
$site_title = get_bloginfo( 'name' ); | |
$product_title = ' ' . str_ireplace( $site_title, '', $title ); | |
$brand = isset( $dfrps_product['brand'] ) ? $dfrps_product['brand'] : '_NO_BRAND_'; | |
$product_title = str_ireplace( $brand, '', $product_title ); | |
// Remove some extra characters from the product title | |
$product_title = str_replace( array( " - ", "(", ")", "," ), array( " ", "", "", "" ), $product_title ); | |
if ( '_NO_BRAND_' == $brand ) { | |
$brand = ''; | |
} | |
$title_prefix .= $discount . '% OFF ' . $brand; | |
$title = $title_prefix . $product_title . $title_suffix . $sep . $site_title; | |
return gg_remove_extra_spaces( $title ); | |
} | |
/** | |
* Enable shortcodes in WordPress SEO (Yoast) titles. | |
* | |
* This is STRICTLY a WordPress SEO plugin modification. It does not work | |
* if the WordPress SEO plugin is not installed and activated. | |
* | |
* @param string $title The current title. | |
* | |
* @return string Updated $title. | |
*/ | |
add_filter( 'wpseo_title', 'gg_enable_shortcodes_in_titles', 30, 1 ); | |
function gg_enable_shortcodes_in_titles( $title ) { | |
return do_shortcode( $title ); | |
} | |
/** | |
* Add metadesc to WooCommerce Category Pages. | |
* | |
* This returns a WooCommerce category description to be used in the <meta> | |
* tags by the WordPress SEO (Yoast) plugin. This will NOT work if Yoast is not installed. | |
* | |
* @param string $description Category description. | |
* | |
* @return string Updated category description. | |
*/ | |
add_filter( 'wpseo_metadesc', 'gg_wpseo_woocommerce_category_metadesc' ); | |
function gg_wpseo_woocommerce_category_metadesc( $description ) { | |
if ( ! is_product_category() ) { | |
return $description; | |
} | |
if ( ! empty( $description ) ) { | |
return $description; | |
} | |
$description = gg_woocommerce_category_description(); | |
return $description; | |
} | |
/** | |
* Enable shortcodes in WordPress SEO (Yoast) meta descriptions. | |
* | |
* This is STRICTLY a WordPress SEO plugin modification. It does not work | |
* if the WordPress SEO plugin is not installed and activated. | |
* | |
* @param string $description The current description. | |
* | |
* @return string Updated $description. | |
*/ | |
add_filter( 'wpseo_metadesc', 'gg_enable_shortcodes_in_metadesc', 30, 1 ); | |
function gg_enable_shortcodes_in_metadesc( $description ) { | |
return do_shortcode( $description ); | |
} | |
/** | |
* Modify the title tag of WooCommerce category pages. | |
* | |
* This is STRICTLY a WordPress SEO plugin modification. It does not work | |
* if the WordPress SEO plugin is not installed and activated. | |
* | |
* This overrides any setting entered here WordPress Admin Area > SEO > Titles & Metas > Taxonomies > Product categories | |
* | |
* This uses the $format of: | |
* | |
* SALE %%brand%% %%term_title%% %%sep%% %%brand%% %%term_title%% Sale, Discount & Clearance %%page%% %%sep%% %%sitename%% | |
* | |
* @see is_product_category() | |
* @global WP_Query $wp_query | |
* | |
* @param string $title Page title tag. | |
* | |
* @return string Updated $title. | |
*/ | |
add_filter( 'wpseo_title', 'gg_wpseo_woocommerce_category_page_title', 20, 1 ); | |
function gg_wpseo_woocommerce_category_page_title( $title ) { | |
if ( ! is_product_category() ) { | |
return $title; | |
} | |
// %1$s- Brand Filter | |
$brand = gg_get_brand_by_filter(); | |
// %2$s - Category Name | |
$title = single_term_title( '', false ); | |
// %3$s - Separator | |
$sep = '|'; | |
// %4$s - Page Number | |
global $wp_query; | |
$total = $wp_query->max_num_pages; | |
$page = ( get_query_var( 'paged' ) ) ? $sep . ' Page: ' . get_query_var( 'paged' ) . ' of ' . $total : ''; | |
// %5$s - Sitename | |
$sitename = get_bloginfo( 'name' ); | |
// SALE %%brand%% %%term_title%% %%sep%% %%brand%% %%term_title%% Sale, Discount & Clearance %%page%% %%sep%% %%sitename%% | |
$format = 'SALE %1$s %2$s %3$s %1$s %2$s Sale, Discount & Clearance %4$s %3$s %5$s'; | |
$title = sprintf( $format, $brand, $title, $sep, $page, $sitename ); | |
return gg_remove_extra_spaces( $title ); | |
} | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
// Helper Functions | |
/* ------------------------------------------------------------------------------------------------------------- */ | |
/** | |
* Dynamically generates a description of a WooCommerce product category. | |
* | |
* @global WP_Query $wp_query | |
* | |
* @return string | |
*/ | |
function gg_woocommerce_category_description() { | |
$description = ''; | |
if ( ! is_product_category() ) { | |
return $description; | |
} | |
global $wp_query; | |
$category = $wp_query->get_queried_object(); | |
// %1$s- Brand Filter | |
$brand = gg_get_brand_by_filter(); | |
// %2$s - Category Name | |
$title = strtolower( single_term_title( '', false ) ); | |
// %3$s - Number of products in category. | |
$count = number_format( $category->count ); | |
// Example: Browse all 34 Patagonia men's jackets on sale. | |
$format = 'Browse all %3$s %1$s %2$s on sale.'; | |
$description = sprintf( $format, $brand, $title, $count ); | |
return gg_remove_extra_spaces( $description ); | |
} | |
/** | |
* Removes multiple spaces from a string. | |
* | |
* @param string $string | |
* | |
* @return string String with extra spaces removed. | |
*/ | |
function gg_remove_extra_spaces( $string ) { | |
return str_replace( | |
array( ' ', ' ', ' ' ), | |
array( ' ', ' ', ' ' ), | |
$string | |
); | |
} | |
/** | |
* Returns the brand name if ?filter_brand is set. | |
* | |
* For example, ?filter_brand=la-sportiva will return La Sportiva | |
* | |
* @return string | |
*/ | |
function gg_get_brand_by_filter() { | |
$brand = ''; | |
if ( ! isset( $_GET['filter_brand'] ) ) { | |
return $brand; | |
} | |
$term = get_term_by( 'slug', sanitize_title( $_GET['filter_brand'] ), 'pa_brand' ); | |
if ( ! isset( $term->name ) ) { | |
return $brand; | |
} | |
$brand = $term->name; | |
return $brand; | |
} | |
/** | |
* This overrides the already existing function here: | |
* | |
* ~/wp-content/plugins/woocommerce/includes/wc-template-functions.php:667 | |
*/ | |
function woocommerce_taxonomy_archive_description() { | |
if ( is_product_taxonomy() && 0 === absint( get_query_var( 'paged' ) ) ) { | |
$description = gg_woocommerce_category_description(); | |
if ( $description ) { | |
echo '<div class="term-description">' . $description . '</div>'; | |
} | |
} | |
} | |
/** | |
* Get the total number of published products in the store. | |
* | |
* This counts ALL of the products in your WooCommerce store and returns the value, caching the value for 15 | |
* minutes. | |
* | |
* @see get_posts() | |
* | |
* @return integer Number of products in the store. | |
*/ | |
function gg_get_total_products_in_store() { | |
$option_name = 'gg_total_products'; | |
$total_products = get_transient( $option_name ); | |
if ( false === $total_products || empty ( $total_products ) ) { | |
$total_products = count( get_posts( array( | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'fields' => 'ids', | |
'posts_per_page' => '-1' | |
) ) ); | |
set_transient( $option_name, $total_products, ( MINUTE_IN_SECONDS * 15 ) ); | |
} | |
return $total_products; | |
} | |
/** | |
* Function to normalize brand names. | |
* | |
* @link https://datafeedrapi.helpscoutdocs.com/article/156-normalize-brand-names | |
* | |
* @param string $field The value we are looking for. | |
* @param string $default Optional. Value to use if $field is not present. | |
* | |
* @return array|string | |
*/ | |
function gg_get_brand( $field, $default = '' ) { | |
$map = array(); | |
// ++++++++++ Begin Editing Here ++++++++++ | |
$map["Arc'teryx"] = array( "Arcteryx", "Arcteryx.com", "Arc'teryx", "Arc teryx" ); | |
$map["Adidas"] = array( "Adidas", "Adidas Outdoor" ); | |
$map["Alpinestars"] = array( "Alpine Stars" ); | |
$map["ALPS Mountaineering"] = array( "alps" ); | |
$map["Altra"] = array( "Altra" ); | |
$map["Aquatherm"] = array( "Aquatherm By Santana Canada" ); | |
$map["Astral"] = array( "Astral" ); | |
$map["Bell"] = array( "Bell" ); | |
$map["Bergans"] = array( "Bergans Of Norway" ); | |
$map["Bern"] = array( "Bern" ); | |
$map["Big Agnes"] = array( "Big Agnes" ); | |
$map["BIC"] = array( "bic" ); | |
$map["Black Diamond"] = array( "Black Diamond", "Black Diamond Equipment" ); | |
$map["Blue Line 409"] = array( "Blue Line 409 Llc" ); | |
$map["Bogner"] = array( "Bogner", "Bogner Fire + Ice", "Fire And Ice", "Bogner Fire And Ice" ); | |
$map["Bond"] = array( "Bond Outerwear" ); | |
$map["Books"] = array( "Book:" ); | |
$map["Boreas"] = array( "Boreas" ); | |
$map["Brooks"] = array( "Brooks" ); | |
$map["Burton"] = array( "Burton" ); | |
$map["C.A.M.P."] = array( "CAMP USA", "C.A.M.P." ); | |
$map["Campagnolo"] = array( "Campagnolo" ); | |
$map["Carhartt"] = array( | |
"Carhartt, Inc", | |
"Carhartt, Inc.", | |
"Carhartt Inc", | |
"Carhartt Inc.", | |
"Carhartt" | |
); | |
$map["CEP"] = array( "CEP Sports" ); | |
$map["Chrome"] = array( "Chrome Industries" ); | |
$map["Club Ride"] = array( "Club Ride" ); | |
$map["Columbia River"] = array( "Columbia River" ); | |
$map["Columbia"] = array( "Columbia", "Columbia Sportswear" ); | |
$map["Craghoppers "] = array( "Crag Hoppers", "Craghoppers" ); | |
$map["CWB"] = array( "Cwb" ); | |
$map["DC Shoe Co"] = array( "DC" ); | |
$map["Descente"] = array( "Descente" ); | |
$map["Dimension"] = array( "Dimension" ); | |
$map["DMM"] = array( "DMM" ); | |
$map["Easton"] = array( "Easton" ); | |
$map["EMS"] = array( "Eastern Mountain Sports", "EMS" ); | |
$map["Emu"] = array( "Emu Australia" ); | |
$map["Evolv"] = array( "Evolve" ); | |
$map["ExOfficio"] = array( "Ex Officio" ); | |
$map["Exped"] = array( "Exped Llc" ); | |
$map["Fischer"] = array( "Fischer" ); | |
$map["Five Ten"] = array( "FiveTen", "5.10" ); | |
$map["Fixe"] = array( "Fixe" ); | |
$map["Fjallraven"] = array( "Fjall Raven" ); | |
$map["Flylow"] = array( "Flylow" ); | |
$map["Foursquare"] = array( "Four Square" ); | |
$map["Fox"] = array( "Fox" ); | |
$map["G3"] = array( "G3" ); | |
$map["GNU"] = array( "Gnu" ); | |
$map["Garmin"] = array( "Garmin" ); | |
$map["Goal Zero"] = array( "Goalzero" ); | |
$map["HippyTree"] = array( "Hippy Tree" ); | |
$map["HO"] = array( "Ho" ); | |
$map["Herschel Supply Co"] = array( "Herschel Supply Co", "Herschel Supply" ); | |
$map["Holden"] = array( "Holden" ); | |
$map["Howler Brothers"] = array( "Howler Bros" ); | |
$map["Ibex"] = array( "Ibex", "Ibex Outdoor Clothing" ); | |
$map["Icebreaker"] = array( "Ice breaker" ); | |
$map["Inov8"] = array( "Inov-8" ); | |
$map["Jetboil"] = array( "Jet Boil" ); | |
$map["Jack O'Neill"] = array( "Jack O'neill", "Jack Oneill" ); | |
$map["K2"] = array( "K2" ); | |
$map["Keen"] = array( "Keen" ); | |
$map["Kinetic"] = array( "Kinetic" ); | |
$map["Kühl"] = array( "K?Hl", "Kühl", "Kuhl" ); | |
$map["La Sportiva"] = array( "La Sportiva", "LaSportiva" ); | |
$map["Level Six"] = array( "LevelSix", "Level 6", "Level6" ); | |
$map["Liberty Mountain"] = array( "Liberty Mtn", "Liberty Mountain" ); | |
$map["Life Is Good"] = array( "Life Is Good", "LifeIsGood" ); | |
$map["LOOK"] = array( "LOOK" ); | |
$map["Metolius"] = array( "Metolius" ); | |
$map["Montbell"] = array( "Mont Bell" ); | |
$map["MSR"] = array( "MSR" ); | |
$map["MTI"] = array( "Mti" ); | |
$map["MHM"] = array( "Mhm" ); | |
$map["Nathan Sports"] = array( "Nathan" ); | |
$map["Nemo"] = array( "Nemo" ); | |
$map["New Balance"] = array( "New Balance" ); | |
$map["Nike"] = array( "Nike" ); | |
$map["NRS"] = array( "NRS", "Nrs", "nrs" ); | |
$map["O'Brien"] = array( "O Brien" ); | |
$map["O'Neill"] = array( "O'Neill", "O Neill", "ONeill" ); | |
$map["Olicamp"] = array( "Oli Camp" ); | |
$map["Omega Pacific"] = array( "Omega", "Omegapacific" ); | |
$map["Osprey"] = array( "Osprey" ); | |
$map["Obermeyer"] = array( "Sport Obermeyer", "Obermeyer", "Obermeyer Sport" ); | |
$map["Outdoor Research"] = array( "Outdoor Research" ); | |
$map["Patagonia"] = array( "Patagonia", "Patagonia Inc" ); | |
$map["Petzl"] = array( "Petzl" ); | |
$map["POC"] = array( "POC" ); | |
$map["Polar"] = array( "Polar" ); | |
$map["POW Gloves"] = array( "pow" ); | |
$map["Prana"] = array( "Prana" ); | |
$map["Pro-Tec"] = array( "Pro-Tec", "pro tec" ); | |
$map["Ride"] = array( "Ride Snowboards" ); | |
$map["Rocky"] = array( "Rocky S2V" ); | |
$map["Roxy"] = array( "Roxy" ); | |
$map["Salomon"] = array( "Salomon", "Salomon North Americ", "Salomon North America" ); | |
$map["Scott"] = array( "Scott" ); | |
$map["SE Bicycles"] = array( "SE" ); | |
$map["Sigma"] = array( "Sigma" ); | |
$map["Skirt Sports"] = array( "Skirtsports" ); | |
$map["Smartwool"] = array( "SmartWool", "Smart Wool" ); | |
$map["Spy"] = array( "Spy" ); | |
$map["Spyder"] = array( "Spyder" ); | |
$map["SRAM"] = array( "SRAM" ); | |
$map["Stan's"] = array( "Stan's", "Stans" ); | |
$map["Sterling"] = array( "Sterling" ); | |
$map["Sunice"] = array( "Sun Ice" ); | |
$map["Tasc"] = array( "Tasc Performance" ); | |
$map["Terra Nova"] = array( "Terra Nova" ); | |
$map["Teva"] = array( "Teva" ); | |
$map["The North Face"] = array( | |
"North Face", | |
"The North Face Inc", | |
"The North Face", | |
"The NorthFace", | |
"NorthFace" | |
); | |
$map["Therm-a-Rest"] = array( "Thermarest", "Therma rest", "Therm A Rest" ); | |
$map["ThirtyTwo"] = array( "Thirty Two" ); | |
$map["Thule"] = array( "Thule" ); | |
$map["Timberland"] = array( "Timberland" ); | |
$map["Timbuk2"] = array( "Timbuk 2" ); | |
$map["Toad&Co"] = array( "Horny Toad", "HornyToad", "Toad & Co", "Toad&Co", "Toad & Co" ); | |
$map["TYR"] = array( "Tyr-Swimwear", "Tyr" ); | |
$map["Union"] = array( "Union Bindings" ); | |
$map["Von Zipper"] = array( "Vonzipper" ); | |
$map["Wild Country"] = array( | |
"Wildcountry", | |
"Wild country", | |
"Wildcountry climbing", | |
"Wild country climbing" | |
); | |
$map["Woolrich"] = array( "Woolrich", "Woolrich, Inc" ); | |
$map["Yakima"] = array( "Yakima" ); | |
// ++++++++++ Stop Editing Here ++++++++++ | |
$terms = array(); | |
foreach ( $map as $key => $keywords ) { | |
if ( preg_match( '/\b' . preg_quote( $key, '/' ) . '\b/i', $field ) ) { | |
$terms[] = $key; | |
} | |
foreach ( $keywords as $keyword ) { | |
if ( preg_match( '/\b' . preg_quote( $keyword, '/' ) . '\b/i', $field ) ) { | |
$terms[] = $key; | |
} | |
} | |
} | |
if ( ! empty( $terms ) ) { | |
return implode( WC_DELIMITER, array_unique( $terms ) ); | |
} | |
return $default; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment