Skip to content

Instantly share code, notes, and snippets.

@artikus11
Last active July 16, 2021 21:49
Show Gist options
  • Save artikus11/ffb0aa638ba3def4df003c1a2f660758 to your computer and use it in GitHub Desktop.
Save artikus11/ffb0aa638ba3def4df003c1a2f660758 to your computer and use it in GitHub Desktop.
<?php
/**
* Collection Products
*
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/related.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 3.9.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( $collection_products ) :
?>
<section class="collection products">
<?php
$heading = 'Товары из коллекции ' . $collection_name;
if ( $heading ) :
?>
<h2><?php echo esc_html( $heading ); ?></h2>
<?php endif; ?>
<?php woocommerce_product_loop_start(); ?>
<?php foreach ( $collection_products as $collection_product ) : ?>
<?php
$post_object = get_post( $collection_product->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, Squiz.PHP.DisallowMultipleAssignments.Found
wc_get_template_part( 'content', 'product' );
?>
<?php endforeach; ?>
<?php woocommerce_product_loop_end(); ?>
</section>
<?php
endif;
wp_reset_postdata();
/**
* Вывод товарв по аттрибуту (здесь: аттрибут Коллекция)
*
* @testedwith WooCommerce 5.5
* @author Artem Abramovich
*/
public function woocommerce_collection_products(): void {
global $product;
if ( ! $product ) {
return;
}
$args = [
'attribute' => 'kollekcziya',
'values' => '',
'posts_per_page' => 6,
'columns' => 6,
'orderby' => 'title',
'order' => 'desc',
];
$collections = $this->get_attribute_slug( $product, $args['attribute'] );
if ( ! $collections ) {
return;
}
$ordering_args = WC()->query->get_catalog_ordering_args( $args['orderby'], $args['order'] );
$args_query = [
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => $args['posts_per_page'],
'fields' => 'ids',
'tax_query' => [
'relation' => 'OR',
[
'taxonomy' => 'pa_' . $args['attribute'],
'terms' => $collections,
'field' => 'slug',
'operator' => 'IN',
],
],
];
$cache_key = sprintf( 'collection_by_product_%s', $product->get_id() );
$cache_group = sprintf( '%s_%s', $args['attribute'], implode( '_', (array) $collections ) );
$found = false;
$products = wp_cache_get( $cache_key, $cache_group, $found );
if ( false === $found ) {
$products = get_posts( $args_query );
wp_cache_set( $cache_key, $products, $cache_group, 10 );
}
$args['collection_name'] = $product->get_attribute( $args['attribute'] );
$args['collection_products'] = array_filter( array_map( 'wc_get_product', $products ), 'wc_products_array_filter_visible' );
// Set global loop values.
wc_set_loop_prop( 'name', 'collection' );
wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_related_products_columns', $args['columns'] ) );
wc_get_template( 'single-product/collection.php', $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment