单一产品页面中的Dokan Marketplace商店链接

时间:2021-10-22 作者:Teymur Abbasov

我用这种方式展示产品store link with [vendor_shop_name] shortcode in custom product page.

/*Shortcode [vendor_shop_name] https://stackoverflow.com/questions/55364574/display-dokan-vendor-name-and-total-sales-on-woocommerce-single-product-pages*/
add_shortcode(\'vendor_shop_name\', \'vendor_shop_name_function\');
function vendor_shop_name_function() {
    global $product;
    $seller = get_post_field(\'post_author\', $product->get_id());
    $author  = get_user_by(\'id\', $seller);
    $vendor = dokan()->vendor->get($seller);
    $store_info = dokan_get_store_info($author->ID);
    
    if (!empty($store_info[\'store_name\'])) {
        ?>
            <?php printf(\'<span class="vendorpage"><a href=" %s">in this store</a></span>\', $vendor->get_shop_url(), $vendor->get_shop_name()); ?>
        <?php
    }
}
它起作用了。但我的问题是store link appear before header. I can not put it where I want. 谁能帮帮我吗

1 个回复
最合适的回答,由SO网友:Pat J 整理而成

短代码应该永远return 内容,从不echoprintf()

add_shortcode() documentation:

请注意,由shortcode调用的函数应该不会产生任何类型的输出。短代码函数应返回用于替换短代码的文本。直接生成输出将导致意外的结果。

因此,您的代码应该更像这样。

add_shortcode(\'vendor_shop_name\', \'vendor_shop_name_function\');
function vendor_shop_name_function() {
    global $product;
    $seller = get_post_field(\'post_author\', $product->get_id());
    $author  = get_user_by(\'id\', $seller);
    $vendor = dokan()->vendor->get($seller);
    $store_info = dokan_get_store_info($author->ID);
    
    if (!empty($store_info[\'store_name\'])) {
        return sprintf( \'<span class="vendorpage"><a href=" %s">in this store %s</a></span>\', $vendor->get_shop_url(), $vendor->get_shop_name() );
    }
    // Nothing found; return an empty string.
    return \'\';
}