'1',
'enable_ship' => '',
'require_email' => '0',
'require_district' => '0',
'require_village' => '0',
'require_address' => '0',
'enable_location' => '',
'popup_infor_enable' => '1',
'in_loop_prod' => '0',
'button_text1' => 'Mua ngay',
'button_text2' => 'Gọi điện xác nhận và giao hàng tận nơi',
'popup_title' => 'Đặt mua %s',
'popup_gotothankyou' => '0',
'popup_mess' => 'Bạn vui lòng nhập đúng số điện thoại để chúng tôi sẽ gọi xác nhận đơn hàng trước khi giao hàng. Xin cảm ơn!',
'popup_sucess' => '
',
'popup_error' => 'Đặt hàng thất bại. Vui lòng đặt hàng lại. Xin cảm ơn!',
'out_of_stock_mess' => 'Hết hàng!',
'license_key' => ''
);
public static function init()
{
is_null(self::$instance) AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
$this->define_constants();
global $quickbuy_settings;
$quickbuy_settings = $this->get_dvlsoptions();
add_filter( 'plugin_action_links_' . DEVVN_QB_BASENAME, array( $this, 'add_action_links' ), 10, 2 );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_init', array( $this, 'dvls_register_mysettings') );
add_action( 'plugins_loaded', array($this,'dvls_load_textdomain') );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
if($quickbuy_settings['enable']){
add_action('wp_enqueue_scripts', array($this, 'load_plugins_scripts'));
add_shortcode('devvn_quickbuy', array($this, 'devvn_button_quick_buy'));
add_action('woocommerce_single_product_summary', array($this, 'add_button_quick_buy'), 35);
add_action('woocommerce_after_single_product', array($this, 'quick_buy_popup_content_single') );
add_action( 'wp_ajax_devvn_quickbuy', array($this,'devvn_quickbuy_func') );
add_action( 'wp_ajax_nopriv_devvn_quickbuy', array($this,'devvn_quickbuy_func') );
add_action( 'wp_ajax_devvn_form_quickbuy', array($this,'devvn_form_quickbuy_func') );
add_action( 'wp_ajax_nopriv_devvn_form_quickbuy', array($this,'devvn_form_quickbuy_func') );
add_action( 'wp_ajax_quickbuy_load_diagioihanhchinh', array($this, 'load_diagioihanhchinh_func') );
add_action( 'wp_ajax_nopriv_quickbuy_load_diagioihanhchinh', array($this, 'load_diagioihanhchinh_func') );
add_action('devvn_prod_variable','woocommerce_template_single_add_to_cart');
if($quickbuy_settings['in_loop_prod']) {
add_action('woocommerce_after_shop_loop_item', array($this, 'add_quick_buy_to_loop_func'), 15);
}
}
add_action( 'admin_notices', array($this, 'admin_notices') );
if( is_admin() ) {
add_action('in_plugin_update_message-' . DEVVN_QB_BASENAME, array($this,'devvn_modify_plugin_update_message'), 10, 2 );
}
include_once ('includes/updates.php');
}
public function define_constants()
{
if (!defined('DEVVN_QB_VERSION_NUM'))
define('DEVVN_QB_VERSION_NUM', $this->_version);
if (!defined('DEVVN_QB_URL'))
define('DEVVN_QB_URL', plugin_dir_url(__FILE__));
if (!defined('DEVVN_QB_BASENAME'))
define('DEVVN_QB_BASENAME', plugin_basename(__FILE__));
if (!defined('DEVVN_QB_PLUGIN_DIR'))
define('DEVVN_QB_PLUGIN_DIR', plugin_dir_path(__FILE__));
}
function dvls_load_textdomain() {
load_textdomain('devvn-quickbuy', dirname(__FILE__) . '/languages/devvn-quickbuy-' . get_locale() . '.mo');
}
function devvn_button_quick_buy($atts)
{
$atts = shortcode_atts( array(
'id' => '',
'view' => true,
'button_text1' => '',
'button_text2' => '',
'small_link' => false
), $atts, 'devvn_quickbuy' );
$id = $atts['id'];
$view = (bool) $atts['view'];
$button_text1 = $atts['button_text1'];
$button_text2 = $atts['button_text2'];
$small_link = (bool) $atts['small_link'];
global $quickbuy_settings;
if(!$id) {
global $product;
$this_product = $product;
}else {
$this_product = wc_get_product($id);
}
if($this_product) {
ob_start();
if (!is_admin() && $this_product->is_in_stock()):
if(!$small_link):
?>
quick_buy_popup_content($atts, false);
}
endif;
return ob_get_clean();
}
}
function devvn_form_quickbuy_func(){
$prodID = isset($_POST['prodid']) ? intval($_POST['prodid']) : '';
if($prodID){
$args = array(
'id' => $prodID,
);
$this->form_popup_content($args);
}
die();
}
function form_popup_content($args = array())
{
global $quickbuy_settings, $product;
if($args){
$prodID = isset($args['id']) ? intval($args['id']) : '';
if($prodID){
$thisProduct = wc_get_product($prodID);
if($thisProduct){
$require_email = $quickbuy_settings['require_email'];
?>
$product->get_id()
);
$this->quick_buy_popup_content($args, true);
}
function quick_buy_popup_content($args = array(), $view = true)
{
global $quickbuy_settings;
if($args){
$prodID = isset($args['id']) ? intval($args['id']) : '';
if($prodID){
$thisProduct = wc_get_product($prodID);
if($thisProduct){
?>
get_id().'"]');
}
function add_quick_buy_to_loop_func(){
global $product;
echo do_shortcode('[devvn_quickbuy small_link="1" id="'.$product->get_id().'"]');
}
function devvn_get_rates($package = array(), $product_info = array()){
$available_methods = $old_cart_key = array();
$package = wp_parse_args($package, array(
'country' => 'VN',
'state' => '01',
'city' => '',
'postcode' => '',
));
$old_cart = WC()->cart->get_cart_contents();
if($old_cart && !empty($old_cart)){
foreach($old_cart as $k=>$cartItem){
$old_cart_key[] = $k;
WC()->cart->remove_cart_item($k);
}
}
if($product_info && is_array($product_info) && !empty($product_info)){
$product_id = isset($product_info['product_id']) ? intval($product_info['product_id']) : '';
if(!$product_id) {
$product_id = isset($product_info['add-to-cart']) ? intval($product_info['add-to-cart']) : '';
}
$quantity = isset($product_info['quantity']) ? intval($product_info['quantity']) : 0;
$variation_id = isset($product_info['variation_id']) ? intval($product_info['variation_id']) : '';
if ($product_id) {
if ($variation_id && $variation_id != "" && $variation_id > 0) {
$variation = array();
foreach($product_info as $k=>$v){
if( strpos($k, 'attribute_') !== false ){
$variation[$k] = $product_info[$k];
}
}
$cart_item_key = WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variation);
} else {
$cart_item_key = WC()->cart->add_to_cart($product_id, $quantity);
}
WC()->customer->set_billing_location($package['country'],$package['state'],$package['postcode'],$package['city']);
WC()->customer->set_shipping_location($package['country'],$package['state'],$package['postcode'],$package['city']);
$packages = WC()->cart->get_shipping_packages();
$packages = WC()->shipping->calculate_shipping($packages);
$available_methods = WC()->shipping->get_packages();
$available_methods = isset($available_methods[0]['rates']) ? $available_methods[0]['rates'] : array();
WC()->cart->remove_cart_item($cart_item_key);
}
if($old_cart && !empty($old_cart)){
foreach($old_cart as $k=>$cartItem){
$product_id = isset($cartItem['product_id']) ? intval($cartItem['product_id']) : '';
$variation_id = isset($cartItem['variation_id']) ? intval($cartItem['variation_id']) : '';
$variation = isset($cartItem['variation']) ? wc_clean($cartItem['variation']) : array();
$quantity = isset($cartItem['quantity']) ? intval($cartItem['quantity']) : '';
if($product_id) {
if ($variation_id > 0) {
WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variation);
} else {
WC()->cart->add_to_cart($product_id, $quantity);
}
}
}
}
}
wc_clear_notices();
return $available_methods;
}
function check_product_incart($product_info = array())
{
$product_id = isset($product_info['product_id']) ? intval($product_info['product_id']) : '';
$variation_id = isset($product_info['variation_id']) ? intval($product_info['variation_id']) : '';
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product_id = isset($values['product_id']) ? $values['product_id'] : '';
$_variation_id = isset($values['variation_id']) ? $values['variation_id'] : '';
if (($product_id == $_product_id && $_variation_id == '') || ($product_id == $_product_id && $_variation_id && $variation_id == $_variation_id)) {
return $cart_item_key;
}
}
return false;
}
function get_productkey_incart($product_id)
{
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($product_id == $_product->id) {
return true;
}
}
return false;
}
function devvn_calculate_shipping($package = array(), $product_info = array()){
$available_methods = $this->devvn_get_rates($package, $product_info);
//print_r($available_methods);
ob_start();
?>
%5$s ',
0,
sanitize_title( $method->id ),
esc_attr( $method->id ),
($stt == 1) ? 'checked' : '',
wc_cart_totals_shipping_method_label( $method ),
sanitize_text_field($method->cost));
do_action( 'woocommerce_after_shipping_rate', $method, 0 );
?>
', 0, esc_attr( $method->id ), wc_cart_totals_shipping_method_label( $method ),sanitize_text_field($method->cost) );
do_action( 'woocommerce_after_shipping_rate', $method, 0 );
?>
devvn_calculate_shipping($package, $product_info);
}
if($getvalue == 1 && $matp){
$result['list_district'] = $address->get_list_district($matp);
wp_send_json_success($result);
}elseif($getvalue == 2 && $maqh){
$result['list_district'] = $address->get_list_village($maqh);
wp_send_json_success($result);
}
wp_send_json_error();
die();
}
function devvn_quickbuy_func(){
global $quickbuy_settings;
$prod_id = isset($_POST['prod_id']) ? intval($_POST['prod_id']) : '';
$prod_check = wc_get_product($prod_id);
if(!$prod_check || is_wp_error($prod_check)) wp_send_json_error();
parse_str($_POST['customer_info'], $customer_info);
parse_str($_POST['product_info'], $product_info);
$qty = isset($product_info['quantity']) ? (float) $product_info['quantity'] : 1;
$variation_id = isset($product_info['variation_id']) ? (float) $product_info['variation_id'] : '';
$product_id = isset($product_info['product_id']) ? (int) $product_info['product_id'] : '';
if(!$product_id) {
if(!isset($product_info['add-to-cart']) && $prod_id){
$product_info['add-to-cart'] = $prod_id;
}
$product_id = isset($product_info['add-to-cart']) ? (int)$product_info['add-to-cart'] : '';
}
$customer_gender = (isset($customer_info['customer-gender']) && $customer_info['customer-gender'] == 1) ? 'Anh' : 'Chị';
$customer_name = isset($customer_info['customer-name']) ? sanitize_text_field($customer_info['customer-name']): '';
$customer_email = isset($customer_info['customer-email']) ? sanitize_email($customer_info['customer-email']): '';
$customer_phone = isset($customer_info['customer-phone']) ? sanitize_text_field($customer_info['customer-phone']): '';
$customer_address = isset($customer_info['customer-address']) ? sanitize_textarea_field($customer_info['customer-address']): '';
$customer_location = isset($customer_info['customer-location']) ? sanitize_text_field($customer_info['customer-location']): '';
$customer_note = isset($customer_info['order-note']) ? sanitize_textarea_field($customer_info['order-note']): '';
$customer_quan = isset($customer_info['customer-quan']) ? sanitize_text_field($customer_info['customer-quan']): '';
$customer_xa = isset($customer_info['customer-xa']) ? sanitize_text_field($customer_info['customer-xa']): '';
$shipping_method = isset($customer_info['shipping_method']) ? $customer_info['shipping_method']: '';
$address = array(
'first_name' => $customer_gender,
'last_name' => $customer_name,
'email' => $customer_email,
'phone' => $customer_phone,
'address_1' => $customer_address,
'state' => $customer_location,
'city' => $customer_quan,
'address_2' => $customer_xa,
'country' => 'VN'
);
// Now we create the order
$order = wc_create_order();
if(!is_wp_error($order)) {
$args = $variation = array();
if($variation_id && is_array($product_info)){
foreach($product_info as $k=>$v){
if( strpos($k, 'attribute_') !== false ){
$variation[$k] = $product_info[$k];
}
}
if($variation){
$args = array(
'variation_id' => $variation_id,
'variation' => $variation,
'product_id' => $product_id
);
}
$prod_check = wc_get_product($variation_id);
}
$order->add_product($prod_check, $qty, $args);
$order->set_address($address, 'billing');
$order->set_address($address, 'shipping');
if($customer_note) {
if($this->check_woo_version()) {
$order->set_customer_note($customer_note);
}else {
wp_update_post( array( 'ID' => $order->id, 'post_excerpt' => $customer_note ) );
}
}
if($quickbuy_settings['enable_ship'] && $shipping_method) {
$item = new WC_Order_Item_Shipping();
$package['country'] = 'VN';
$package['state'] = sanitize_text_field($customer_location);
if($customer_quan) {
$package['city'] = sprintf("%03d", $customer_quan);
}
$available_methods = $this->devvn_get_rates($package, $product_info);
$shipping_rate = isset($available_methods[$shipping_method[0]]) ? $available_methods[$shipping_method[0]] : array();
if($shipping_rate) {
$item->set_props(array(
'method_title' => $shipping_rate->label,
'method_id' => $shipping_rate->id,
'total' => wc_format_decimal($shipping_rate->cost),
'taxes' => $shipping_rate->taxes,
'order_id' => $order->get_id(),
));
foreach ($shipping_rate->get_meta_data() as $key => $value) {
$item->add_meta_data($key, $value, true);
}
$item->save();
$order->add_item($item);
}
}
$order->calculate_totals();
$order->update_status("processing", 'Đơn hàng nhanh', TRUE);
if($this->check_woo_version()) {
wc_reduce_stock_levels($order->get_id());
}
$result['content'] = str_replace('%%order_id%%', $order->get_order_number(), $quickbuy_settings['popup_sucess']);
$result['gotothankyou'] = ($quickbuy_settings['popup_gotothankyou'] == 1 )? true : false;
$result['thankyou_link'] = $order->get_checkout_order_received_url();
do_action( 'woocommerce_new_order', $order->get_id() );
wp_send_json_success($result);
}
wp_send_json_error();
die();
}
public function check_woo_version($version = '3.0.0'){
if ( defined( 'WOOCOMMERCE_VERSION' ) && version_compare( WOOCOMMERCE_VERSION, $version, '>=' ) ) {
return true;
}
return false;
}
public function add_action_links($links, $file)
{
if (strpos($file, 'devvn-quick-buy.php') !== false) {
$settings_link = '' . __('Settings', 'devvn-quickbuy') . ' ';
array_unshift($links, $settings_link);
}
return $links;
}
function load_plugins_scripts()
{
global $quickbuy_settings;
wp_enqueue_style('devvn-quickbuy-style', plugins_url('css/devvn-quick-buy.css', __FILE__), array(), $this->_version, 'all');
wp_enqueue_script('jquery.validate', plugins_url('js/jquery.validate.min.js', __FILE__), array('jquery'), $this->_version, true);
wp_enqueue_script('bpopup', plugins_url('js/jquery.bpopup.min.js', __FILE__), array('jquery'), $this->_version, true);
wp_enqueue_script('devvn-quickbuy-script', plugins_url('js/devvn-quick-buy.js', __FILE__), array('jquery','bpopup', 'wc-add-to-cart-variation'), $this->_version, true);
$array = array(
'ajaxurl' => admin_url('admin-ajax.php'),
'siteurl' => home_url(),
'popup_error' => $quickbuy_settings['popup_error'],
'out_of_stock_mess' => $quickbuy_settings['out_of_stock_mess'],
'price_decimal' => wc_get_price_decimal_separator(),
'num_decimals' => wc_get_price_decimals(),
'currency_format' => get_woocommerce_currency_symbol()
);
wp_localize_script('devvn-quickbuy-script', 'devvn_quickbuy_array', $array);
}
public function admin_enqueue_scripts()
{
$current_screen = get_current_screen();
if (isset($current_screen->base) && $current_screen->base == 'settings_page_quickkbuy-setting') {
wp_enqueue_style('devvn-quickbuy-admin-styles', plugins_url('/css/admin-style.css', __FILE__), array(), $this->_version, 'all');
wp_enqueue_script('devvn-quickbuy-admin-js', plugins_url('/js/admin-jquery.js', __FILE__), array('jquery'), $this->_version, true);
}
}
function get_dvlsoptions()
{
return wp_parse_args(get_option($this->_optionName), $this->_defaultOptions);
}
function admin_menu()
{
add_options_page(
__('Quick Buy Setting', 'devvn-quickbuy'),
__('Quick Buy Setting', 'devvn-quickbuy'),
'manage_options',
'quickkbuy-setting',
array(
$this,
'devvn_settings_page'
)
);
}
function dvls_register_mysettings()
{
register_setting($this->_optionGroup, $this->_optionName);
}
function admin_notices(){
global $quickbuy_settings;
$class = 'notice notice-error';
$license_key = $quickbuy_settings['license_key'];
if(!$license_key) {
printf('Plugin Mua Hàng Nhanh: Hãy điền License Key để tự động cập nhật khi có phiên bản mới. Thêm tại đây
', esc_attr($class), esc_url(admin_url('options-general.php?page=quickkbuy-setting')));
}
}
function devvn_modify_plugin_update_message( $plugin_data, $response ) {
global $quickbuy_settings;
$license_key = sanitize_text_field($quickbuy_settings['license_key']);
if( $license_key && isset($plugin_data['package']) && $plugin_data['package']) return;
$PluginURI = isset($plugin_data['PluginURI']) ? $plugin_data['PluginURI'] : '';
echo ' ' . sprintf( __('Mua bản quyền để được tự động update. Xem thêm thông tin mua bản quyền hoặc liên hệ mua trực tiếp qua facebook ', 'devvn-quickbuy'), $PluginURI, 'http://m.me/levantoan.wp');
}
function devvn_settings_page()
{
global $quickbuy_settings;
?>