Tuesday, July 22, 2014

thiết kế website bằng wordpress
Một số quy ước cần lưu ý:
- Giá của sản phẩm được tạo bằng plugin: Advanced Custom Fields với meta_key là: "gia"
- Sản phẩm sẽ có danh mục sản phẩm, taxonomy tên là: "danhmucsanpham"
Post type có tên là: "sanpham": URL trong admin của sản phẩm như hình dưới

thiết kế website bằng wordpress

Ta có 4 URL để sắp xếp sản phẩm:
- Sắp xếp giá từ thấp-cao: ?orderby=gia&order=asc
- Sắp xếp giá từ cao-thấp: ?orderby=gia&order=desc
Sắp xếp tên sản phẩm từ A-Z: ?orderby=title&order=asc
Sắp xếp tên sản phẩm từ Z-A: ?orderby=title&order=desc

Xem code sắp xếp sản phẩm trong wordpress:

<?php

// Đoạn 1
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

// Đoạn 2
$order2 = array();
if (isset($_GET['orderby']) && isset($_GET['order']) && $_GET['orderby'] == "title") {
    $order2 = array(
        'orderby' => 'title',
        'order' => $_GET['order']
    );
}

// Đoạn 3
if (isset($_GET['orderby']) && isset($_GET['order']) && $_GET['orderby'] == "gia") {
    $order2 = array(
        'meta_key' => 'gia',
        'orderby' => 'meta_value',
        'order' => $_GET['order']
    );
}

// Đoạn 4
$order1 = array(
    'post_type' => 'sanpham',
    'tax_query' => array(
        array(
            'taxonomy' => 'danhmucsanpham',
            'terms' => $term_id,
            'field' => 'term_id',
        )
    )
);

// Đoạn 5
$order = array_merge($order1, $order2);

// Đoạn 6
query_posts($order);
while (have_posts()) {
    the_post();

?>


      // Code lặp sản phẩm ở đây

    <?php
}
wp_reset_query();
?>

Gải thích chút xíu nhé:
- Đoạn 1: Lấy ID danh mục sản phẩm (taxonomy) hiện tại, mục đích là lấy ra những sản phẩm (post) theo ID danh mục sản phẩm (taxonomy).
- Đoạn 2: Dùng để lấ các tham số từ URL của việc sắp xếp tên sản phẩm từ A-Z hoặc Z-A, giá trị nhận được sẽ lưu trong mảng tên là $order2
- Đoạn 3: Dùng để lấ các tham số từ URL của việc sắp xếp giá sản phẩm từ cao đến thấp hoặc thấp -cao, giá trị nhận được sẽ lưu trong mảng tên là $order2
- Đoạn 4: Tạo ra các mảng để lấy những sản phẩm (post) theo post_type, id danh mục sản phẩm (taxonomy).
- Đoạn 5: Dùng để ghép 2 mảng với nhau
- Đoạn 6: Truyền tham số mảng đã ghép ở đoạn 5
Và lặp theo giao diện của bạn.

Về HTML

// Đoạn 1
<?php
$search = array($_SERVER['QUERY_STRING'], '?');
$replace = array('', '');
$currentUrl = str_replace($search,$replace, $_SERVER['REQUEST_URI']);
?>

// Đoạn 2
<select onchange="window.location = $(this).val();">
    <option selected="selected" value="position:asc">Sắp xếp</option>
    <option value="<?= $currentUrl; ?>?orderby=gia&order=asc">Giá từ thấp - cao</option>
    <option value="<?= $currentUrl; ?>?orderby=gia&order=desc">Giá từ cao - thấp</option>
    <option value="<?= $currentUrl; ?>?orderby=title&order=asc">Tên từ A - Z</option>
    <option value="<?= $currentUrl; ?>?orderby=title&order=desc">Tên Z - A</option>
</select>


Giải thích xíu nhé:

- Đoạn 1: Lấy URL hiện tại chuẩn (đoạn này có rất hay, làm trực tiếp bạn sẽ rõ)
- Đoạn 2: Các tùy chọn để sắp xếp

Dựa vào code mẫu này, bạn có thể tùy biến theo các sắp xếp riêng theo mục đích của bạn.
Thiết kế website bằng wordpress hướng dẫn sắp xếp danh sách sản phẩm theo một trong các tiêu chí như: sắp xếp giá từ thấp-cao, sắp xếp giá từ cao-thấp, sắp xếp tên A-Z, sắp xếp tên Z-A.


0 comments:

Post a Comment