Nội dung
Tổng hợp các đoạn code hay dùng cho woocommerce
Woocommerce là 1 plugin không thể thiếu giúp bạn tạo ra 1 trang web bán hàng hoàn chỉnh. Tuy nhiên, để cho phù hợp với thị trường Việt Nam thường chúng ta sẽ phải tùy chỉnh rất nhiều thứ mới dùng được. Hôm nay, MKTShare sẽ gợi ý 1 vài đoạn code hữu ích giúp bạn tuỳ chỉnh khi làm web.
Bỏ các trường không cần thiết trong trang check out (thanh toán)
/** Bỏ các trường ko cần trong trang checkout------------ **/function cs_woocommerce_remote_billing_fields( $fields ) { unset( $fields['billing_last_name'] ); //unset( $fields['billing_phone'] ); //unset( $fields['billing_address_1'] ); unset( $fields['billing_address_2'] ); unset( $fields['billing_city'] ); unset( $fields['billing_postcode'] ); unset( $fields['billing_state'] ); //unset( $fields['billing_country'] ); //unset( $fields['billing_company'] ); $fields['billing_email']['class'] = array( 'form-row-wide' ); return $fields; } add_filter( 'woocommerce_billing_fields', 'cs_woocommerce_remote_billing_fields' );
Bỏ bắt buộc nhập và xóa trường billing_address_2 và shipping_address_2
// Set address 2 to not required add_filter('woocommerce_checkout_fields', 'unrequire_address_2_checkout_fields' ); function unrequire_address_2_checkout_fields( $fields ) { $fields['billing']['billing_address_2']['required'] = false; $fields['shipping']['shipping_address_2']['required'] = false; return $fields; } // Remove billing address 2 from Checkout for WooCommerce add_filter('cfw_get_billing_checkout_fields', 'remove_billing_address_2_checkout_fields', 100, 3); function remove_billing_address_2_checkout_fields( $fields ) { unset($fields['billing_address_2']); return $fields; } // Remove shipping address 2 from Checkout for WooCommerce add_filter('cfw_get_shipping_checkout_fields', 'remove_shipping_address_2_checkout_fields', 100, 3); function remove_shipping_address_2_checkout_fields( $fields ) { unset($fields['shipping_address_2']); return $fields; }
Ẩn các title field và thêm placeholder cho các trường trong trang Checkout
/**Gỡ bỏ các title field trong trang checkout------------------ **/// WooCommerce Checkout Fields Hook add_filter('woocommerce_checkout_fields','custom_wc_checkout_fields_no_label'); // Our hooked in function - $fields is passed via the filter! // Action: remove label from $fields function custom_wc_checkout_fields_no_label($fields) { // loop by category foreach ($fields as $category => $value) { // loop by fields foreach ($fields[$category] as $field => $property) { // remove label property unset($fields[$category][$field]['label']); } } return $fields; } /**Thêm Placeholder (gợi ý) cho checkout field ------------------------- **/add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 ); function override_billing_checkout_fields( $fields ) { $fields['billing']['billing_first_name']['placeholder'] = 'Họ và tên'; $fields['billing']['billing_phone']['placeholder'] = 'Điện thoại'; $fields['billing']['billing_address_1']['placeholder'] = 'Địa chỉ'; $fields['billing']['billing_email']['placeholder'] = 'Địa chỉ Email'; return $fields; }
Sắp xếp thứ tự các trường trong trang Checkout
/**Sắp xếp các trường ------------------------------------------------------------- **/add_filter("woocommerce_checkout_fields", "new_order_fields"); function new_order_fields($fields) { $order = array( "billing_first_name", "billing_phone", "billing_address_1", "billing_email" ); foreach( $order as $field ) { $ordered_fields[$field] = $fields["billing"][$field]; } $fields["billing"] = $ordered_fields; return $fields; }
Liệt kê các sản phẩm bán chéo Cross Sale
// Lay danh sach cac san pham cross sale function show_cross_sell_in_single_product(){ $crosssells = get_post_meta( get_the_ID(), '_crosssell_ids',true); if(empty($crosssells)){ return; } $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post__in' => $crosssells ); $products = new WP_Query( $args ); ob_start(); if( $products->have_posts() ) : echo '<div class="cross-sells"><h2>Sản phẩm khác</h2>'; woocommerce_product_loop_start(); while ( $products->have_posts() ) : $products->the_post(); wc_get_template_part( 'content', 'product' ); endwhile; // end of the loop. woocommerce_product_loop_end(); echo '</div>'; endif; wp_reset_postdata(); $content = ob_get_contents(); ob_end_clean(); return $content; } add_shortcode( 'tp_cross_sale', 'show_cross_sell_in_single_product' );
Liệt kê các sản phẩm bán kèm Up Sale
// Lay danh sach cac san pham up sale function show_up_sell_in_single_product(){ $upsells = get_post_meta( get_the_ID(), '_upsell_ids',true); if(empty($upsells)){ return; } $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post__in' => $upsells ); $products = new WP_Query( $args ); ob_start(); if( $products->have_posts() ) : echo '<div class="cross-sells"><h2>Sản phẩm liên quan</h2>'; woocommerce_product_loop_start(); while ( $products->have_posts() ) : $products->the_post(); wc_get_template_part( 'content', 'product' ); endwhile; // end of the loop. woocommerce_product_loop_end(); echo '</div>'; endif; wp_reset_postdata(); $content = ob_get_contents(); ob_end_clean(); return $content; } // lây danh sach cac san pham cross sale add_shortcode( 'tp_up_sale', 'show_up_sell_in_single_product' );