首页 > 解决方案 > WooCommerce 块:向所有产品网格添加新徽章

问题描述

尝试将新徽章添加到 WooCommerce 块中的所有产品网格。似乎没有将 span 元素合并到我的代码中。

这是我目前使用的 PHP:

add_filter('woocommerce_blocks_product_grid_item_html', 'add_new_woocommerce_blocks_product_grid_item_html', 10 , 3);
function add_new_desc_woocommerce_blocks_product_grid_item_html($content, $data, $product) {
    $new_badge = '<span class="new-badge onsale">' . esc_html__( 'NEW', 'woocommerce' ) . '</span>';
    $after_link_pos = strpos($content, "</a>");
    $before = substr($content, 0, $after_link_pos + 4);
    $after = substr($content, $after_link_pos + 4);

    $content = $before . $new_badge . $after;

    return $content;
}

标签: wordpresswoocommerce

解决方案


推荐阅读