$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>woocommerce offtopic - How do I display certain products via their category on a section of a page using PHP?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

woocommerce offtopic - How do I display certain products via their category on a section of a page using PHP?

matteradmin8PV0评论

I need to display New Arrivals in a section of my homepage, I have created a new arrivals category and would like to display the products in this category on a page, I don't want to use the shortcode as I am writing the html & php for this page.

Any help would be appreciated, thanks.

I need to display New Arrivals in a section of my homepage, I have created a new arrivals category and would like to display the products in this category on a page, I don't want to use the shortcode as I am writing the html & php for this page.

Any help would be appreciated, thanks.

Share Improve this question asked Jun 13, 2018 at 14:52 Luke - ValkyriLuke - Valkyri 331 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 7

you can still use a shortcode inside of php if you aren't customizing the output. see https://docs.woocommerce/document/woocommerce-shortcodes/#scenario-5-specific-categories and https://developer.wordpress/reference/functions/do_shortcode/

<?php
echo do_shortcode('[products category="new-arrivals"]');
?> 

Alternatively, If you need to customize the output of the products then just use wc_get_products to get a list of products and iterate through it. See https://github/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query

<?php
$args = array(
    'category' => array( 'new-arrivals' ),
);
$products = wc_get_products( $args );
foreach ($products as $product) {
    echo $product->get_title() . ' ' . $product->get_price();
}
?>

Assuming woocommerce plugin is in place:

 if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) 
     {
      $args = array('category' => array( 'Your category name' ), );
      $products = wc_get_products( $args );
      foreach ($products as $product) 
        {
         DebugLog('Title: '. $product->get_name() . ' Price:  ' . $product->get_price()) ;
        }
     }

$product->get_title(), does not always seem to display the product name.

Post a comment

comment list (0)

  1. No comments so far