17/10/17

Thay đổi table prefix sau khi cài đặt WordPress cách 2

WordPress Database là bộ não của một trang wordpress bởi tất cả thông tin từ quan trọng cho đến đơn giản đều được lưu tại đây, các hacker vì thế cũng tấn công vào phần này nhất bằng các câu lệnh SQL injections, vì thế việc bảo mật cho cơ sở dữ liệu trong trang wordpress là điều cần thiết nhất phải làm, tuy nhiên có rất nhiều người khi cài đặt wordpress lại thường quên mất việc đổi lại tiền tố (prefix) wp_ mặc định, việc này rất nguy hiểm vì hacker sẽ dễ dàng xâm nhập vào cơ sở dữ liệu hơn là khi ta thay nó bằng 1 cái tên khó nhớ hơn như wp1234_ chẳng hạn.

Nếu bạn đã lỡ cài rồi thì mình chia sẻ 6 bước sau để tiến hành thay đổi , có rất nhiều plugin hỗ trợ làm điều này,  1 trong số đó là “wp prefix changer”, nhưng theo mình thì các bạn nên tự tay làm sẽ an toàn và chính xác hơn nhiều. Nào chúng ta cùng bắt tay làm nhé.

Bước 1 : Sao lưu (Backup) lại toàn bộ cơ sở dữ liệu :

Điều này là bắt buộc mỗi khi bạn có một sự thay đổi nào đó với toàn bộ trang web của mình, để lỡ khi có làm hỏng hay sai sót điều gì chúng ta vẫn có thể cho website của mình trở lại trạng thái ban đầu nhanh nhất.

Bước 2 : Thay đổi tiền tố (prefix) trong wp-config.php

Các bạn tìm đến đoạn sau và thay đổi thành tên mà các bạn muốn như hình sau :

$table_prefix = ‘wp_’;
// đổi thành
$table_prefix  = 'op2mro445_';

Bước 3 : Thay đổi tên của các bảng (tables) trong cơ sở dữ liệu :

Bạn truy xuất vào phpMyAdmin

Mặc định là có tất cả 11 bảng, sau đó các bạn chọn SQL như hình sau :



Sau đó các bạn dán đoạn code sau :

Rename table wp_commentmeta to op2mro445_commentmeta;
Rename table wp_comments to op2mro445_comments;
Rename table wp_links to op2mro445_links;
Rename table wp_options to op2mro445_options;
Rename table wp_postmeta to op2mro445_postmeta;
Rename table wp_posts to op2mro445_posts;
Rename table wp_terms to op2mro445_terms;
Rename table wp_term_relationships to op2mro445_term_relationships;
Rename table wp_term_taxonomy to op2mro445_term_taxonomy;
Rename table wp_usermeta to op2mro445_usermeta;
Rename table wp_users to op2mro445_users;
Nhấn Go đẻ thực hiện câu lệnh.

Bước 4 : Chỉnh sửa trong wp_options

Làm tương tự như bước 3 bạn chạy câu lệnh sau :

SELECT * FROM `op2mro445_options` WHERE `option_name` LIKE '%wp_%'
câu lệnh trên sẽ liệt kê tất cả các field trong option_name sử dụng tiền tố wp_ mặc định, và sau khi hiển thị ra danh sách, bạn chọn từng cái và thay đổi wp_ bằng op2mro445_

Bước 5 : Chỉnh sửa trong wp_usermeta

Mục đích của bước này cũng như bước 4 và bạn thay đổi câu lệnh sql như sau :

SELECT * FROM op2mro445_usermeta WHERE meta_key LIKE '%wp_%'
và bạn cũng đổi những field sử dụng tiền tố mặc định wp_

Bước 6 : Sao lưu (Backup) lại và hoàn tất :

