Làm việc với giá trong woocommerce
Trong bài viết này mình sẽ chia sẽ các code thường dùng trong mục giá của woocommerce, ví dụ như ẩn giá woocommerce khi chưa đăng nhập, chuyển giá thành liên hệ ….
Lưu ý: Coppy code ở dưới dán vào file functions.php trong child theme hoặc theme chính
Ẩn giá woocommerce khi người dùng chưa đăng nhập và chuyển hướng đến trang đăng nhập
function hide_price_not_login_nhaklamweb( $price, $product ) { if ( ! is_user_logged_in() ) { $price = '<div> <a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">' . __( 'Login to see prices', 'bbloomer' ) . '</a> </div>'; remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 ); remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 ); add_filter( 'woocommerce_is_purchasable', '__return_false' ); } return $price; } add_filter( 'woocommerce_get_price_html', 'hide_price_not_login_nhaklamweb', 9999, 2 );
Thay giá bằng link liên hệ qua zalo, facebook, hoặc link affiliate
add_filter('woocommerce_get_price_html', 'custom_call_for_price'); function custom_call_for_price() { return '<a class="price-ct" rel="no-follow" href="https://zalo.me/0374865097">Liên hệ qua zalo để biết giá</a>'; }
Thêm text trước giá trong woocoomerce
function them_text_truocgia( $price, $product ) { if ($product->is_on_sale()) : $has_sale_text = array( '<ins>' => '<span class="giakhuyenmai">Giá khuyến mãi: </span>', '<del>' => '<del><span class="gianiemyet">Giá niêm yết: </span>' ); $return_string = str_replace(array_keys( $has_sale_text ), array_values( $has_sale_text ), $price); else: $return_string = '<span class="giathuong">Giá niêm yết: </span>' . $price; endif; return $return_string; } add_filter( 'woocommerce_get_price_html', 'them_text_truocgia', 100, 2 );