| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `posts` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
function gutenify_render_wc_product_carousel_block( $attributes, $content, $props ) { |
| 9 |
if ( ! class_exists( 'woocommerce' ) ) { |
| 10 |
return ''; |
| 11 |
} |
| 12 |
$query = $attributes['query']; |
| 13 |
$args = array( |
| 14 |
'post_status' => 'publish', |
| 15 |
'post_type' => 'product', |
| 16 |
); |
| 17 |
|
| 18 |
if ( ! empty( $query['numberOfItems'] ) ) { |
| 19 |
$args['posts_per_page'] = $query['numberOfItems']; |
| 20 |
} |
| 21 |
if ( ! empty( $query['orderBy'] ) ) { |
| 22 |
$args['orderby'] = $query['orderBy']; |
| 23 |
} |
| 24 |
if ( ! empty( $query['order'] ) ) { |
| 25 |
$args['order'] = strtoupper( $query['order'] ); |
| 26 |
} |
| 27 |
|
| 28 |
if ( ! empty( $query['tax']['product_cat'] ) ) { |
| 29 |
$args['tax_query'][] = array( |
| 30 |
'taxonomy' => 'product_cat', |
| 31 |
'field' => 'id', |
| 32 |
'terms' => $query['tax']['product_cat'], |
| 33 |
); |
| 34 |
} |
| 35 |
if ( ! empty( $query['tax']['product_tag'] ) ) { |
| 36 |
$args['tax_query'][] = array( |
| 37 |
'taxonomy' => 'product_tag', |
| 38 |
'field' => 'id', |
| 39 |
'terms' => $query['tax']['product_tag'], |
| 40 |
); |
| 41 |
} |
| 42 |
$posts = new WP_Query( $args ); |
| 43 |
|
| 44 |
ob_start(); |
| 45 |
if ( $posts->have_posts() ) { |
| 46 |
$wrapper_class = array( |
| 47 |
'gutenify-section-' . $attributes['blockClientId'], |
| 48 |
'wp-block-gutenify-wc-product-carousel', |
| 49 |
'gutenify--wc-product-carousel-' . $attributes['layout'], |
| 50 |
'gutenify--wc-product-carousel-col-' . $attributes['columns'], |
| 51 |
); |
| 52 |
if ( isset( $attributes['className'] ) ) { |
| 53 |
array_push( $wrapper_class, $attributes['className'] ); |
| 54 |
} |
| 55 |
$wrapper_class = apply_filters( 'gutenify--slider--wrapper-class', $wrapper_class, $props ); |
| 56 |
echo sprintf( '<div class="%s">', implode( ' ', $wrapper_class ) ); |
| 57 |
echo '<div class="swiper-container">'; |
| 58 |
echo '<div class="swiper-wrapper">'; |
| 59 |
while ( $posts->have_posts() ) { |
| 60 |
$posts->the_post(); |
| 61 |
$product = get_product( get_the_ID() ); |
| 62 |
$permalink = get_the_permalink( $product->get_id() ); |
| 63 |
echo '<div class="gutenify--wc-product--item has-no-hover-shadow-dark swiper-slide">'; |
| 64 |
echo '<div class="gutenify--wc-product--item-wrapper">'; |
| 65 |
|
| 66 |
echo '<div class="gutenify--wc-product--thumb">'; |
| 67 |
echo '<a class="image-hover-zoom" href="' . $permalink . '" tabindex="-1">'; |
| 68 |
echo woocommerce_get_product_thumbnail(); |
| 69 |
echo '</a>'; // Product thumb link |
| 70 |
echo $product->is_on_sale() ? '<div class="gutenify--wc-product--onsale"><span aria-hidden="true">Sale</span><span class="screen-reader-text">Product on sale</span></div>' : ''; |
| 71 |
echo '</div>'; // Product thumb |
| 72 |
|
| 73 |
|
| 74 |
echo '<div class="gutenify--wc-product--item-content">'; |
| 75 |
echo '<h3 class="gutenify--wc-product--title">'; |
| 76 |
echo '<a rel="bookmark" href="' . $permalink . '" tabindex="-1">'; |
| 77 |
echo $product->get_name(); |
| 78 |
echo '</a>'; // Product title link |
| 79 |
echo '</h3>'; // Product title |
| 80 |
|
| 81 |
echo '<div class="gutenify--wc-product--price">'; |
| 82 |
echo $product->get_price_html(); |
| 83 |
echo '</div>'; // Product price |
| 84 |
|
| 85 |
echo '<div class="wp-block-button wc-block-grid__product-add-to-cart">'; |
| 86 |
echo gutenify_wc_get_add_to_cart( $product ); |
| 87 |
// var_dump( $product ); |
| 88 |
echo '</div>'; // Product add to cart wrapper |
| 89 |
echo '</div>'; // .gutenify--wc-product--item-content |
| 90 |
|
| 91 |
echo '</div>'; // Product individual wrapper |
| 92 |
echo '</div>'; // Product individual wrapper |
| 93 |
} |
| 94 |
|
| 95 |
echo '</div>'; // .swiper-wrapper |
| 96 |
$has_navigation = ! empty( $attributes['hasNavigation'] ) && true === $attributes['hasNavigation']; |
| 97 |
$has_pagination = ! empty( $attributes['hasPagination'] ) && true === $attributes['hasPagination']; |
| 98 |
if ( $has_navigation ) { |
| 99 |
echo '<div class="swiper-button-next"></div>'; |
| 100 |
echo '<div class="swiper-button-prev"></div>'; |
| 101 |
} |
| 102 |
if ( $has_pagination ) { |
| 103 |
echo '<div class="swiper-pagination"></div>'; |
| 104 |
} |
| 105 |
echo '</div>'; // .swiper-container |
| 106 |
echo '</div>'; // Product carousels wrapper |
| 107 |
wp_reset_postdata(); |
| 108 |
} |
| 109 |
wp_enqueue_script( 'gutenify-swiper' ); |
| 110 |
|
| 111 |
$columns = ! empty( $attributes['columns'] ) ? $attributes['columns'] : 1; |
| 112 |
$space_between = ! empty( $attributes['spaceBetween'] ) ? $attributes['spaceBetween'] : 0; |
| 113 |
$swiper_config = array( |
| 114 |
'loop' => 'true', |
| 115 |
'slidesPerView' => $columns, |
| 116 |
'breakpoints' => array( |
| 117 |
'320' => array( |
| 118 |
'slidesPerView' => 1, |
| 119 |
'spaceBetween' => 20, |
| 120 |
), |
| 121 |
// when window width is >= 640px |
| 122 |
'640' => array( |
| 123 |
'slidesPerView' => 2, |
| 124 |
'spaceBetween' => 15, |
| 125 |
), |
| 126 |
'1024' => array( |
| 127 |
'slidesPerView' => $columns, |
| 128 |
'spaceBetween' => $space_between, |
| 129 |
), |
| 130 |
), |
| 131 |
'spaceBetween' => $space_between, |
| 132 |
'scrollbar' => array( 'draggable' => true ), |
| 133 |
); |
| 134 |
|
| 135 |
if ( $has_navigation ) { |
| 136 |
$swiper_config['navigation'] = array( |
| 137 |
'nextEl' => '.swiper-button-next', |
| 138 |
'prevEl' => '.swiper-button-prev', |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
if ( $has_pagination ) { |
| 143 |
$swiper_config['pagination'] = array( |
| 144 |
'el' => '.swiper-pagination', |
| 145 |
'clickable' => true, |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
$block_client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : 1; |
| 150 |
$slider_var = 'slider_' . str_replace( '-', '_', $block_client_id ); |
| 151 |
$slider_function_name = 'reinitSlider_' . $slider_var; |
| 152 |
?> |
| 153 |
<script> |
| 154 |
jQuery(function($){ |
| 155 |
var sliderConfig = <?php echo json_encode( $swiper_config ); ?>; |
| 156 |
console.log( sliderConfig ); |
| 157 |
function <?php echo $slider_function_name; ?>() { |
| 158 |
var swiperWrapper = $(".gutenify-section-<?php echo $block_client_id; ?> .swiper-wrapper"); |
| 159 |
var newSlides = swiperWrapper.children(".swiper-slide").clone(true); |
| 160 |
' . $slider_var . '.destroy(); |
| 161 |
swiperWrapper.empty().append(newSlides); |
| 162 |
swiperWrapper.attr("style", ""); |
| 163 |
mySwiper = new Swiper(".gutenify-section-<?php echo $block_client_id; ?> .swiper-container", sliderConfig); |
| 164 |
} |
| 165 |
|
| 166 |
var <?php echo $slider_var; ?> = new Swiper(".gutenify-section-<?php echo $block_client_id; ?> .swiper-container", sliderConfig); |
| 167 |
$(window).on("resize", function(){ |
| 168 |
' . $slider_function_name . '(); |
| 169 |
}); |
| 170 |
$(document).on("containerStretched", function(){ |
| 171 |
' . $slider_function_name . '(); |
| 172 |
}); |
| 173 |
}); |
| 174 |
</script> |
| 175 |
<?php |
| 176 |
$result = ob_get_clean(); |
| 177 |
return $result; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Registers the `posts` block on server. |
| 182 |
*/ |
| 183 |
function gutenify_register_wc_product_carousel_block() { |
| 184 |
// Return early if this function does not exist. |
| 185 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
// Load attributes from block.json. |
| 190 |
ob_start(); |
| 191 |
include GUTENIFY_PLUGIN_DIR . 'src/blocks/wc-product-carousel/block.json'; |
| 192 |
$metadata = json_decode( ob_get_clean(), true ); |
| 193 |
|
| 194 |
register_block_type( |
| 195 |
'gutenify/wc-product-carousel', |
| 196 |
array( |
| 197 |
'attributes' => $metadata['attributes'], |
| 198 |
'render_callback' => 'gutenify_render_wc_product_carousel_block', |
| 199 |
) |
| 200 |
); |
| 201 |
} |
| 202 |
add_action( 'init', 'gutenify_register_wc_product_carousel_block' ); |
| 203 |
|