Tìm theo từ khóa

Tổng hợp các đoạn code hay dùng cho woocommerce



Woocommerce là plugin quản lý bán hàng trên wordpress được dùng phổ biến nhất hiện nay. Để phù hợp với thị trường Việt Nam thường chúng ta thường phải tùy chỉnh rất nhiều thứ mới dùng được. Bài viết này mình tổng hợp tất cả các đoạn code hay dùng cho những nhiệm vụ cụ thể để các bạn tham khảo.

Bỏ các trường không cần thiết trong trang check out (thanh toán)

/** codfe.com 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' );

Các bạn xem thêm bài viết các trường trong trang checkout Woocommerce  để ẩn các trường cần ẩn nhé

Bỏ bắt buộc nhập và xóa trường billing_address_2 và shipping_address_2

// Do NOT include the opening php tag.
// Place in your theme's functions.php file

// 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 

Mục đích là làm gọn lại và đưa ra các gợi ý(placeholder) nhập chính xác cho khách hàng

/** codfe.com 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;
}

/** codfe.com 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 

/** Codfe.com 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

// lây 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

// lây 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' );

 

5/5 - (1 bình chọn)
0 0 đánh giá
Đánh giá bài viết
Theo dõi
Thông báo của
guest
0 Góp ý
Mới nhất
Cũ nhất Được bỏ phiếu nhiều nhất
0
Rất thích suy nghĩ của bạn, hãy bình luận.x