52

woocommerce のカスタム テーマを作成しており、ミニ製品ディスプレイを作成できるようにする必要があります。woocommerce API に関するドキュメントを見つけるのに問題があります。製品 ID のコンマ区切りのリストがあり、これを反復処理して、それぞれのカスタム ミニ製品ディスプレイを順番に表示する必要があります。

$key_values = get_post_custom_values('rel_products_ids');
//get comma delimited list from product

$rel_product_ids = explode(",", trim($key_values, ",")); 
// create array of just the product ids

foreach ( $rel_product_ids as $pid ) { 
    //sequentially get each id and do something with it

    $loop = new WP_Query( array( 'post__in' => $pid ) );
    // also tried ...
    //$loop = new WP_Query( array( 'ID' => $pid ) );

    while ( $loop->have_posts() ) : $loop->the_post(); $_product = &new WC_Product( $loop->post->ID );
        //do stuff here I have stripped the html in favor of getting to the meat of the issue
        woocommerce_show_product_sale_flash( $post, $_product );
        if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single');
        get_permalink( $loop->post->ID );
        the_title(); 
        $_product->get_price_html();
    endwhile;
}

どんな助けでも大歓迎です。

ありがとうございました、

ティム

4

4 に答える 4

122

次の方法を使用します。

$_product = wc_get_product( $id );

公式 API ドキュメント: wc_get_product

于 2016-09-17T08:38:24.840 に答える
4

わかった、私は絞られるに値する。間違いなく RTM ですが、WooCommerce ではなく、Wordpress です。JOLTコーラが原因で解決策が見つかりました(すべてJOLTコーラの雹)。

タスク: 「related_product_ids」という名前のフィールドがカスタム投稿タイプに追加されました。したがって、その投稿が表示されると、ミニ製品の表示が表示されます。

問題: WP_Query を介して返される複数の ID の取得に問題がありました。

解決:

$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;

これに時間を費やしてくれた人に感謝します。

ティム

于 2012-09-25T09:21:08.700 に答える
0
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);
}
于 2016-02-06T10:52:07.733 に答える