添加css类的正确方法是什么?



我试图改变一个按钮的标签后,它被点击。我尝试在返回值中添加CSS类,以便我可以使用它来设置样式。

代码

//Rename the button on the Product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'ts_product_add_cart_button' );

function ts_product_add_cart_button( $label ) {

foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
if( get_the_ID() == $product->get_id() ) {
$label = __('Already added to Cart', 'woocommerce');
}
}

return $label;

}

//Rename the button on the Shop page 
add_filter( 'woocommerce_product_add_to_cart_text', 'ts_shop_add_cart_button', 99, 2 );

function ts_shop_add_cart_button( $label, $product ) {

if ( $product->get_type() == 'simple' && $product->is_purchasable() && $product->is_in_stock() ) 
{

foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if( get_the_ID() == $_product->get_id() ) {
$label = __('Already added to Cart', 'woocommerce');
}
}    
}
return $label;    
}

这是我管理的最接近的,但与echo。但是,它弄乱了按钮的外观和对齐方式。

echo '<span style=background:yellow;>'. $label . '</span>';  

我也试过

$label = __('<span style=background:yellow;>Added to cart</span>', 'woocommerce');

WooCommerce有两个钩子,可以让你在自定义标记中包装按钮,并使用它可以在javascript中访问按钮以更改标签?

add_action( 'woocommerce_before_add_to_cart_button', 'before_button_markup' );
function before_button_markup(){
echo '<div class="custom-button">';
}
add_action( 'woocommerce_after_add_to_cart_button', 'after_button_markup' );
function after_button_markup(){
echo '</div>';
}

相关内容

最新更新