__('added successfully.','xoo-cp-domain'),
'updated' => __('updated successfully.','xoo-cp-domain'),
'removed' => __('removed from cart.','xoo-cp-domain'),
'undo' => __('Undo?','xoo-cp-domain')
);
wp_localize_script('xoo-cp-js','xoo_cp_localize',array(
'adminurl' => admin_url().'admin-ajax.php',
'enshop' => $xoo_cp_gl_atces_value,
'xcp_text' => json_encode($xoo_cp_js_text)
));
}
add_action('wp_enqueue_scripts','xoo_cp_enqueue_scripts',500);
//Get rounded total
function xoo_cp_round($number,$places = 2){
return round($number,$places);
}
//Get price with currency
function xoo_cp_with_currency($price){
$price = xoo_cp_round($price);
$format = get_option( 'woocommerce_currency_pos' );
$csymbol = get_woocommerce_currency_symbol();
switch ($format) {
case 'left':
$currency = $csymbol.$price;
break;
case 'left_space':
$currency = $csymbol.' '.$price;
break;
case 'right':
$currency = $price.$csymbol;
break;
case 'right_space':
$currency = $price.' '.$csymbol;
break;
default:
$currency = $csymbol.$price;
break;
}
return $currency;
}
//Popup HTML
function xoo_cp_popup(){
global $woocommerce;
$cart_url = $woocommerce->cart->get_cart_url();
$checkout_url = $woocommerce->cart->get_checkout_url();
?>
cart->add_to_cart($product_id,$quantity,$variation_id,$attribute_values );
}
elseif($variation_id === 0){
$cart_success = $woocommerce->cart->add_to_cart($product_id,$quantity);
}
//Successfully added to cart.
if($cart_success){
$cart_item_key = $cart_success;
$cart_data = $woocommerce->cart->get_cart();
$cart_item_data = $cart_data[$cart_item_key];
$item_cart_qty = $cart_item_data['quantity'];
if($variation_id){
$product = new WC_product_variation($variation_id);
$attributes = $product->get_formatted_variation_attributes();
}
else{
$product = new WC_product($product_id);
}
$product_title = $product->post->post_title;
$product_price = $product->get_price();
$product_image = $product->get_image('shop_thumbnail');
$is_sold_single = $product->is_sold_individually();
$product_total = $product_price*$item_cart_qty;
$html = '';
$html .= ''.__('Total','xoo-cp-domain').': '.xoo_cp_with_currency($product_total).'
';
$ajax_fragm = xoo_cp_ajax_fragments();
wp_send_json(array('pname' => $product_title , 'cp_html' => "$html" , 'ajax_fragm' => $ajax_fragm));
}
else{
if(wc_notice_count('error') > 0){
echo wc_print_notices();
}
}
die();
}
add_action('wp_ajax_xoo_cp_cart_ajax','xoo_cp_cart_ajax');
add_action('wp_ajax_nopriv_xoo_cp_cart_ajax','xoo_cp_cart_ajax');
//Ajax change in cart
function xoo_cp_change_ajax(){
global $woocommerce;
$cart_key = sanitize_text_field($_POST['cart_key']);
$new_qty = (int) $_POST['new_qty'];
$cart_data = $woocommerce->cart->get_cart();
$cart_item_data = $cart_data[$cart_key];
$item_cart_qty = $cart_item_data['quantity'];
$product_id = $cart_item_data['product_id'];
$variation_id = $cart_item_data['variation_id'];
if($variation_id){
$product = new WC_product_variation($variation_id);
}
else{
$product = new WC_product($product_id);
}
if($item_cart_qty > $new_qty){
$cart_success = $woocommerce->cart->set_quantity($cart_key,$new_qty);
}
else{
$additional_qty = $new_qty - $item_cart_qty;
if($variation_id){
$attribute_values = $cart_item_data['variation'];
$cart_success = $woocommerce->cart->add_to_cart($product_id,$additional_qty,$variation_id,$attribute_values );
}
else{
$cart_success = $woocommerce->cart->add_to_cart($product_id,$additional_qty);
}
}
if($cart_success){
if($new_qty == 0){
$ptotal = 0;
}
else{
$product_price = $product->get_price();
$new_cart_data = $woocommerce->cart->get_cart();
$new_cart_item_data = $new_cart_data[$cart_key];
$ptotal = xoo_cp_with_currency($new_qty*$product_price);
}
$ajax_fragm = xoo_cp_ajax_fragments();
$data = array('ptotal' => $ptotal ,'ajax_fragm' => $ajax_fragm);
wp_send_json($data);
}
else{
if(wc_notice_count('error') > 0){
echo wc_print_notices();
}
}
die();
}
add_action('wp_ajax_xoo_cp_change_ajax','xoo_cp_change_ajax');
add_action('wp_ajax_nopriv_xoo_cp_change_ajax','xoo_cp_change_ajax');
//Get Ajax refreshed fragments
function xoo_cp_ajax_fragments(){
// Get mini cart
ob_start();
woocommerce_mini_cart();
$mini_cart = ob_get_clean();
// Fragments and mini cart are returned
$data = array(
'fragments' => apply_filters( 'woocommerce_add_to_cart_fragments', array(
'div.widget_shopping_cart_content' => '' . $mini_cart . '
'
)
),
'cart_hash' => apply_filters( 'woocommerce_add_to_cart_hash', WC()->cart->get_cart_for_session() ? md5( json_encode( WC()->cart->get_cart_for_session() ) ) : '', WC()->cart->get_cart_for_session() )
);
return $data;
}
//Options Styles
function xoo_cp_styles(){
global $xoo_cp_sy_pw_value,$xoo_cp_sy_imgw_value,$xoo_cp_sy_btnbg_value,$xoo_cp_sy_btnc_value,$xoo_cp_sy_btns_value,$xoo_cp_sy_btnbr_value,$xoo_cp_sy_tbc_value,$xoo_cp_sy_tbs_value,$xoo_cp_gl_ibtne_value,$xoo_cp_gl_vcbtne_value,$xoo_cp_gl_chbtne_value,$xoo_cp_gl_qtyen_value;
$style = '';
if(!$xoo_cp_gl_ibtne_value || !$xoo_cp_gl_qtyen_value){
$style .= '.xcp-chng{
display: none;
}';
}
if(!$xoo_cp_gl_vcbtne_value){
$style .= 'a.xoo-cp-btn-vc{
display: none;
}';
}
if(!$xoo_cp_gl_chbtne_value){
$style .= 'a.xoo-cp-btn-ch{
display: none;
}';
}
echo "";
}
add_action('wp_head','xoo_cp_styles');