Products::render( $attr,  $instance )

Summary

No summary available.

Source

File: src/BigCommerce/Shortcodes/Products.php

	public function render( $attr, $instance ) {
		$attr = shortcode_atts( self::default_attributes(), $attr, self::NAME );

		$mapper     = new Query_Mapper();
		$query_args = $mapper->map_shortcode_args_to_query( $attr );

		$current_post = get_the_ID();
		if ( $current_post ) {
			$query_args[ 'post__not_in' ] = [ $current_post ];
		}

		$query   = new \WP_Query( $query_args );
		$results = $query->posts;

		$products = array_map( function ( $post_id ) {
			return new Product( $post_id );
		}, $results );

		$count = count( $products );
		if ( $count < 1 ) {
			return ''; // TODO: something nicer?
		}

		if ( count( $products ) > 1 || $attr[ 'paged' ] > 1 ) {
			$cards = array_map( function ( Product $product ) use ( $attr ) {
				if ( empty( $attr[ 'preview' ] ) ) {
					$card = Product_Card::factory( [
						Product_Card::PRODUCT => $product,
					] );
				} else {
					$card = Product_Card_Preview::factory( [
						Product_Card::PRODUCT => $product,
					] );
				}

				return $card->render();
			}, $products );
			$grid  = Product_Shortcode_Grid::factory( [
				Product_Shortcode_Grid::CARDS         => $cards,
				Product_Shortcode_Grid::NEXT_PAGE_URL => $this->next_page_url( $attr, $query->max_num_pages ),
				Product_Shortcode_Grid::WRAP          => intval( $attr[ 'ajax' ] ) !== 1,
			] );

			return $grid->render();
		} else {
			if ( empty( $attr[ 'preview' ] ) ) {
				$single = Product_Shortcode_Single::factory( [
					Product_Shortcode_Single::PRODUCT => reset( $products ),
				] );
			} else {
				$single = Product_Shortcode_Single_Preview::factory( [
					Product_Shortcode_Single::PRODUCT => reset( $products ),
				] );
			}

			return $single->render();
		}
	}


User Contributed Notes

You must log in before being able to contribute a note or feedback.