vậy là bạn đã hoàn thành việc đổi lại tiền tố (prefix) mặc định, bạn thử vào lại site xem có xảy ra vấn đề gì không, nếu không thì bạn backup lại dữ liệu, và ngồi rung đùi khỏi lo bị hack nữa ^^
Có nhiều cách hơn để thực hiện thủ thuật này , nếu bạn nào có cách hay hơn xin chia sẻ cho mình và mọi người nhá.

Chúc các bạn thành công !

14/9/17

Custom Trang Checkout Woocommerce Thành 2 Cột

Để Custom Trang Checkout Woocommerce Thành 2 Cột chúng ta cần lập trình đoạn code sau:
Remove và reorder và rename lại label của các field checkout

Sắp Xếp Sản Phẩm Trong Woocommerce Theo Ý Người Quản Trị

Để Sắp Xếp Sản Phẩm Trong Woocommerce Theo Ý Người Quản Trị: Chúng ta Thêm cột thứ tự vào bảng quản lý sản phẩm của woocommerce

Hướng Dẫn Custom Woocommerce – Phần 1

Hướng Dẫn Custom Woocommerce – Phần 1
Theme đã hỗ trợ woocommerce
Copy tất cả file trong folder templates nằm trong folder của plugin woocommerce vào folder theme hiện hành và đổi tên thành woocommerce

Tùy Biến Minicart Trong Woocommerce

Hôm nay tôi sẽ chia sẽ với anh chị cách tùy biến minicart trong woocommerce. Trên mạng có rất nhiều bài hướng dẫn, anh/chị có thể tìm hiểu, ở đây tôi chia sẽ theo cách làm của tôi.

Product data tabs trong woocommerce

Product data tabs trong woocommerce
Product data tabs trong woocommerce chính là thanh chức năng hiển thị description của product, reviews..
Như thế nào để anh chị tùy biến thanh này, hôm nay tôi sẽ chia sẽ các code để làm việc này
Xóa tab:
add_filter( ‘woocommerce_product_tabs’, ‘woo_remove_product_tabs’, 98 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs[‘description’] ); // Xóa  tab description
unset( $tabs[‘reviews’] ); // Xóa tab reviews
return $tabs;
}
Thay đổi tên tab:
add_filter( ‘woocommerce_product_tabs’, ‘woo_rename_tabs’, 98 );
function woo_rename_tabs( $tabs ) {
$tabs[‘description’][‘title’] = __( ‘Mô tả’ );
$tabs[‘reviews’][‘title’] = __( ‘Ý kiến khách hàng’ );
return $tabs;
}
Thay đổi thứ tự tab
add_filter( ‘woocommerce_product_tabs’, ‘woo_reorder_tabs’, 98 );
function woo_reorder_tabs( $tabs ) {
$tabs[‘reviews’][‘priority’] = 5; // Tab Reviews ở vị trí đầu tiên thay vì ở vị trí thứ 2
$tabs[‘description’][‘priority’] = 10; // Tab Description ở vị trí 1
return $tabs;
}
Thêm tab vào thanh tab
Điều này thì hữu ích khi ta muốn thêm một tab để hiển thị các image của sản phẩm chẳn hạn
add_filter( ‘woocommerce_product_tabs’, ‘image_product_tab’ );
function image_product_tab( $tabs ) {
//thêm vào tab để chứa image sản phẩm
$tabs[‘image_sp’] = array(
‘title’ => __( ‘Hình ảnh sản phẩm’, ‘woocommerce’ ),
‘priority’ => 50,
‘callback’ => ‘function_lay_image_sp’
);
return $tabs;
}
function function_lay_image_spt() {
// Viết code lấy image của sản phẩm tại đây
}
Chỉ sửa tab hiện có của woocommerce
add_filter( ‘woocommerce_product_tabs’, ‘custom_description_tab’, 98 );
function woo_custom_description_tab( $tabs ) {
$tabs[‘description’][‘callback’] = ‘function_lam_viec_nay’; // hàm để thay đổi cách hiển thị description
return $tabs;
}
function function_lam_viec_nay() {
// viết code làm điều này tại đây
}
Hị vọng sẽ giúp được anh chị qua bài viết Product data tabs trong woocommerce là gì?
Từ khóa tìm kiếm trên google:
-Product data tabs trong woocommerce là gì?
-Thay đổi tabs description trong woocommerce
-Thay đổi tabs review trong woocommerce
-Thêm tabs mới vào thanh tabs woocommerce
Nguồn:fcwordpress.net

