Home > Net >  How to get products data from woocommerce and place it in a custom theme?
How to get products data from woocommerce and place it in a custom theme?

Time:12-02

As I searched and read in order to extract the product data completely I have to use Json and I have no idea how to do that any ideas? i have this code it's not working

<?php
 
add_action( 'woocommerce_before_cart', 'bbloomer_echo_product_ids' );
 
function bbloomer_echo_product_ids() {
   $all_ids = get_posts( array(
        'post_type' => 'product',
        'numberposts' => -1,
        'post_status' => 'publish',
        'fields' => 'ids',
   ) );
   foreach ( $all_ids as $id ) {
        echo $id;
   }
}
?>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Try this...

$args = array(
    'post_type'      => 'product',
    'posts_per_page' => 10,
    'product_cat'    => 'hoodies'
);

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();
    global $product;
    echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
endwhile;

wp_reset_query();

there is a nice documentation to get all datas from products here https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/

CodePudding user response:

try to add this with the wp 'init' action, just to display it on every page. The hook you are using just display that at the top of your cart. Depending your theme, the hook may not used.

  • Related