C
C
caof192020-01-19 21:35:58
WordPress
caof19, 2020-01-19 21:35:58

How to display Woocomerce bestsellers?

Hello, I'm studying WooComerce, I found the layout of an online store, there is a block "News", "Bestsellers". I want to make it so that in the admin panel you can specify which products to display in this block, plus for each of these products in the corner on the card it should be displayed that it is, say, "New".
Having spread my brains a little, I found two options, they do not differ much from each other. Add a custom field using Woocomerce hooks or other plugins (like Carbon, ACF, CFS, etc.).
Added a field using Woo like this:

add_action( 'woocommerce_product_options_general_product_data', 'art_woo_add_custom_fields' );
function art_woo_add_custom_fields() {
  global $product, $post;
  echo '<div class="options_group">';// Группировка полей
    woocommerce_wp_checkbox( array(
      'id'            => '_checkbox',
      'wrapper_class' => 'show_if_simple',
      'label'         => 'Чекбокс',
      'description'   => 'Новинка',
    ) );
    woocommerce_wp_text_input( array(
      'id'                => '_number_field',
      'label'             => __( 'Порядковый номер', 'woocommerce' ),
      'placeholder'       => 'Порядковый номер на главной',
      'description'       => __( 'Вводятся только числа', 'woocommerce' ),
      'type'              => 'number',
      'custom_attributes' => array(
        'step' => '1',
        'min'  => '0',
      ),
    ) );
  echo '</div>';
}

And I tried to display on the main:
<?php
    $loop = new WP_Query( array(
        'post_type' => 'product', 
        'posts_per_page' => 4,
        'orderby' => 'date',
    ));
?>

<?php if ($loop->have_posts()) : ?>
  <?php while ($loop->have_posts()) :
    $loop->the_post();
    $product = wc_get_product();
    dump($product);
    dump($product->get_meta( '_checkbox', true ));?>
    <?php endwhile; endif;?>

But nothing has happened yet.
The question is: how could this be implemented more correctly or how can I improve my version? Do not beat with sticks, for the first experience.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question