22/8/17

10 WordPress Coupon Plugin trên cửa hàng thương mại điện tử

10 WordPress Coupon Plugin trên cửa hàng thương mại điện tử
Bạn đang mong muốn thúc đẩy bán hàng trên cửa hàng thương mại điện tử WordPress của bạn trong dịp lễ này? Coupons và Discounts (phiếu giảm giá) là cách tốt nhất để thu hút khách hàng mới cũng như khách hàng trung thành mua nhiều sản phẩm hơn. Để thêm vào các coupons một cách dễ dàng bạn có thể cài đặt WordPress Coupon Plugin.

1/12/16

WooCommerce Add to Cart with Ajax on Single Product Page

jax is fun. Most people prefer Ajax so that there is loading of pages involved and the user can see everything right in front of them without refreshing or redirecting to another page.

I will explain you step by step how you can add a product from your single product page with Ajax in WooCommerce. This a lengthy article so make sure you have some coffee with you.

Adding Cart Icon in the header

The first step is to add an icon in the header which will show your cart contents after the Ajax updates the cart.

You can pick up any image of a basket/cart or any font-awesome icon to display this. You can also display the cart count, that is the number of products currently in the cart and also show the amount.

Here is the code to show the cart items count and total in the header
<div class="cart-header">
<a class="cart-contents" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>">
<span class="icon icon-basket"></span>
<span class="cart-count-total"><!--?php echo sprintf ( _n( '%d', '%d', WC()--->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?></span>
</a>
</div>
 The icon-basket class can be used to display an icon from font-awesome or if you want to display an image there.

The cart-count-total class will contain the number of items that the user has in his/her shopping cart.

Add the necessary CSS to those classes the way you want them to look. I have used this on a theme so this is how it looks after adding that code.


We also need a drop down here for the cart items to show up so let’s add the drop down div to the code.
<div class="cart-header">
<a class="cart-contents" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>">
<span class="icon icon-basket"></span>
<span class="cart-count-total"><!--?php echo sprintf ( _n( '%d', '%d', WC()--->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?></span>
</a>

<div class="cart-dropdown">
<div class="cart-dropdown-inner">
<h4>Shopping Bag</h4>
</div>
</div>
</div>
We will also want to display the existing cart items here so we use the following code to display that along with the cart and checkout links

<div class="cart-header">
<a class="cart-contents" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>">
<span class="icon icon-basket"></span>
<span class="cart-count-total"><!--?php echo sprintf ( _n( '%d', '%d', WC()--->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?></span>
</a>

<div class="cart-dropdown">
<div class="cart-dropdown-inner">
<!--?php $items = WC()--->cart->get_cart();

if ($items) { ?>
<h4>Shopping Bag</h4>

<!--?php foreach($items as $item =--> $values) { 
$_product = $values['data']->post; ?>
<div class="dropdown-cart-wrap">
<div class="dropdown-cart-left">
<!--?php echo get_the_post_thumbnail( $values['product_id'], 'thumbnail' ); ?-->
</div>

<div class="dropdown-cart-right">
<h5><!--?php echo $_product--->post_title; ?></h5>
<p>Quantity: <!--?php echo $values['quantity']; ?--></p>
<p>Price: £ <!--?php echo get_post_meta($values['product_id'] , '_price', true); ?--></p>
</div>

<div class="clear"></div>
</div>
<!--?php } ?-->

<div class="dropdown-cart-wrap">
<div class="dropdown-cart-left">
<h6>Subtotal</h6>
</div>

<div class="dropdown-cart-right">
<h6><!--?php echo WC()--->cart->get_cart_total(); ?></h6>
</div>

<div class="clear"></div>
</div>

<div class="dropdown-cart-wrap dropdown-cart-last">
<div class="dropdown-cart-left dropdown-cart-link">
<a href="/cart/">View Basket</a>
</div>

<div class="dropdown-cart-right dropdown-checkout-link">
<a href="/checkout/">Checkout</a>
</div>

<div class="clear"></div>
</div>
<!--?php } else { ?-->
<h4>Shopping Bag</h4>

<div class="dropdown-cart-wrap">
<p>Your cart is empty.</p>
</div>

<div class="dropdown-cart-wrap">
<div class="dropdown-cart-left">
<h6>Subtotal</h6>
</div>

<div class="dropdown-cart-right">
<h6><!--?php echo WC()--->cart->get_cart_total(); ?></h6>
</div>

<div class="clear"></div>
</div>

<div class="dropdown-cart-wrap dropdown-cart-last">
<div class="dropdown-cart-left dropdown-cart-link">
<a href="/cart/">View Basket</a>
</div>

<div class="dropdown-cart-right dropdown-checkout-link">
<a href="/checkout/">Checkout</a>
</div>

<div class="clear"></div>
</div>
<!--?php } ?-->
</div>
</div>
</div>
After adding the above code, add the CSS that you want to add to style this dropdown. This is how the drop down looks on the theme that I am creating this on.


Now that the header is ready. Let’s begin the WooCommerce part.

Adding JS to the WooCommerce Add to Cart Button on Single Product page

By default if the user clicks on the add to cart button on the single product pages, that item is added to the cart and then a message is displayed below the header which says that the item has been added to the cart.

So let’s disable that altogether and create our own Ajax for that button

Place the following code in your JS file of your theme. If you don’t have one (which is very unlikely to happen) or if you are using a child theme, use wp_enqueue_script() to add a JS file to your theme. Also use wp_localize_script to get the admin-ajax.php URL in your Ajax call.

jQuery(document).ready(function($) {
$(".single_add_to_cart_button").click(function(e) {
e.preventDefault();

console.log('clicked');
});
});
The above code will basically disable the add to cart button on the single product page to function and just to test it will send a “clicked” text to the developer console of your browser. After adding the above code, if you head over to any single product page, you will see a message in your console, something like this..


jQuery(document).ready(function($) {
$(".single_add_to_cart_button").click(function(e) {
e.preventDefault();

var product_id = $('input[name="add-to-cart"]').val();
var quantity = $('input[name="quantity"]').val();
$('.cart-dropdown-inner').empty();

$.ajax ({
                    url: my_ajax_object.ajax_url,
                    type:'POST',
                    data:'action=my_wc_add_cart&product_id=' + product_id + '&quantity=1',

                    success:function(results) {
                        $('.cart-dropdown-inner').html(results);
                    }
               });
       });
});
If you have simple as well as variable products on your website, then use the following code.

jQuery(document).ready(function($) {
$(".single_add_to_cart_button").click(function(e) {
e.preventDefault();

var product_id = $('input[name="add-to-cart"]').val();
var variation_id = $('input[name="variation_id"]').val();
var quantity = $('input[name="quantity"]').val();
$('.cart-dropdown-inner').empty();

if (variation_id != '') {
$.ajax ({
                   url: my_ajax_object.ajax_url,
                   type:'POST',
                   data:'action=my_wc_add_cart&product_id=' + product_id + '&variation_id=' + variation_id + '&quantity=' + quantity,

                   success:function(results) {
                        $('.cart-dropdown-inner').append(results);
                   }
              });
} else {
$.ajax ({
                   url: my_ajax_object.ajax_url,  
                   type:'POST',
                   data:'action=my_wc_add_cart&product_id=' + product_id + '&quantity=' + quantity,

                   success:function(results) {
                       $('.cart-dropdown-inner').append(results);
                   }
       });
 }
    });
});
The product_id will pull in the ID of the product. quantity will get the quantity selected by the user. variation_id gets the variation ID of the item that is added to the cart. We also run a conditional tag on ‘variation_id’ so that we can pass the variation ID to the Ajax function. ‘.empty()’ is used to empty the contents of the drop down so that we can update it with new content. The ‘my_wc_add_cart’ is the function which will add the product to the cart and then send back the cart contents to update the drop down in the header.

The PHP side

Every theme should have a functions.php file. If you are using a child theme, then use the functions.php of the child theme and paste the following code there.

function my_wc_add_cart_ajax() {
$product_id = $_POST['product_id'];
$variation_id = $_POST['variation_id'];
$quantity = $_POST['quantity'];

if ($variation_id) {
WC()->cart->add_to_cart( $product_id, $quantity, $variation_id );
} else {
WC()->cart->add_to_cart( $product_id, $quantity);
}

$items = WC()->cart->get_cart(); ?>

<h4>Shopping Bag</h4>

<!--?php foreach($items as $item =--> $values) { 
$_product = $values['data']->post; ?>

<div class="dropdown-cart-wrap">
<div class="dropdown-cart-left">
<!--?php echo get_the_post_thumbnail( $values['product_id'], 'thumbnail' ); ?-->
</div>

<div class="dropdown-cart-right">
<h5><!--?php echo $_product--->post_title; ?></h5>
<p>Quantity: <!--?php echo $values['quantity']; ?--></p>
<p>Price: £ <!--?php echo get_post_meta($values['product_id'] , '_price', true); ?--></p>
</div>

<div class="clear"></div>
</div>
<!--?php } ?-->

<div class="dropdown-cart-wrap">
<div class="dropdown-cart-left">
<h6>Subtotal</h6>
</div>

<div class="dropdown-cart-right">
<h6><!--?php echo WC()--->cart->get_cart_total(); ?></h6>
</div>

<div class="clear"></div>
</div>

<div class="dropdown-cart-wrap dropdown-cart-last">
<div class="dropdown-cart-left dropdown-cart-link">
<a href="/cart/">View Basket</a>
</div>

<div class="dropdown-cart-right dropdown-checkout-link">
<a href="/checkout/">Checkout</a>
</div>

<div class="clear"></div>
</div>

<!--?php die();
}

add_action('wp_ajax_my_wc_add_cart', 'my_wc_add_cart_ajax');
add_action('wp_ajax_nopriv_my_wc_add_cart', 'my_wc_add_cart_ajax'); ?-->
We fire the ‘my_wc_add_cart’ function in the functions.php file which first pulls the item which was sent to it from the Ajax function. We then add the product to the cart along with the quantity and the variation which was added. Then we use the WC()->cart->get_cart() function to get the items in the cart and then create the HTML markup for the cart drop down. The above function will return the product image, title, quantity and the price of the products in the cart. In addition to that, it will also show the Subtotal (since total price is calculated after entering the shipping details) and the WooCommerce cart and checkout page links.

This is how my cart drop down looks after I added the CSS for that particular site.


Additionally, a spinner/loader can be added to the “Add to Cart” button and also the drop down can be displayed for a few seconds to show the user that the item has been added to the cart.

I have also displayed the attribute name which was added to the cart by the user. If you want to display that, use the following function to overwrite the item details function with this.

<div class="dropdown-cart-right">
<h5><!--?php echo $_product--->post_title; ?></h5>
<p>Quantity: <!--?php echo $values['quantity']; ?--></p>
<!--?php if($values['variation_id']) { ?-->
<p>Size: <!--?php $variation = wc_get_product($values['variation_id']);
$varname = $variation--->get_formatted_name();
preg_match('/(?<=size: )\S+/i', $varname, $match);
echo $match[0]; ?></p>
<!--?php } ?-->
<p>Price: £ <!--?php echo get_post_meta($values['product_id'] , '_price', true); ?--></p>
</div>
Feel free to comment or ask anything you want.