Кастомний шорткод woocommerce

Блог Фрилансера 1 хвилин 5 секунд 9 лип. 2019
 Кастомний шорткод woocommerce

 Woocommerce — відмінний плагін для інтернет-магазину. Але й у ньому є дрібні недоліки. Але вони легко усуваються руками)

Зробимо свій шорткод для відбору товарів за кастомними полями Advanced Custom Fields.

Наприклад, зробимо поле «що товар»  особливий.

 

Приклад шорткоду 

[hit_category_product per_page="4" columns="4" orderby="date" order="desc"]

Функцію додати до functions.php

add_shortcode( 'hit_category_product', 'featured_category_product_function' );
function featured_category_product_function( $atts ) {

    global $woocommerce_loop;

    extract(shortcode_atts(array(
        'category' => '',
        'per_page' => '4',
        'columns' => '4',
        'orderby' => 'date',
        'order' => 'desc'
    ), $ Atts));

     $ args = array (
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts' => 1,
        'posts_per_page' => $per_page,
        'orderby' => 'meta_value_num',
        'order' => $order,
        'meta_query' => array(
            array(
                'key' => 'hit_product', // hit_product – ім'я кастомного поля
                'value' => true,
                'compare' => 'IN'
            )
        )
    );

    ob_start();

    $products = new WP_Query( $args );

    $woocommerce_loop['columns'] = $columns;

    if ( $products->have_posts() ) : ?>

        

            have_posts() ) : $products->the_post(); ?>

                

            

        

    ' . ob_get_clean(). '
'; }

Якщо потрібно відбирати два поля 

$args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts' => 1,
        'posts_per_page' => $per_page,
        'orderby' => 'meta_value_num',
        'order' => $order,
        'meta_query' => array(
            'relation' => 'OR', // вказуємо що потрібно шукати або перше чи інше
            array(
                'key' => 'hit_product', // перше кастомне поле
                'value' => true,
                'compare' => 'IN'
            ),array(
                'key' => 'Selling',// друге кастомне поле
                'value' => true,
                'compare' => 'IN'
            )
        )
    );