以下是我的解决方案:
function woocommerce_template_loop_add_to_cart() {
global $product;
$defaults = array(
\'quantity\' => 1,
\'class\' => implode(
\' \',
array_filter(
array(
\'button\',
\'product_type_\' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? \'add_to_cart_button\' : \'\',
$product->supports( \'ajax_add_to_cart\' ) && $product->is_purchasable() && $product->is_in_stock() ? \'ajax_add_to_cart\' : \'\',
)
)
),
\'attributes\' => array(
\'data-product_id\' => $product->get_id(),
\'data-product_sku\' => $product->get_sku(),
\'aria-label\' => $product->add_to_cart_description(),
\'rel\' => \'nofollow\',
),
);
$args = apply_filters( \'woocommerce_loop_add_to_cart_args\', wp_parse_args( $args, $defaults ), $product );
$link = apply_filters( \'woocommerce_loop_product_link\', get_the_permalink(), $product );
echo \'<div class="woocommerce-LoopProduct-buttons-container">\';
if ( $product->is_purchasable() ) {
echo \'<a href="\' . esc_url( $link ) . \'" class="button button--read-more">\'.__( \'Read more\', \'woocommerce\' ).\'</a>\';
}
echo apply_filters(
\'woocommerce_loop_add_to_cart_link\', // WPCS: XSS ok.
sprintf(
\'<a href="%s" data-quantity="%s" class="%s" %s>%s</a>\',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $args[\'quantity\'] ) ? $args[\'quantity\'] : 1 ),
esc_attr( isset( $args[\'class\'] ) ? $args[\'class\'] : \'button\' ),
isset( $args[\'attributes\'] ) ? wc_implode_html_attributes( $args[\'attributes\'] ) : \'\',
esc_html( $product->add_to_cart_text() )
),
$product,
$args
);
echo \'</div>\';
}
这是关键因素:
if ( $product->is_purchasable() ) {
echo \'<a href="\' . esc_url( $link ) . \'" class="button button--read-more">\'.__( \'Read more\', \'woocommerce\' ).\'</a>\';
}
缺少$args,因此我也添加了它。