Skip to content

Instantly share code, notes, and snippets.

@campusboy87
Created May 30, 2017 17:28
Show Gist options
  • Save campusboy87/6b9dc8f15561f4298dd27041d8b09594 to your computer and use it in GitHub Desktop.
Save campusboy87/6b9dc8f15561f4298dd27041d8b09594 to your computer and use it in GitHub Desktop.
Выводит товары из указанной рубрики без подгрузки товаров из подрубрик
<?php
/*
Plugin Name: Product Category Without Child
Description: Выводит товары из указанной рубрики без подгрузки товаров из подрубрик
Version: 1.0.0
Author: Campusboy
Author URI: https://wp-plus.ru/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
// При загрузке woocommerce движком, запускаем наш код
add_action( 'woocommerce_loaded', 'init_My_WC_Shortcodes' );
// Функция создания класса по формированию нового шоткода
function init_My_WC_Shortcodes(){
class My_WC_Shortcodes extends WC_Shortcodes{
public function __construct() {
add_shortcode( 'product_category_wo_child', array( 'My_WC_Shortcodes', 'product_category_wo_child' ) );
}
public static function product_category_wo_child( $atts ) {
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'menu_order title',
'order' => 'asc',
'category' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts, 'product_category' );
if ( ! $atts['category'] ) {
return '';
}
// Default ordering args
$ordering_args = WC()->query->get_catalog_ordering_args( $atts['orderby'], $atts['order'] );
$meta_query = WC()->query->get_meta_query();
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => $atts['per_page'],
'meta_query' => $meta_query
);
$query_args = self::_maybe_add_category_args( $query_args, $atts['category'], $atts['operator'] );
if ( isset( $ordering_args['meta_key'] ) ) {
$query_args['meta_key'] = $ordering_args['meta_key'];
}
$return = self::product_loop( $query_args, $atts, 'product_cat' );
// Remove ordering query arguments
WC()->query->remove_ordering_args();
return $return;
}
/**
* Adds a tax_query index to the query to filter by category.
*
* @param array $args
* @param string $category
* @param string $operator
* @return array;
* @access private
*/
private static function _maybe_add_category_args( $args, $category, $operator ) {
if ( ! empty( $category ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'terms' => array_map( 'sanitize_title', explode( ',', $category ) ),
'field' => 'slug',
'include_children' => false,
'operator' => $operator
)
);
}
return $args;
}
/**
* Loop over found products.
* @param array $query_args
* @param array $atts
* @param string $loop_name
* @return string
*/
private static function product_loop( $query_args, $atts, $loop_name ) {
global $woocommerce_loop;
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $query_args, $atts, $loop_name ) );
$columns = absint( $atts['columns'] );
$woocommerce_loop['columns'] = $columns;
$woocommerce_loop['name'] = $loop_name;
ob_start();
if ( $products->have_posts() ) {
?>
<?php do_action( "woocommerce_shortcode_before_{$loop_name}_loop" ); ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php do_action( "woocommerce_shortcode_after_{$loop_name}_loop" ); ?>
<?php
} else {
do_action( "woocommerce_shortcode_{$loop_name}_loop_no_results" );
}
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
}
new My_WC_Shortcodes();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment