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

admin2025-06-02  1

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748804550a313859.html

最新回复(0)