我是 wordpress 和 woocommerce 的新手。如果产品重量大于 8 克,我必须隐藏添加到购物车按钮。我已使用此代码执行此操作。
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
$weight = $product->get_weight();
preg_replace('/\D/', '', $weight);
if ( $weight > 8 ){
$button = '';
}
return $button;
}
它在商店页面上运行良好。但它不适用于单个产品页面。仅当产品重量大于 8 克时,请帮我隐藏单个产品页面中的添加到购物车按钮。
答案 0 :(得分:0)
您的代码适用于商店页面,对于单个页面您需要使用不同的钩子:
// Removing the single product button add to cart
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
global $product;
$weight = $product->get_weight();
preg_replace('/\D/', '', $weight);
if ( $weight > 8 ){
// For variable product types
remove_action( 'woocommerce_single_variation','woocommerce_single_variation_add_to_cart_button', 20 );
// For all other product types
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}