Skip to content

Instantly share code, notes, and snippets.

@racmanuel
Last active May 4, 2021 21:47
Show Gist options
  • Save racmanuel/bdd54ba15299ec9ea287f98b7786a44f to your computer and use it in GitHub Desktop.
Save racmanuel/bdd54ba15299ec9ea287f98b7786a44f to your computer and use it in GitHub Desktop.
Custom Loop for Woocommerce Shop only show the products of the author of the store of WCVendors
<?php
/**
* Custom Loop for Woocommerce Shop only show the products of the author of the store of WCVendors
*
* The Template for displaying product archives, including the main shop page which is a post type archive
*
* This template can be overridden by copying it to yourtheme/woocommerce/archive-product.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.4.0
* @author Manuel Ramírez Coronel
* @see https://github.com/racmanuel
*/
defined('ABSPATH') || exit;
get_header('shop');
/**
* Hook: woocommerce_before_main_content.
*
* @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* @hooked woocommerce_breadcrumb - 20
* @hooked WC_Structured_Data::generate_website_data() - 30
*/
do_action('woocommerce_before_main_content');
?>
<header class="woocommerce-products-header">
<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
<h1 class="woocommerce-products-header__title page-title"><?php woocommerce_page_title(); ?></h1>
<?php endif; ?>
<?php
/**
* Hook: woocommerce_archive_description.
*
* @hooked woocommerce_taxonomy_archive_description - 10
* @hooked woocommerce_product_archive_description - 10
*/
do_action( 'woocommerce_archive_description' );
?>
</header>
<div class="products-container container p-0">
<?php
if( ! function_exists('wc_get_products') ) return;
// definimos las variables de paginación y filtro
$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
$ordering = WC()->query->get_catalog_ordering_args();
$ordering['orderby'] = array_shift(explode(' ', $ordering['orderby']));
$ordering['orderby'] = stristr($ordering['orderby'], 'price') ? 'meta_value_num' : $ordering['orderby'];
$products_per_page = apply_filters('loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page());
$vendor_shop = urldecode(get_query_var('vendor_shop'));
$vendor_id = WCV_Vendors::get_vendor_id($vendor_shop);
$store_url = WCV_Vendors::get_vendor_shop_page($vendor_id);
// Construimos la consulta usando diferentes argumentos, sólo necesitaremos los Ids de los productos
$custom_products = wc_get_products(array(
'status' => 'publish',
'visibility' => 'visible',
'limit' => $products_per_page,
'page' => $paged,
'paginate' => true,
'return' => 'ids',
'orderby' => $ordering['orderby'],
'order' => $ordering['order'],
'author' => $vendor_id
));
// Establecemos las propiedades globales para el bucle
wc_set_loop_prop('current_page', $paged);
wc_set_loop_prop('is_paginated', true);
wc_set_loop_prop('page_template', get_page_template_slug());
wc_set_loop_prop('per_page', $products_per_page);
wc_set_loop_prop('total', $custom_products->total);
wc_set_loop_prop('total_pages', $custom_products->max_num_pages);
// Construcción del bucle de WooCommerce teniendo en cuenta los hooks
if($custom_products) {
/**
* Hook: woocommerce_before_shop_loop.
*
* @hooked woocommerce_output_all_notices - 10
* @hooked woocommerce_result_count - 20
* @hooked woocommerce_catalog_ordering - 30
*/
do_action( 'woocommerce_before_shop_loop' );
woocommerce_product_loop_start();
// Recorremos todos los Ids obtenidos
foreach($custom_products->products as $item) {
$post_object = get_post($item);
// Establecemos la variable global al post actual
setup_postdata($GLOBALS['post'] =& $post_object);
// Mostramos el producto usando la plantilla por defecto de WC
wc_get_template_part('content', 'product-vendors');
}
wp_reset_postdata();
woocommerce_product_loop_end();
/**
* Hook: woocommerce_after_shop_loop.
*
* @hooked woocommerce_pagination - 10
*/
do_action( 'woocommerce_after_shop_loop' );
} else {
/**
* Hook: woocommerce_no_products_found.
*
* @hooked wc_no_products_found - 10
*/
do_action( 'woocommerce_no_products_found' );
}
/**
* Hook: woocommerce_after_main_content.
*
* @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
get_footer('shop');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment