在多供应商设置中按作者/用户对购物车中的产品进行排序

我有一个使用Dokan 插件的多供应商 Woocommerce 商店,我试图根据供应商是谁将购物车分成几个部分。例如:

  • 供应商 1

    • 产品C

    • 产品B

  • 供应商 2

    • 产品A

Dokan 使用自定义角色 '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>

(在此之后是实际的产品循环,这在这里不重要,因为它们的排序顺序发生在上面的代码中)


关于如何以获取类别的相同方式获取购物车产品的作者信息的任何想法?我很困惑...


在此先感谢您提供的所有帮助!


茅侃侃
浏览 84回答 1
1回答

潇湘沐

以下是有关如何按 Dokan 供应商商店名称排序和显示购物车商品的示例:<table><?php$car_items&nbsp; = WC()->cart->get_cart(); // Cart items$items_sort = array(); // Initializing// Loop through cart itemsforeach ( $car_items as $cart_item_key => $cart_item ) {&nbsp; &nbsp; // Get the vendor_id&nbsp; &nbsp; $vendor_id&nbsp; &nbsp;= get_post_field( 'post_author', $cart_item['product_id'] );&nbsp; &nbsp; $store_info&nbsp; = dokan_get_store_info( $vendor_id ); // Get the store data&nbsp; &nbsp; $store_name&nbsp; = $store_info['store_name'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get the store name&nbsp; &nbsp; // Set in multidimentional array the vendor and then the cart item key&nbsp; &nbsp; $items_sort[$store_name][$cart_item_key] = $vendor_id;}if ( count($car_items) > 1 ) {&nbsp; &nbsp; ksort( $items_sort ); // Sorting by vendor name}// 1st Loop by vendor nameforeach ( $items_sort as $store_name => $values ) {&nbsp; &nbsp; $vendor_id&nbsp; = reset($values); // The vendor id&nbsp; &nbsp; /$store_url = dokan_get_store_url( $vendor_id );&nbsp; // Get the store URL (if needed)&nbsp; &nbsp; ?>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <!-- Store name display -->&nbsp; &nbsp; &nbsp; &nbsp; <td colspan="6" class="store-name"><strong><?php echo $store_name; ?></strong></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <?php&nbsp; &nbsp; // 2nd Loop the cart items for the vendor name&nbsp; &nbsp; foreach( $values as $cart_item_key => $vendor_id) {&nbsp; &nbsp; &nbsp; &nbsp; // Retreive the cart item from the cart item key&nbsp; &nbsp; &nbsp; &nbsp; $cart_item = $car_items[$cart_item_key];&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- Product name display -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td colspan="6" class="product-name"><?php echo $cart_item['data']->get_name(); ?></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; } // End of 2nd Loop} // End of 1st Loop?></table>
打开App,查看更多内容
随时随地看视频慕课网APP