0% found this document useful (0 votes)
32 views

Functionsby Zeeshan

The document contains PHP code to retrieve product data from the database based on city and area cookies, prepare the data to create variable products and their variations in WordPress, and insert the products and variations into the database. It performs queries to get product IDs, loops through the results to build arrays of attribute data and product data, encodes it as JSON, then decodes it to pass to functions that insert the variable products, variations, and attributes into WordPress and WooCommerce.

Uploaded by

Hareem Vekriwala
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Functionsby Zeeshan

The document contains PHP code to retrieve product data from the database based on city and area cookies, prepare the data to create variable products and their variations in WordPress, and insert the products and variations into the database. It performs queries to get product IDs, loops through the results to build arrays of attribute data and product data, encodes it as JSON, then decodes it to pass to functions that insert the variable products, variations, and attributes into WordPress and WooCommerce.

Uploaded by

Hareem Vekriwala
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

function attributes_test($attribute){

$data = [];
foreach($attribute as $key => $att){

$data[] = [
'attributes' => ['pack' => $att->PackDesc],
'stock' => $att->quantity,
'price' => $att->price,
];
}

return $data;
}

global $wpdb;
$city = $_COOKIE['city'];
$area = $_COOKIE['area_name'];
$query = $wpdb->get_results("SELECT distinct p.item_id from product as p join price
as pp on p.item_id=pp.item_id join City as c on c.CityCode=pp.CityCode
join pack as pa on pp.PackDesc=pa.id join inventory as i on
i.PackDesc=pa.id AND i.item_id=p.item_id where pp.CityCode='" . $city . "' AND
i.area_id = '".$area."'");

$variation = [];
$items = [];
// $available_attributes = [];
$attributes = [];
foreach($query as $key => $_data){
$item_id = $_data->item_id;

$query_pack = $wpdb->get_results("SELECT p.*, pp.price, pa.PackDesc, i.quantity


from product as p join price as pp on p.item_id=pp.item_id join City as c on
c.CityCode=pp.CityCode
join pack as pa on pp.PackDesc=pa.id join inventory as i on
i.PackDesc=pa.id AND i.item_id=p.item_id where pp.CityCode='" . $city . "' AND
i.area_id = '".$area."' AND p.item_id = '". $item_id."' ");

$attributes = attributes_test($query_pack) ;

$items[] = [
'item_name' => $query_pack[0]->item_name,
'sku'=> $query_pack[0]->item_id,
'available_attributes' => 'Pack',
'variations' => $attributes
];
// }
// $product = wc_get_product($item_id);
//var_dump($query);
}

$json = json_encode($items);
$products_data = json_decode($json, true);

$_COOKIE["attributes"] = $attributes;
//var_dump($products_data);

/**
* Create a product variation for a defined variable product ID.
*
* @since 3.0.0
* @param int $product_id | Post ID of the product parent variable product.
* @param array $variation_data | The data to insert in the product.
*/

function create_product_variation( $product_id, $products_data ){

// Get the Variable product object (parent)

$_pf = new WC_Product_Factory();

$product = $_pf->get_product( $product_id );

$variation_post = array(
'post_title' => $product->get_name(),
'post_name' => 'product-'.$product_id.'-variation',
'post_status' => 'publish',
'post_parent' => $product_id,
'post_type' => 'product_variation',
'guid' => $product->get_permalink()
);

// Creating the product variation


$variation_id = wp_insert_post( $variation_post );

// Get an instance of the WC_Product_Variation object


$variation = new WC_Product_Variation( $variation_id );

// Iterating through the variations attributes


foreach ($_COOKIE["attributes"] as $attribute => $term_name )
{

$taxonomy = 'pa_'.array_keys($term_name["attributes"])[0]; // The attribute


taxonomy

// If taxonomy doesn't exists we create it (Thanks to Carl F. Corneil)


if( ! taxonomy_exists( $taxonomy ) ){
register_taxonomy(
$taxonomy,
'product_variation',
array(
'hierarchical' => false,
'label' => ucfirst( array_keys($term_name["attributes"])[0] ),
'query_var' => true,
'rewrite' => array( 'slug' =>
sanitize_title(array_keys($term_name["attributes"])[0]) ), // The base slug
)
);
}
// var_dump($term_name["attributes"]["pack"]);die();
// Check if the Term name exist and if not we create it.
if( ! term_exists( $term_name["attributes"]["pack"], $taxonomy ) )
wp_insert_term( $term_name["attributes"]["pack"], $taxonomy ); //
Create the term
$term_slug = get_term_by('name', $term_name["attributes"]["pack"],
$taxonomy )->slug; // Get the term slug

// Get the post Terms names from the parent variable product.
$post_term_names = wp_get_post_terms( $product_id, $taxonomy,
array('fields' => 'names') );

// Check if the post term exist and if not we set it in the parent variable
product.
if( ! in_array( $term_name["attributes"]["pack"], $post_term_names ) )
wp_set_post_terms( $product_id, $term_name["attributes"]["pack"],
$taxonomy, true );

// Set/save the attribute data in the product variation


update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );
}

// SKU
if( ! empty( $variation_data['sku'] ) )
$variation->set_sku( $variation_data['sku'] );

// Prices
if( empty( $variation_data['sale_price'] ) ){
$variation->set_price( $variation_data['regular_price'] );
} else {
$variation->set_price( $variation_data['sale_price'] );
$variation->set_sale_price( $variation_data['sale_price'] );
}
$variation->set_regular_price( $variation_data['regular_price'] );

// Stock
if( ! empty($variation_data['stock_qty']) ){
$variation->set_stock_quantity( $variation_data['stock_qty'] );
$variation->set_manage_stock(true);
$variation->set_stock_status('');
} else {
$variation->set_manage_stock(false);
}

$variation->set_weight('');

$variation->save();
}

function insert_products ($products)


{
if (!empty($products))
{
array_map('insert_product', $products);
}
}

// insert_products($products_data);
function insert_product($product_data)
{

$post = array( // Set up the basic post data to insert for our product

'post_author' => 1,
'post_content' => $product_data['item_name'],
'post_status' => 'publish',
'post_title' => $product_data['item_name'],
'post_parent' => '',
'post_type' => 'product'
);

// var_dump($post);
$post_id = wp_insert_post($post);
// var_dump($post_id);

if (!$post_id)
{
return false;
}

update_post_meta($post_id, '_sku', $product_data['item_id']);


update_post_meta( $post_id,'_visibility','visible');

wp_set_object_terms($post_id, 'test', 'product_cat');


wp_set_object_terms($post_id, 'variable', 'product_type', true);

create_product_variation($post_id,$products_data);

//insert_product_attributes($post_id, $product_data['available_attributes'],
$product_data['variations']);
//insert_product_variations($post_id, $product_data['variations']);
}

