Tổng hợp code hay dùng cho woocommerce

4 năm trước, Woocommerce, 1766 Lượt xem

Đăng ký cập nhập video từ netweb.vn

Code theme woocommerce

Mặc định khi cài plugin woocommerce vào 1 theme wordpress setup từ đầu thì nó sẽ không hiển thị trang shop và sản phẩm bạn cần phải add thêm 1 số thành phần vào thì theme của bạn mới hỗ trợ được woocommerce

 

add WooCommerce Theme Developer Handbook

Add vào file function.php trong theme của bạn để hỗ trợ WooCommerce

function mytheme_add_woocommerce_support() {
    add_theme_support( 'woocommerce' );
}

add_action( 'after_setup_theme', 'mytheme_add_woocommerce_support' );

https://docs.woocommerce.com/document/woocommerce-theme-developer-handbook/

Nếu cần dùng css của WooCommerce thì add thêm thẻ <?php body_class(); ?> này vào body, còn tự viết css thì không cần

<body <?php body_class(); ?>>

Hiển thị giỏ hàng mini với ajax

Cách 1: https://stackoverflow.com/questions/50033227/is-there-any-way-to-display-the-woocommerce-mini-cart-on-my-wordpress-site

Function

function custom_mini_cart() { 
    echo '<a href="#" class="dropdown-back" data-toggle="dropdown"> ';
    echo '<i class="fa fa-shopping-cart" aria-hidden="true"></i>';
    echo '<div class="basket-item-count" style="display: inline;">';
        echo '<span class="cart-items-count count">';
            echo WC()->cart->get_cart_contents_count();
        echo '</span>';
    echo '</div>';
    echo '</a>';
    echo '<ul class="dropdown-menu dropdown-menu-mini-cart">';
        echo '<li> <div class="widget_shopping_cart_content">';
                  woocommerce_mini_cart();
            echo '</div></li></ul>';

      }
       add_shortcode( '[custom-techno-mini-cart]', 'custom_mini_cart' );

Chèn vị trí muốn hiển thị cart

<div class="widget_shopping_cart_content"><?php woocommerce_mini_cart(); ?></div>

Cách 2: thay đổi số lượng, tổng giá của woocommerce mini cart

Function

/*Woocommerce minicart*/
add_filter('woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
 global $woocommerce;
 ob_start();
 ?>
 <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
 <?php
 $fragments['a.cart-contents'] = ob_get_clean();
 return $fragments;
}

Chỗ muốn hiển thị mini cart

<?php global $woocommerce; ?>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>">
<?php
echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total();
?>
</a>

Hiển thị breadcrumb cho woo

https://docs.woocommerce.com/document/woocommerce_breadcrumb/

<?php woocommerce_breadcrumb(); ?>

Code phân trang paginate theo Shortcodes

Link doc Shortcodes: https://docs.woocommerce.com/document/woocommerce-shortcodes/

để paginate=”true” để phân trang

[recent_products limit="4" columns="4" paginate="true"]

paginate – Toggles pagination on. Use in conjunction with limit. Defaults to false set to true to paginate .

 

Code hiện giỏ hàng

$id = get_the_ID();

<?php echo do_shortcode( '[add_to_cart id="' . $id . '" show_price="false"]' )?>
<a rel="nofollow" href="<?php echo do_shortcode( '[add_to_cart_url id="' . $id . '"]' )?>" class="add_to_cart_button ajax_add_to_cart">Add to cart</a>
<?php echo do_shortcode( '[add_to_cart_url id="' . $id . '"]' )?>

<?php echo do_shortcode( '[add_to_cart id="' . $id . '"]' )?>

Hiển thị (sale flash) % chiết khấu cho sản phẩm – WooCommerce

thêm đoạn code vào file function

add_filter('woocommerce_sale_flash', 'my_custom_sale_flash', 10, 3);
function my_custom_sale_flash($text, $post, $_product) {
    $from = $_product->regular_price;
    $to = $_product->price;
    if($from==$to || !$to) return '';
    $percent=round(($from-$to)/$from*100);
    $text=$from>$to? 'Giảm':'Tăng';
    return '<span class="onsale">'.$text.' '.$percent.' %</span>';  
}

Thay đổi số lượng sản phẩm trên một hàng trong shop – woocommerce

// Change number or products per row to 3
add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
    function loop_columns($num) {
        return 5; // 3 products per row
    }
}

Thay đổi số lượng sản phẩm hiển thị trong cửa hàng để phân trang

mặc định woo phân trang 16 sản phẩm, các bạn dùng code dưới chỉnh cho cho hợp lý

add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 8;' ), 20 );

Checkout Field Editor sửa lại form

dùng plugin sửa cho nhanh: https://docs.woocommerce.com/document/checkout-field-editor/

Thêm nội dung vào trang oder trong woo, 9, 10 là thứ tự ưu tiên trước sau

 function order_ngay_giao($order){
    echo "<p><strong>Ngày giao hàng:</strong> " . get_post_meta( $order->id, 'billing_ngay-giao', true ) . "</p>";
} 
add_action( 'woocommerce_admin_order_data_after_billing_address', 'order_ngay_giao', 9, 1 );

 function order_buoi_giao_hang($order){
    echo "<p><strong>Buổi giao hàng:</strong> " . get_post_meta( $order->id, 'billing_buoi-giao', true ) . "</p>";
} 
add_action( 'woocommerce_admin_order_data_after_billing_address', 'order_buoi_giao_hang', 10, 1 );

 

 

Bài viết mới cập nhập

  • Hướng dẫn Plugin floating contact

    Hướng dẫn Plugin floating contact

    10 tháng trước, 336 Lượt xem

    Netweb xin hướng dẫn cách quản lý Plugin floating contact các bạn nhớ làm theo kỹ tường bước để plugin hoạt động Tính năng: Hiển…

  • Backup Yandex Mail

    Backup Yandex Mail

    12 tháng trước, 225 Lượt xem

    Khác với dịch vụ email theo tên của microsoft và google với chi phí đắt đỏ tầm 1tr/năm/1user, vì thế nên netweb khuyên dùng dịch…

Bình luận trên facebook netweb.vn

Đam mê thiết kế web, kinh nghiệm 9 năm trong nghề thiết kế website, đang thiết kế web dạo tại netweb.vn, Callme: 08.9898.2526 (Mr Hải)