WooCommerce return product object by id

前端 未结 4 1446
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 18:39

I am creating a custom theme for woocommerce and I need to be able to create a mini product display. I am having problems finding documentation on the woocommerce api. I hav

相关标签:
4条回答
  • 2020-12-29 18:53
    global $woocommerce;
    var_dump($woocommerce->customer->get_country());
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = new WC_product($cart_item['product_id']);
        var_dump($product);
    }
    
    0 讨论(0)
  • 2020-12-29 18:55

    Use this method:

    $_product = wc_get_product( $id );
    

    Official API-docs: wc_get_product

    0 讨论(0)
  • 2020-12-29 18:56

    Alright, I deserve to be throttled. definitely an RTM but not for WooCommerce, for Wordpress. Solution found due to a JOLT cola (all hail JOLT cola).

    TASK: Field named 'related_product_ids' added to a custom post type. So when that post is displayed mini product displays can be displayed with it.

    PROBLEM: Was having a problem getting the multiple ids returned via WP_Query.

    SOLUTION:

    $related_id_list          = get_post_custom_values('related_product_ids');
        // Get comma delimited list from current post
    $related_product_ids      = explode(",", trim($related_id_list[0],','));
        // Return an array of the IDs ensure no empty array elements from extra commas
    $related_product_post_ids = array( 'post_type' => 'product', 
                                       'post__in'  => $related_product_ids,
                                       'meta_query'=> array( 
                                            array( 'key'    => '_visibility',
                                                   'value'  => array('catalog', 'visible'),'compare' => 'IN'
                                            )
                                ) 
    );      
        // Query to get all product posts matching given IDs provided it is a published post
    $loop = new WP_Query( $related_posts );
        // Execute query
    while ( $loop->have_posts() ) : $loop->the_post(); $_product = get_product( $loop->post->ID );
        // Do stuff here to display your products 
    endwhile;
    

    Thank you for anyone who may have spent some time on this.

    Tim

    0 讨论(0)
  • 2020-12-29 19:01

    Another easy way is to use the WC_Product_Factory class and then call function get_product(ID)

    http://docs.woothemes.com/wc-apidocs/source-class-WC_Product_Factory.html#16-63

    sample:

    // assuming the list of product IDs is are stored in an array called IDs;
    $_pf = new WC_Product_Factory();  
    foreach ($IDs as $id) {
    
        $_product = $_pf->get_product($id);
    
        // from here $_product will be a fully functional WC Product object, 
        // you can use all functions as listed in their api
    }
    

    You can then use all the function calls as listed in their api: http://docs.woothemes.com/wc-apidocs/class-WC_Product.html

    0 讨论(0)
提交回复
热议问题