根据WooCommerce产品权重替换添加到购物车按钮



我正在用wordpress和woocommerce建立一个在线商店。如果产品的重量大于10克,我需要用一个新的按钮替换添加到购物车按钮。我怎样才能做到呢?

我有一个解决方案来替换添加到购物车按钮为特定的WooCommerce产品(基于重量)。因此,您可以使用下面的代码并将其放入functions.php

// To change add to cart text on single product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 
'woocommerce_custom_single_add_to_cart_text' ); 
function woocommerce_custom_single_add_to_cart_text() {
global $product;
$weight = $product->get_weight();
preg_replace('/D/', '', $weight);
if ( $product->has_weight() && ($weight > '9') ) {
return __( 'Custom text', 'text-domain' );
}
return __( 'Add to Cart', 'text-domain' );
}
// To change add to cart text on product archives(Collection) page
add_filter( 'woocommerce_product_add_to_cart_text', 
'woocommerce_custom_product_add_to_cart_text' );  
function woocommerce_custom_product_add_to_cart_text() {
global $product;
$weight = $product->get_weight();
preg_replace('/D/', '', $weight);
if ( $product->has_weight() && ($weight > '9')) {
return __( 'Custom text', 'text-domain' );
}
return __( 'Add to Cart', 'text-domain' );
}

最新更新