亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在多供應商設置中按作者/用戶對購物車中的產品進行排序

在多供應商設置中按作者/用戶對購物車中的產品進行排序

PHP
茅侃侃 2023-04-15 20:44:34
我有一個使用Dokan 插件的多供應商 Woocommerce 商店,我試圖根據供應商是誰將購物車分成幾個部分。例如:供應商 1產品C產品B供應商 2產品ADokan 使用自定義角色 'vendor' 來擴展用戶類,因此要獲取供應商的 ID,我應該能夠使用如下內容:$post_data = get_post( $cart_item['product_id'] );  $vendor_id = $post_data->post_author;這確實有效,但它只會獲得第一個供應商 ID,并對購物車中的所有剩余產品重復此操作。我知道這是因為我沒有檢索數組,但我在 WP 文檔中找不到任何關于如何獲取作者 ID 數組的內容(wp_list_authors 除外,但效果不佳)。作為一個實驗,只要我按類別排序,我就設法使拆分 + 排序工作,因為我可以使用 wp_get_post_terms()。不過,我無法為作者數據復制這個……當前(相關)代碼如下:<?php$cat_sort = array();foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {  $product_id = $cart_item['product_id'];  $cat_ids = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );  foreach ( $cat_ids as $id ) {    $cat_sort[$id][$cart_item_key] = $cart_item;  }                                                    }ksort( $cat_sort ); $grouped_cart_items = array();  foreach ( $cat_sort as $cat_id => $cart_items ) {    $term = get_term( $cat_id, 'product_cat' );                           ?><tr>  <td colspan="6" class=""><strong><?php echo $term->name; ?></strong></td></tr>(在此之后是實際的產品循環,這在這里不重要,因為它們的排序順序發生在上面的代碼中)關于如何以獲取類別的相同方式獲取購物車產品的作者信息的任何想法?我很困惑...在此先感謝您提供的所有幫助!
查看完整描述

1 回答

?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

以下是有關如何按 Dokan 供應商商店名稱排序和顯示購物車商品的示例:


<table>

<?php


$car_items  = WC()->cart->get_cart(); // Cart items


$items_sort = array(); // Initializing


// Loop through cart items

foreach ( $car_items as $cart_item_key => $cart_item ) {

    // Get the vendor_id

    $vendor_id   = get_post_field( 'post_author', $cart_item['product_id'] );


    $store_info  = dokan_get_store_info( $vendor_id ); // Get the store data

    $store_name  = $store_info['store_name'];          // Get the store name


    // Set in multidimentional array the vendor and then the cart item key

    $items_sort[$store_name][$cart_item_key] = $vendor_id;

}


if ( count($car_items) > 1 ) {

    ksort( $items_sort ); // Sorting by vendor name

}


// 1st Loop by vendor name

foreach ( $items_sort as $store_name => $values ) {

    $vendor_id  = reset($values); // The vendor id

    /$store_url = dokan_get_store_url( $vendor_id );  // Get the store URL (if needed)

    ?>

    <tr>

        <!-- Store name display -->

        <td colspan="6" class="store-name"><strong><?php echo $store_name; ?></strong></td>

    </tr>

    <?php

    // 2nd Loop the cart items for the vendor name

    foreach( $values as $cart_item_key => $vendor_id) {


        // Retreive the cart item from the cart item key

        $cart_item = $car_items[$cart_item_key];

        ?>

        <tr>

            <!-- Product name display -->

            <td colspan="6" class="product-name"><?php echo $cart_item['data']->get_name(); ?></td>

        </tr>

        <?php


    } // End of 2nd Loop


} // End of 1st Loop


?>

</table>


查看完整回答
反對 回復 2023-04-15
  • 1 回答
  • 0 關注
  • 138 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號