// function insert_product_attributes ($post_id, $available_attributes,


$variations)
// {

// // var_dump($available_attributes);
// foreach ($available_attributes as $attribute)
// {
// // var_dump($variations);

// $values = array();

// foreach ($variations as $variation)


// {
// $attribute_keys = array_keys($variation['attributes']);
// // var_dump($attribute_keys);
// foreach ($attribute_keys as $key)
// {
// // var_dump($key);
// if ($key === $attribute)
// {
// $values[] = $variation['attributes'][$key];

// // var_dump($values);
// }
// }
// }
// $values = array_unique($values);

// // var_dump($values);
// wp_set_object_terms($post_id, $values, 'pa_' . $attribute);
// }

// $product_attributes_data = array();

// foreach ($available_attributes as $attribute)


// {
// var_dump($attribute);
// $product_attributes_data['pa_'.$attribute] = array(

// 'name' => 'pa_'.$attribute,


// 'value' => '',
// 'is_visible' => '1',
// 'is_variation' => '1',
// 'is_taxonomy' => '1'

// );
// }

// update_post_meta($post_id, '_product_attributes', $product_attributes_data);

// }

// function insert_product_variations ($post_id, $variations)


// {
// foreach ($variations as $index => $variation)
// {
// $variation_post = array(

// 'post_title' => 'Variation #'.$index.' of '.count($variations).'


for product#'. $post_id,
// 'post_name' => 'product-'.$post_id.'-variation-'.$index,
// 'post_status' => 'publish',
// 'post_parent' => $post_id,
// 'post_type' => 'product_variation',
// 'guid' => home_url() . '/?product_variation=product-' .
$post_id . '-variation-' . $index
// );

// $variation_post_id = wp_insert_post($variation_post);

// foreach ($variation['attributes'] as $attribute => $value)


// {
// $attribute_term = get_term_by('name', $value, 'pa_'.$attribute);
// update_post_meta($variation_post_id, 'attribute_pa_'.$attribute,
$attribute_term->slug);

// }

// update_post_meta($variation_post_id, '_price', $variation['price']);


// update_post_meta($variation_post_id, '_regular_price',
$variation['price']);
// }
// }

You might also like