Bạn muốn ẩn nút thêm vào giỏ hàng (add to cart) hoặc ẩn cả giá trong trong WooCommerce ra khỏi trang thông tin sản phẩm thì sau đây muaplugin.com chia sẻ với bạn 1 số đoạn code giúp bạn điều đó. Bạn chỉ việc thêm đoạn code vào trong file functions.php trong child theme là xong.
Ẩn nút thêm vào giỏ hàng trong WooCommerce
Để ẩn nút thêm vào giỏ hàng bạn sử dụng đoạn code dưới đây
add_action( 'init', 'paulc_hide_add_to_cart_button', 1000 ); function paulc_hide_add_to_cart_button() { add_filter( 'woocommerce_loop_add_to_cart_link', '__return_null' ); add_filter( 'wc_get_template', 'paulc_wc_get_template', 90, 2 ); } function paulc_wc_get_template( $located, $template_name ) { $new_path = get_stylesheet_directory_uri() . '/woocommerce/no-add-to-cart-button.php'; $templates = array( 'single-product/add-to-cart/simple.php', 'single-product/add-to-cart/grouped.php', 'single-product/add-to-cart/variable.php', 'single-product/add-to-cart/external.php' ); if( in_array( $template_name, $templates ) ) { return $new_path; } return $located; }
Code Ẩn Giá và Nút thêm vào giỏ hàng với tất cả Khách hàng
add_action( 'init', 'paulc_hide_price_add_to_cart_button', 1000 ); function paulc_hide_price_add_to_cart_button() { add_filter( 'woocommerce_get_price_html', '__return_false' ); add_filter( 'woocommerce_loop_add_to_cart_link', '__return_null' ); add_filter( 'wc_get_template', 'paulc_wc_get_template', 90, 2 ); } function paulc_wc_get_template( $located, $template_name ) { $new_path = get_stylesheet_directory_uri() . '/woocommerce/no-add-to-cart-button.php'; $templates = array( 'single-product/add-to-cart/simple.php', 'single-product/add-to-cart/grouped.php', 'single-product/add-to-cart/variable.php', 'single-product/add-to-cart/external.php' ); if( in_array( $template_name, $templates ) ) { return $new_path; } return $located; }
Ẩn Giá và Nút nút thêm vào giỏ hàng khi khách hàng chưa đăng nhập
add_action( 'init', 'paulc_hide_price_add_to_cart_button', 1000 ); function paulc_hide_price_add_to_cart_button() { if( is_user_logged_in() ) return; add_filter( 'woocommerce_get_price_html', '__return_false' ); add_filter( 'woocommerce_loop_add_to_cart_link', '__return_null' ); add_filter( 'wc_get_template', 'paulc_wc_get_template', 90, 2 ); } function paulc_wc_get_template( $located, $template_name ) { $new_path = get_stylesheet_directory_uri() . '/woocommerce/no-add-to-cart-button.php'; $templates = array( 'single-product/add-to-cart/simple.php', 'single-product/add-to-cart/grouped.php', 'single-product/add-to-cart/variable.php', 'single-product/add-to-cart/external.php' ); if( in_array( $template_name, $templates ) ) { return $new_path; } return $located; }