Created
August 14, 2018 16:05
-
-
Save hereswhatidid/ab6c77f880df2d6b95eeea731727136b to your computer and use it in GitHub Desktop.
Add WooCommerce product variations to the WordPress Link Chooser
This file contains 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 | |
namespace HWID\Woo; | |
class LinkChooser { | |
static function init() { | |
add_filter( 'wp_link_query_args', [ 'HWID\Woo\LinkChooser', 'add_variations_link' ] ); | |
add_filter( 'wp_link_query', [ 'HWID\Woo\LinkChooser', 'variations_labels' ], 10, 2 ); | |
} | |
/** | |
* Add the `product_variation` post type to the default link chooser | |
* | |
* @param array $args | |
* | |
* @return array mixed | |
*/ | |
static function add_variations_link( $args ) { | |
$post_types = $args['post_type']; | |
$post_types[] = 'product_variation'; | |
$args['post_type'] = $post_types; | |
return $args; | |
} | |
/** | |
* Add the `Variation` label to the found link results. | |
* | |
* Because the `product_variation` post type | |
* is not public it does not properly display the post type label so we have to add it ourselves. | |
* | |
* @param array $results | |
* @param $query | |
* | |
* @return array | |
*/ | |
static function variations_labels( $results, $query ) { | |
foreach( $results as &$result ) { | |
if ( $result['info'] === null ) { | |
$result['info'] = 'Variation'; | |
} | |
} | |
return $results; | |
} | |
} | |
LinkChooser::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment