You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于WooCommerce+CoCart+Dokan通过REST API获取购物车商品卖家名称报错

解决调用getAuthor()致命错误并获取Dokan卖家名称

错误原因

WooCommerce的WC_Product_Simple(及其他产品类)没有内置getAuthor()方法,直接调用这个方法必然触发未定义方法的致命错误。在Dokan多商家体系中,商品的卖家信息需通过Dokan专属函数获取,而非WooCommerce原生产品对象的方法。

解决方案

替换出错的代码行,使用Dokan提供的工具函数获取商品对应的卖家ID,再通过ID提取卖家店铺名称:

// 获取商品所属卖家ID(Dokan核心函数)
$vendor_id = dokan_get_product_author_id( $_product->get_id() );
// 通过卖家ID获取店铺名称,无名称时返回空字符串
$vendor_name = $vendor_id ? get_user_meta( $vendor_id, 'dokan_store_name', true ) : '';
// 将卖家名称写入购物车数组,保留过滤钩子支持后续扩展
$cart_contents[ $item_key ]['product_author'] = apply_filters( 'cocart_product_author', $vendor_name, $_product, $cart_item, $item_key );

完整修改后的代码片段(对应循环部分)

foreach ( $cart_contents as $item_key => $cart_item ) {
    // If product data is missing then get product data and apply.
    if ( ! isset( $cart_item['data'] ) ) {
        $cart_item['data']                  = wc_get_product( $cart_item['variation_id'] ? $cart_item['variation_id'] : $cart_item['product_id'] );
        $cart_contents[ $item_key ]['data'] = $cart_item['data'];
    }

    $_product = apply_filters( 'cocart_item_product', $cart_item['data'], $cart_item, $item_key );

    // If product is no longer purchasable then don't return it and notify customer.
    if ( ! $_product->is_purchasable() ) {
        /* translators: %s: product name */
        $message = sprintf( __( '%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.', 'cart-rest-api-for-woocommerce' ), $_product->get_name() );

        /**
         * Filter message about item removed from the cart.
         *
         * @since 2.1.0
         * @param string     $message Message.
         * @param WC_Product $_product Product data.
         */
        $message = apply_filters( 'cocart_cart_item_removed_message', $message, $_product );

        WC()->cart->set_quantity( $item_key, 0 ); // Sets item quantity to zero so it's removed from the cart.

        wc_add_notice( $message, 'error' );
    } else {
        // Adds the product name and title as new variables.
        $cart_contents[ $item_key ]['product_name']  = apply_filters( 'cocart_product_name', $_product->get_name(), $_product, $cart_item, $item_key );
        $cart_contents[ $item_key ]['product_title'] = apply_filters( 'cocart_product_title', $_product->get_title(), $_product, $cart_item, $item_key );
        
        // 替换原错误代码,获取Dokan卖家店铺名称
        $vendor_id = dokan_get_product_author_id( $_product->get_id() );
        $vendor_name = $vendor_id ? get_user_meta( $vendor_id, 'dokan_store_name', true ) : '';
        $cart_contents[ $item_key ]['product_author'] = apply_filters( 'cocart_product_author', $vendor_name, $_product, $cart_item, $item_key );

额外说明

  • dokan_get_product_author_id()支持所有商品类型,变体商品会自动继承父商品的卖家信息
  • 如果卖家未设置店铺名称,可改用用户名作为备选:$vendor_name = $vendor_id ? ( get_user_meta( $vendor_id, 'dokan_store_name', true ) ?: get_userdata( $vendor_id )->user_login ) : '';
  • 保留cocart_product_author过滤钩子,方便后续通过扩展自定义卖家名称的输出逻辑

内容的提问来源于stack exchange,提问作者Wonder In Wonderland

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 10:51:09