Last active
February 7, 2021 10:31
-
-
Save Prroffessorr/785d6e60d72f69d2132c9629def17829 to your computer and use it in GitHub Desktop.
WooCommerce ( Product Handling Cheat Sheet ) ( content-single-product.php )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Картинки из стандартной галереи товара | |
$meta = explode(',',get_post_meta( $post->ID,'_product_image_gallery')[0]); | |
// Добавляем главную картинку товара (Для вывода ее в галерее товара) | |
$product_galegy[] = array( | |
'thumb' =>wp_get_attachment_image_src(get_post_meta( $post->ID,'_thumbnail_id')[0],'slide_product_thumb')[0], | |
'full' => wp_get_attachment_image_src(get_post_meta( $post->ID,'_thumbnail_id')[0],'slide_product_full')[0], | |
'type'=> 'photo', | |
'url'=> '' | |
); | |
foreach ($meta as $key ) { | |
// Добавляем картинки из галереи | |
$product_galegy[] = array( | |
'thumb' => wp_get_attachment_image_src($key,'slide_product_thumb')[0], | |
'full' => wp_get_attachment_image_src($key,'slide_product_full')[0], | |
'type'=> 'photo' | |
); | |
} | |
//Код верстки, который уже настраивается под конкретный сайт ($post-Глобальная переменная, в которой хранится информация о товаре) | |
<section> | |
<div> | |
<?php //Картинка товара | |
do_action( 'woocommerce_before_shop_loop_item_title' );?> | |
//Или можно воспользоваться таким методом (Если нужна более точная насткройка отображения) | |
<img src="<?php echo wp_get_attachment_image_src(get_post_meta( $post->ID,'_thumbnail_id')[0],'slide_product_thumb')[0]; ?>" alt=""> | |
</div> | |
//Вывод в цикле всех изображений товара (Изображение и Галерея товара ) | |
<ul> | |
<?php //Вывод галереи товара на странице | |
foreach ($product_galegy as $thumbnail ): | |
if ( $thumbnail['type'] == 'photo'): ?> | |
<li> | |
<?php $image_attributes = wp_get_attachment_image_src( $post->ID ); ?> | |
<a href="<?php echo esc_url( $thumbnail['full'] ); ?>" > | |
<img src="<?php echo $thumbnail['full'] ?>" alt=""> | |
</a> | |
</li> | |
<?php | |
endif; | |
endforeach; ?> | |
</ul> | |
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Код верстки, который уже настраивается под конкретный сайт ($post-Глобальная переменная, в которой хранится информация о товаре) | |
<section> | |
<h1> | |
<?php //Отображение заглавия товара | |
echo get_the_title($post->ID); ?> | |
</h1> | |
<?php //Получаем и отображаем имя категории товара | |
$terms_post = get_the_terms( $post->ID , 'product_cat' ); | |
foreach ($terms_post as $term_cat): ?> | |
<p> Категория: <?php echo $term_cat->name; ?> </p> | |
<?php endforeach; ?> | |
<?php //Получение и вывод артикула товара | |
$sku = wc_get_product($post->ID ); ?> | |
<p> Артикул: <?php echo $sku->get_sku(); ?> </p> | |
<p> | |
<?php //Вывод описания товара | |
echo $post->post_content; ?> | |
</p> | |
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $product; | |
//Категорию товаров, по которой будем отображать | |
$product_category = array(); | |
foreach (get_the_terms($product->ID, 'product_cat') as $term) { | |
array_push($product_category, $term->slug); | |
} | |
//Получение списка параметров необходимых для вывода товаров | |
$args = array( | |
'posts_per_page' => 16, | |
'tax_query' => array( | |
'relation' => 'OR', | |
array( | |
'taxonomy' => 'product_cat', | |
'field' => 'slug', | |
'terms' => $product_category | |
), | |
), | |
'post_type' => 'product', | |
'orderby' => 'title', | |
); | |
//Код верстки, который уже настраивается под конкретный сайт | |
<section> | |
<ul> | |
<?php //Выбираем записи из базы данных по указанным ранее (в масиве $args ) критериям | |
$query = new WP_Query( $args ); ?> | |
<?php //Цикл вывода товаров | |
while ( $query->have_posts() ): | |
$query->the_post(); ?> | |
<li> | |
//Вывод изображения товара со ссылкой на него | |
<a href="<?php the_permalink(); ?>"> | |
<figure> | |
<img src="<?php echo the_post_thumbnail_url($the_query->ID); ?>" alt="<?php the_title(); ?>"> | |
<figcaption> | |
<span title="<?php the_title(); ?>"><?php the_title(); ?></span> | |
</figcaption> | |
</figure> | |
</a> | |
<footer> | |
<?php //Получаем и отображем имя категории товара | |
$terms_post = get_the_terms( $query->ID , 'product_cat' ); | |
foreach ($terms_post as $term_cat): ?> | |
<div>Категория: <?php echo $term_cat->name; ?></div> | |
<?php | |
endforeach; ?> | |
<?php //Получение артикула товара | |
echo do_action("woocommerce_after_shop_loop_item_title"); ?> | |
</footer> | |
</li> | |
<?php | |
endwhile; | |
wp_reset_postdata(); | |
?> | |
</ul> | |
</section> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //Получение артикула товара | |
add_action( 'woocommerce_after_shop_loop_item_title', 'get_sku_product' ); | |
function get_sku_product(){ | |
global $product; | |
echo ' <div>Артикул:'.$product->sku.' </div>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment