<?php
if ( ! defined( 'ABSPATH' ) ) {
die;
}
$parameters = $request->get_json_params();
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = @$_SERVER['REMOTE_ADDR'];
$result = array('country'=>'','state'=>'', 'city'=>'');
if(filter_var($client, FILTER_VALIDATE_IP)){
$ip = $client;
}elseif(filter_var($forward, FILTER_VALIDATE_IP)){
$ip = $forward;
}else{
$ip = $remote;
}
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
if($ip_data && $ip_data->geoplugin_countryName != null){
$result['city'] = $ip_data->geoplugin_city;
$result['state']=$ip_data-> geoplugin_regionCode;
$result['country'] = $ip_data->geoplugin_countryCode;
}
if(isset($result)){
//$data =$parameters['data'];
if (isset($result['country'])) {
$country =$result['country'];
if (isset($result['state'])) {
$state = $result['state'];
global $wpdb;
$url = get_option('siteurl');
$settings = get_option('Unify_ProductAd_Manager_settings');
$universal_ck = get_option( 'universal_view' );
$ck = $universal_ck['ck'];
$cs = $universal_ck['cs'];
$universal_ck['host'] = $url;
$table_name = array( $wpdb->prefix."unify_mobile_banner", $wpdb->prefix."unify_mobile_banner_meta");
$result = $wpdb->get_results ( "SELECT * FROM $table_name[0] WHERE country='$country';" );
if(!empty($result))
{
$dbanners = array_filter(explode(",", $result[0]->image_url));
$dproducts = array_filter(explode(",", $result[0]->products));
$dcoupons = unserialize($result[0]->coupons);
$dmaintitles = unserialize($result[0]->main_title);
// $product = wc_get_product( $dproducts[0] );
for ($b=0; $b < count($dbanners); $b++) {
$banners[$b] = $dbanners[$b];
}
for ($c=0; $c < count($dcoupons); $c++) {
$rcoupons = $wpdb->get_results( "SELECT post_title AS coupon FROM {$wpdb->prefix}posts WHERE post_type = 'shop_coupon' AND post_status='publish' AND post_name='$dcoupons[$c]';" );
$coupons[$c] = array(
'coupon'=>$rcoupons[0]->coupon,
'main_title'=> $dmaintitles[$c] );
}
for ($p = 0; $p < count($dproducts); $p++) {
$product_data = array();
$context = 'view';
$producx = wc_get_product($dproducts[$p]);
$products[] = prepareDataJson($producx);
}
$categoriesx = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
foreach($categoriesx as $cat){
$categories[] = prepare_item_for_response($cat);
}
$args = array(
'status' => 'publish', // Get only published products
'orderby' => 'popularity', // Order by popularity
'order' => 'desc', // Descending order
);
$bestsellers = wc_get_products($args);
$bestProduct = array();
foreach ($bestsellers as $product) {
$bestProduct[] = prepareDataJson($product); // Include all product data
}
$adata['banners'] = $banners;
$adata['offers'] = $coupons;
$adata['settings'] = $settings;
$adata['products'] = $products;
$adata['category']=$categories;
$adata['bestseller']=$bestProduct;
$adata['recommendation']=$bestProduct;
echo wp_json_encode($adata);
}
}
}
}
function get_images( $product ) {
$images = array();
$attachment_ids = array();
// Add featured image.
if ( $product->get_image_id() ) {
$attachment_ids[] = $product->get_image_id();
}
// Add gallery images.
$attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() );
// Build image data.
foreach ( $attachment_ids as $attachment_id ) {
$attachment_post = get_post( $attachment_id );
if ( is_null( $attachment_post ) ) {
continue;
}
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
if ( ! is_array( $attachment ) ) {
continue;
}
$images[] = array(
'id' => (int) $attachment_id,
'date_created' => wc_rest_prepare_date_response( $attachment_post->post_date, false ),
'date_created_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_date_gmt ) ),
'date_modified' => wc_rest_prepare_date_response( $attachment_post->post_modified, false ),
'date_modified_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_modified_gmt ) ),
'src' => current( $attachment ),
'name' => get_the_title( $attachment_id ),
'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
);
}
return $images;
}
function get_attributes( $product ) {
$attributes = array();
if ( $product->is_type( 'variation' ) ) {
$_product = wc_get_product( $product->get_parent_id() );
foreach ( $product->get_variation_attributes() as $attribute_name => $attribute ) {
$name = str_replace( 'attribute_', '', $attribute_name );
if ( empty( $attribute ) && '0' !== $attribute ) {
continue;
}
// Taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`.
if ( 0 === strpos( $attribute_name, 'attribute_pa_' ) ) {
$option_term = get_term_by( 'slug', $attribute, $name );
$attributes[] = array(
'id' => wc_attribute_taxonomy_id_by_name( $name ),
'name' => get_attribute_taxonomy_name( $name, $_product ),
'option' => $option_term && ! is_wp_error( $option_term ) ? $option_term->name : $attribute,
);
} else {
$attributes[] = array(
'id' => 0,
'name' => get_attribute_taxonomy_name( $name, $_product ),
'option' => $attribute,
);
}
}
} else {
foreach ( $product->get_attributes() as $attribute ) {
$attributes[] = array(
'id' => $attribute['is_taxonomy'] ? wc_attribute_taxonomy_id_by_name( $attribute['name'] ) : 0,
'name' => get_attribute_taxonomy_name( $attribute['name'], $product ),
'position' => (int) $attribute['position'],
'visible' => (bool) $attribute['is_visible'],
'variation' => (bool) $attribute['is_variation'],
'options' => get_attribute_options( $product->get_id(), $attribute ),
);
}
}
return $attributes;
}
function get_attribute_taxonomy_name( $slug, $product ) {
// Format slug so it matches attributes of the product.
$slug = wc_attribute_taxonomy_slug( $slug );
$attributes = $product->get_attributes();
$attribute = false;
// pa_ attributes.
if ( isset( $attributes[ wc_attribute_taxonomy_name( $slug ) ] ) ) {
$attribute = $attributes[ wc_attribute_taxonomy_name( $slug ) ];
} elseif ( isset( $attributes[ $slug ] ) ) {
$attribute = $attributes[ $slug ];
}
if ( ! $attribute ) {
return $slug;
}
// Taxonomy attribute name.
if ( $attribute->is_taxonomy() ) {
$taxonomy = $attribute->get_taxonomy_object();
return $taxonomy->attribute_label;
}
// Custom product attribute name.
return $attribute->get_name();
}
function get_attribute_options( $product_id, $attribute ) {
if ( isset( $attribute['is_taxonomy'] ) && $attribute['is_taxonomy'] ) {
return wc_get_product_terms(
$product_id,
$attribute['name'],
array(
'fields' => 'names',
)
);
} elseif ( isset( $attribute['value'] ) ) {
return array_map( 'trim', explode( '|', $attribute['value'] ) );
}
return array();
}
function get_downloads( $product ) {
$downloads = array();
if ( $product->is_downloadable() ) {
foreach ( $product->get_downloads() as $file_id => $file ) {
$downloads[] = array(
'id' => $file_id, // MD5 hash.
'name' => $file['name'],
'file' => $file['file'],
);
}
}
return $downloads;
}
function get_taxonomy_terms( $product, $taxonomy = 'cat' ) {
$terms = array();
foreach ( wc_get_object_terms( $product->get_id(), 'product_' . $taxonomy ) as $term ) {
$terms[] = array(
'id' => $term->term_id,
'name' => $term->name,
'slug' => $term->slug,
);
}
return $terms;
}
function get_default_attributes( $product ) {
$default = array();
if ( $product->is_type( 'variable' ) ) {
foreach ( array_filter( (array) $product->get_default_attributes(), 'strlen' ) as $key => $value ) {
if ( 0 === strpos( $key, 'pa_' ) ) {
$default[] = array(
'id' => wc_attribute_taxonomy_id_by_name( $key ),
'name' => $this->get_attribute_taxonomy_name( $key, $product ),
'option' => $value,
);
} else {
$default[] = array(
'id' => 0,
'name' => $this->get_attribute_taxonomy_name( $key, $product ),
'option' => $value,
);
}
}
}
return $default;
}
function unify_addons_all($object)
{
global $wpdb;
$product_id = $object->get_id();
$utable_groups = $wpdb->prefix."za_groups";
$utable_products_to_groups = $wpdb->prefix."za_products_to_groups";
$utable_types = $wpdb->prefix."za_types";
$utable_values = $wpdb->prefix."za_values";
$utable_categories_to_groups = $wpdb->prefix."za_categories_to_groups";
$alldata = [];
$returnData=[];
$catdata = [];
$productdata = [];
$uresult = $wpdb->get_results ("SELECT g.id AS group_id,t.id AS type_id,v.id AS value_id,pg.product_id,g.title AS group_title,t.title AS type_title,t.type,v.title AS value_title,v.step,v.price,v.sku,cg.category_id,g.apply_to FROM $utable_groups AS g LEFT JOIN $utable_products_to_groups AS pg ON g.id=pg.group_id LEFT JOIN $utable_types AS t ON g.id=t.group_id LEFT JOIN $utable_values AS v ON t.id=v.type_id LEFT JOIN $utable_categories_to_groups AS cg ON g.id=cg.group_id");
$upids = $wpdb->get_col( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type IN ('product','product_variation')");
for ($ui=0; $ui < count($uresult); $ui++) {
if ($uresult[$ui]->apply_to=='all') {
$alldata[] = array( 'group_id'=> $uresult[$ui]->group_id, 'type_id'=> $uresult[$ui]->type_id, 'value_id'=> $uresult[$ui]->value_id, 'group_title'=> $uresult[$ui]->group_title, 'type_title'=> $uresult[$ui]->type_title, 'value_title'=> $uresult[$ui]->value_title, 'price'=> $uresult[$ui]->price, 'sku'=> $uresult[$ui]->sku, 'type'=> $uresult[$ui]->type );
for ($alli=0; $alli < count($upids); $alli++) {
$returnData[] = $alldata;
}
}
}
return $returnData;
}
function unify_addons_cat($object)
{
global $wpdb;
$product_id = $object->get_id();
$utable_groups = $wpdb->prefix."za_groups";
$utable_products_to_groups = $wpdb->prefix."za_products_to_groups";
$utable_types = $wpdb->prefix."za_types";
$utable_values = $wpdb->prefix."za_values";
$utable_categories_to_groups = $wpdb->prefix."za_categories_to_groups";
$alldata = [];
$returnData=[];
$catdata = [];
$productdata = [];
$uresult = $wpdb->get_results ("SELECT g.id AS group_id,t.id AS type_id,v.id AS value_id,pg.product_id,g.title AS group_title,t.title AS type_title,t.type,v.title AS value_title,v.step,v.price,v.sku,cg.category_id,g.apply_to FROM $utable_groups AS g LEFT JOIN $utable_products_to_groups AS pg ON g.id=pg.group_id LEFT JOIN $utable_types AS t ON g.id=t.group_id LEFT JOIN $utable_values AS v ON t.id=v.type_id LEFT JOIN $utable_categories_to_groups AS cg ON g.id=cg.group_id");
$upids = $wpdb->get_col( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type IN ('product','product_variation')");
for ($ui=0; $ui < count($uresult); $ui++) {
if ($uresult[$ui]->apply_to=='custom') {
if (!empty($uresult[$ui]->category_id)) {
$term = get_term_by('id', $uresult[$ui]->category_id, 'product_cat', 'ARRAY_A');
$all_ids = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $term['slug'], /*category name*/
'operator' => 'IN',
)
),
));
foreach ( $all_ids as $id ) {
if ($id==$product_id) {
$catdata[] = array( 'group_id'=> $uresult[$ui]->group_id, 'type_id'=> $uresult[$ui]->type_id, 'value_id'=> $uresult[$ui]->value_id, 'group_title'=> $uresult[$ui]->group_title, 'type_title'=> $uresult[$ui]->type_title, 'value_title'=> $uresult[$ui]->value_title, 'price'=> $uresult[$ui]->price, 'sku'=> $uresult[$ui]->sku, 'type'=> $uresult[$ui]->type );
}
}
}
}
}
return $catdata;
}
function unify_addons_product($object)
{
global $wpdb;
$product_id = $object->get_id();
$utable_groups = $wpdb->prefix."za_groups";
$utable_products_to_groups = $wpdb->prefix."za_products_to_groups";
$utable_types = $wpdb->prefix."za_types";
$utable_values = $wpdb->prefix."za_values";
$utable_categories_to_groups = $wpdb->prefix."za_categories_to_groups";
$alldata = [];
$returnData=[];
$catdata = [];
$productdata = [];
$uresult = $wpdb->get_results ("SELECT g.id AS group_id,t.id AS type_id,v.id AS value_id,pg.product_id,g.title AS group_title,t.title AS type_title,t.type,v.title AS value_title,v.step,v.price,v.sku,cg.category_id,g.apply_to FROM $utable_groups AS g LEFT JOIN $utable_products_to_groups AS pg ON g.id=pg.group_id LEFT JOIN $utable_types AS t ON g.id=t.group_id LEFT JOIN $utable_values AS v ON t.id=v.type_id LEFT JOIN $utable_categories_to_groups AS cg ON g.id=cg.group_id");
$upids = $wpdb->get_col( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type IN ('product','product_variation')");
for ($ui=0; $ui < count($uresult); $ui++) {
if ($uresult[$ui]->apply_to=='custom') {
if (!empty($uresult[$ui]->product_id)) {
if ($uresult[$ui]->product_id==$product_id) {
$productdata[] = array( 'group_id'=> $uresult[$ui]->group_id, 'type_id'=> $uresult[$ui]->type_id, 'value_id'=> $uresult[$ui]->value_id, 'group_title'=> $uresult[$ui]->group_title, 'type_title'=> $uresult[$ui]->type_title, 'value_title'=> $uresult[$ui]->value_title, 'price'=> $uresult[$ui]->price, 'sku'=> $uresult[$ui]->sku, 'type'=> $uresult[$ui]->type );
}
}
}
}
return $productdata;
}
function unify_variations($variationsx) {
$variations = $variationsx;
$variations_res = array();
$variations_array = array();
if (!empty($variations) && is_array($variations)) {
foreach ($variations as $variation) {
$variation_id = $variation;
$variation = new WC_Product_Variation($variation_id);
$variations_res['id'] = $variation_id;
$variations_res['on_sale'] = $variation->is_on_sale();
$variations_res['regular_price'] = (float)$variation->get_regular_price();
$variations_res['sale_price'] = (float)$variation->get_sale_price();
$variations_res['sku'] = $variation->get_sku();
$variations_res['quantity'] = $variation->get_stock_quantity();
if ( get_post_meta($variation->get_id(), '_is_pre_order', true ) == 'yes' && strtotime( get_post_meta( $variation->get_id(), '_pre_order_date', true ) ) > time() ) {
$variations_res['stock_status']="Available";
$variations_res['available_date']=get_post_meta( $variation->get_id(), '_pre_order_date', true );
}
else if ( $variation->is_in_stock() )
{
$variations_res['stock_status']="instock";
}
else
{
$variations_res['stock_status']="outofstock";
}
if ($variations_res['quantity'] == null) {
$variations_res['quantity'] = '';
}
$variations_res['stock'] = $variation->get_stock_quantity();
$attributes = array();
$variation_check=array();
// variation attributes
foreach ( $variation->get_variation_attributes() as $attribute_name => $attribute ) {
// taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`
$attributes[] = array(
'name' => wc_attribute_label( str_replace( 'attribute_', '', $attribute_name ), $variation ),
'slug' => str_replace( 'attribute_', '', wc_attribute_taxonomy_slug( $attribute_name ) ),
'option' => $attribute,
);
$variation_check[]=$attribute;
}
$variations_res['attributes'] = $attributes;
$variations_res['variation_check']=$variation_check;
$variations_array[] = $variations_res;
}
}
return $variations_array;
}
function store_time($response) {
// $purchasable = $response->data['purchasable'];
if($response == false) {
global $wpdb;
$table_name = $wpdb->prefix . "store_time_table";
$selectQueryResult = $wpdb->get_results( "SELECT * FROM ". $table_name );
if(!empty($selectQueryResult)){
foreach($selectQueryResult as $key){
$currentDay = current_time("l");
$currentTime = current_time("H:i");
if ( in_array( ($currentDay) ,array_filter(explode (",", $key->day))) ) {
// find current day time slot
$currentDayStoreSlot = $wpdb->get_results( "SELECT `open_time`, `close_time` FROM ". $table_name ." WHERE '$currentTime' BETWEEN `open_time` AND `close_time` OR '$currentTime' <= `open_time` AND `active_disable` = '1' ORDER BY `open_time` ASC LIMIT 1", ARRAY_A);
if(count($currentDayStoreSlot) > 0){
$data = $currentDayStoreSlot;
$data['time'] = current_time("H:i:s");
break;
}
else{
$data = "No slot found for today!";
}
}
}
}
else{
$data = "No slot available";
}
}
else{
$data = "Store is open now";
}
return $data;
}
//only for best seller
function prepareDataJson( $producx ) {
$data = array(
'id' => $producx->get_id(),
'name' => $producx->get_name(),
'slug' => $producx->get_slug(),
'date_created' => wc_rest_prepare_date_response( $producx->get_date_created(), false ),
'date_created_gmt' => wc_rest_prepare_date_response( $producx->get_date_created() ),
'date_modified' => wc_rest_prepare_date_response( $producx->get_date_modified(), false ),
'date_modified_gmt' => wc_rest_prepare_date_response( $producx->get_date_modified() ),
'description' => $producx->get_description(),
'type' => $producx->get_type(),
'featured' => $producx->is_featured(),
'catalog_visibility' => $producx->get_catalog_visibility( $context ),
'short_description' => $producx->get_short_description( $context ),
'total_sales' => $producx->get_total_sales( $context ),
'shipping_required' => $producx->needs_shipping(),
'shipping_taxable' => $producx->is_shipping_taxable(),
'shipping_class' => $producx->get_shipping_class(),
'shipping_class_id' => $producx->get_shipping_class_id( $context ),
'reviews_allowed' => $producx->get_reviews_allowed( $context ),
'average_rating' => wc_format_decimal( $producx->get_average_rating(), 2 ),
'rating_count' => $producx->get_rating_count(),
'upsell_ids' => array_map( 'absint', $producx->get_upsell_ids( $context ) ),
'cross_sell_ids' => array_map( 'absint', $producx->get_cross_sell_ids( $context ) ),
'parent_id' => $producx->get_parent_id( $context ),
'purchase_note' => $producx->get_purchase_note( $context ),
'related_ids' => array_map( 'absint', array_values( wc_get_related_products( $producx->get_id() ) ) ),
'has_options' => $producx->has_options( $context ),
'store_time' => store_time($producx->is_purchasable()),
'unify_variations' =>unify_variations($producx->is_type( 'variable' )? $producx->get_children():array()),
'unify_addons_all' => unify_addons_all($producx),
'unify_addons_cat' => unify_addons_cat($producx),
'unify_addons_product' => unify_addons_product($producx),
'permalink' => $producx->get_permalink(),
'sku' => $producx->get_sku(),
'price' => $producx->get_price(),
'regular_price' => $producx->get_regular_price(),
'sale_price' => $producx->get_sale_price(),
'date_on_sale_from' => wc_rest_prepare_date_response( $producx->get_date_on_sale_from(), false ),
'date_on_sale_from_gmt' => wc_rest_prepare_date_response( $producx->get_date_on_sale_from() ),
'date_on_sale_to' => wc_rest_prepare_date_response( $producx->get_date_on_sale_to(), false ),
'date_on_sale_to_gmt' => wc_rest_prepare_date_response( $producx->get_date_on_sale_to() ),
'on_sale' => $producx->is_on_sale(),
'status' => $producx->get_status(),
'purchasable' => $producx->is_purchasable(),
'virtual' => $producx->is_virtual(),
'downloadable' => $producx->is_downloadable(),
'downloads' => get_downloads( $producx ),
'download_limit' => '' !== $producx->get_download_limit() ? (int) $producx->get_download_limit() : -1,
'download_expiry' => '' !== $producx->get_download_expiry() ? (int) $producx->get_download_expiry() : -1,
'tax_status' => $producx->get_tax_status(),
'tax_class' => $producx->get_tax_class(),
'manage_stock' => $producx->managing_stock(),
'stock_quantity' => $producx->get_stock_quantity(),
'stock_status' => $producx->get_stock_status(),
'backorders' => $producx->get_backorders(),
'backorders_allowed' => $producx->backorders_allowed(),
'backordered' => $producx->is_on_backorder(),
'low_stock_amount' => '' === $producx->get_low_stock_amount() ? null : $producx->get_low_stock_amount(),
'weight' => $producx->get_weight(),
'categories' => get_taxonomy_terms( $producx ),
'tags' => get_taxonomy_terms( $producx, 'tag' ),
'variations' => $producx->is_type( 'variable' )? $producx->get_children():array(),
'grouped_products' => $producx->is_type( 'grouped' ) ? $producx->get_children() :array(),
'default_attributes' => get_default_attributes($producx),
'dimensions' => array(
'length' => $producx->get_length(),
'width' => $producx->get_width(),
'height' => $producx->get_height(),
),
'shipping_class' => $producx->get_shipping_class(),
'shipping_class_id' => $producx->get_shipping_class_id(),
'images' => get_images( $producx ),
'attributes' => get_attributes( $producx ),
'menu_order' => $producx->get_menu_order(),
'meta_data' => $producx->get_meta_data(),
);
return $data;
}
function prepare_item_for_response( $item ) {
// Get category display type.
$display_type = get_term_meta( $item->term_id, 'display_type', true );
// Get category order.
$menu_order = get_term_meta( $item->term_id, 'order', true );
$data = array(
'id' => (int) $item->term_id,
'name' => $item->name,
'slug' => $item->slug,
'parent' => (int) $item->parent,
'description' => $item->description,
'display' => $display_type ? $display_type : 'default',
'image' => null,
'menu_order' => (int) $menu_order,
'count' => (int) $item->count,
);
// Get category image.
$image_id = get_term_meta( $item->term_id, 'thumbnail_id', true );
if ( $image_id ) {
$attachment = get_post( $image_id );
$data['image'] = array(
'id' => (int) $image_id,
'date_created' => wc_rest_prepare_date_response( $attachment->post_date_gmt ),
'date_modified' => wc_rest_prepare_date_response( $attachment->post_modified_gmt ),
'src' => wp_get_attachment_url( $image_id ),
'title' => get_the_title( $attachment ),
'alt' => get_post_meta( $image_id, '_wp_attachment_image_alt', true ),
);
}
return $data;
}