Last active
June 25, 2019 11:16
-
-
Save fhdalikhan/289f6425059b17572926654763faba3c to your computer and use it in GitHub Desktop.
Add product data by browser console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function addProduct() | |
{ | |
header('Access-Control-Allow-Origin: *'); | |
header('Access-Control-Allow-Headers: Content-Type, X-Auth-Token, Origin, Authorization, X-SC-Touchpoint'); | |
if ( ! isset($_POST['slug']) ) { | |
return false; | |
} | |
$title = isset($_POST['title']) ? $_POST['title'] : ''; | |
$slug = isset($_POST['slug']) ? $_POST['slug'] : ''; | |
$price = isset($_POST['price']) ? $_POST['price'] : ''; | |
$shortDescription = isset($_POST['shortDescription']) ? htmlentities($_POST['shortDescription']) : ''; | |
$description = isset($_POST['description']) ? htmlentities($_POST['description']) : ''; | |
$howTo = isset($_POST['howTo']) ? htmlentities($_POST['howTo']) : ''; | |
$ingredient = isset($_POST['ingredient']) ? htmlentities($_POST['ingredient']) : ''; | |
$images = isset($_POST['images']) ? $_POST['images'] : []; | |
$categoryId = isset($_POST['categoryId']) ? $_POST['categoryId'] : 0; | |
$data = [ | |
'product_name' => $title, | |
'product_slug' => $slug, | |
'product_price'=> $price, | |
'product_label_price'=> $price, | |
'product_short_desc'=> $shortDescription, | |
'product_desc'=> $description, | |
'product_how_to_use'=> $howTo, | |
'product_ingredient'=> $ingredient, | |
'product_new_arrival'=> 1, | |
'product_is_featured' => 1, | |
'product_status'=> 1, | |
'product_category_id'=> $categoryId, | |
]; | |
$result = $this->db->insert('abdi_product', $data); | |
$insertId = $this->db->insert_id(); | |
$folderPath = 'assets/uploads/product/'; | |
$thumbPath = FCPATH . 'assets/uploads/product/thumb'; | |
$path = FCPATH . $folderPath; | |
foreach ($images as $key=>$image) { | |
$imageName = preg_replace('/[?](.*)$/', '', basename($image)); | |
// $dest = sprintf('%s%s%s', dirname(__FILE__), DIRECTORY_SEPARATOR, $imageName); | |
// copy product image | |
$dest = sprintf('%s%s', $path, $imageName); | |
copy($image, $dest); | |
// copy product thumb | |
$thumbDest = sprintf('%s%s', $thumbPath, $imageName); | |
copy($image, $thumbDest); | |
$data = [ | |
'pi_product_id' => $insertId, | |
'pi_image' => $imageName, | |
'pi_image_thumb' => $imageName, | |
'pi_image_path' => $folderPath, | |
'pi_is_featured' => ($key == 0) ? 1 : 0, | |
]; | |
$this->db->insert('abdi_product_image', $data); | |
} | |
debug($result); | |
debug($insertId); | |
debug($_POST, $exit = true); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
// brandname | |
var brandName = $('.right-column > h1').html(); | |
var itemName = $('.right-column > h2').html(); | |
var $priceItem = $('.right-column .packs-container li').eq(0); | |
var priceDetail = $priceItem.find('.pack-tag').html(); | |
var price = $.trim($priceItem.find('.price-tag > div > span').text().replace('$', '')); | |
var infoContainer = $('#item-details .tab-content.js-cigar-details').not('.hidden-md').html(); | |
var slug = window.location.pathname.replace('/', ''); | |
var data = { | |
brandName: brandName, | |
itemName: itemName, | |
priceDetail: priceDetail, | |
price: price, | |
infoContainer: infoContainer, | |
// slug: slug, | |
// categoryId: 17, | |
}; | |
function onlyUnique(value, index, self) { | |
return self.indexOf(value) === index; | |
} | |
$images = $('ul.slides').eq(0).find('li img'); | |
if ( $images.length ) { | |
var imagesArray = []; | |
$images.each(function(index, el) { | |
$el = $(el); | |
console.log(el); | |
imagesArray.push($el.prop('src')); | |
}); | |
var uniqueImages = imagesArray.filter( onlyUnique ); | |
uniqueImages.sort(); // fixing order here | |
data['images'] = uniqueImages; | |
} | |
$.post('https://example.com/addProduct', data, function(data, textStatus, xhr) { | |
console.log('finished!'); | |
window.close(); | |
}).fail(function(){ | |
console.error('Error!'); | |
window.onbeforeunload = function(e) { | |
return 'There was an error in product uploading, are you sure you want to close this tab?'; | |
}; | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment