Skip to content

Instantly share code, notes, and snippets.

@Alexander-Pop
Alexander-Pop / check-if-valid-url.php
Last active February 1, 2019 16:55 — forked from thagxt/check-if-valid-url.php
check if url is valid #php #url
<?php
/* @ http://stackoverflow.com/a/12628971 */
function isValidUrl($url){
// first do some quick sanity checks:
if(!$url || !is_string($url)){
return false;
}
// quick check url is roughly a valid http request: ( http://blah/... )
if( ! preg_match('/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i', $url) ){
return false;
@Alexander-Pop
Alexander-Pop / url-check.php
Last active February 1, 2019 16:56 — forked from thagxt/url-check.php
php check headers status going thru all page statuses #php #url
<?php
/* @ http://stackoverflow.com/a/12628971 */
function getHttpResponseCode_using_getheaders($url, $followredirects = true){
// returns string responsecode, or false if no responsecode found in headers (or url does not exist)
// NOTE: could potentially take up to 0-30 seconds , blocking further code execution (more or less depending on connection, target site, and local timeout settings))
// if $followredirects == false: return the FIRST known httpcode (ignore redirects)
// if $followredirects == true : return the LAST known httpcode (when redirected)
if(! $url || ! is_string($url)){
return false;
@Alexander-Pop
Alexander-Pop / rename-file.php
Last active February 1, 2019 16:56 — forked from thagxt/rename-file.php
Quickly rename a file name with PHP. w/ error handling #php #file
<?php
$oldname = "index.html";
$newname = "index.php";
// checking if file exist or not.
if ( file_exists($oldname) && ( (!file_exists($newname))|| is_writable($newname) ) ) {
$renameResult = rename($oldname, $newname);
die($oldname . " renamed to " . $newname);
} else {
die('nothing to rename.');
@Alexander-Pop
Alexander-Pop / magento-BulkSKUpdate.php
Last active February 1, 2019 16:58
Programmatically Updating SKUs in Bulk on Magento #magento
<?php
/*
1) Create a CSV File with Before and After SKUs.
- In the first column, list your current SKUs and in the second column list the new SKUs.
- Do not include headings in your CSV file.
- Be sure this file is saved as a CSV file in the UTF-8 or ANSI encoding. You might run into problems with this if you create the file using Excel.
2) Upload the CSV file to the var/export directory on your Magento server so that it’s path is /var/export/sku2sku.csv.
3) Run the Script
+ If you run into the following error, don’t worry too much. Just re-run the script and see if more SKUs get updated.
- Cannot retrieve products from Magento: SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction
@Alexander-Pop
Alexander-Pop / php-read-write-scan.php
Last active June 28, 2019 14:07
PHP: Read Write File, Read Scan Directory #php #crud
<?php
/*PHP code to:
1) Read / Scan any directory
2) Read any file
3) Write to any file
1) Read or Scan directory
Get all files and folders inside the directory.
Three ways are shown below.
@Alexander-Pop
Alexander-Pop / m-tax-rate.html
Created May 22, 2019 19:28
Generates tax rate table for magento import
Tax rate: <input id='taxrate' /> <input type='button' id='selecttable' value='Select content' />
<div id='result'>
</div>
<script>
var fields = [
'Code', 'Country', 'State', 'Zip/Post Code',
'Rate', 'Zip/Post is Range', 'Range From',
@Alexander-Pop
Alexander-Pop / magento-tax-rules-rates.php
Last active May 30, 2019 21:36
Magento - programmatically Creating a Tax Rate, Tax Rule, Editing Tax Rate
Programmatically Creating a Tax Rate
(for the state of North Carolina(NC). You can modify it as needed to create a rate:
/* Helper Function for Getting a Region ID from a state code(NC) */
function get_region_id_from_state($state){
$region_data = Mage::getModel('directory/region')->loadByCode($state, 'NC');
@Alexander-Pop
Alexander-Pop / moveElementInArray.php
Last active June 2, 2019 21:05
PHP - Move one element before another in an associative array
<?php
function moveArrayKeyBefore($arr, $find, $move) {
if (!isset($arr[$find], $arr[$move])) {
return $arr;
}
$elem = [$move=>$arr[$move]]; // cache the element to be moved
$start = array_splice($arr, 0, array_search($find, array_keys($arr)));
unset($start[$move]); // only important if $move is in $start
return $start + $elem + $arr;
@Alexander-Pop
Alexander-Pop / database-query-in-magento.php
Created June 11, 2019 15:01
Magento - Database Query
<?php
$sql = "SELECT sku FROM catalog_product_entity";
$data = Mage::getSingleton(‘core/resource’) ->getConnection(‘core_read’)->fetchAll($sql);
if(!$data){
echo 'empty';
}else{
foreach ($data as $data){
echo $data['sku'].'Some description';
}
@Alexander-Pop
Alexander-Pop / magento-get-set-session-variable.php
Last active June 11, 2019 15:29
Magento - set/get Session Variable
<?php
/*
* create new session variable foo_data
* You have two options, for the session name, "frontend" for frontend session or "adminhtml" for admin session
*/
Mage::getSingleton("core/session", array("name" => "frontend"));
// This will get the session of the current logged in user.
$session = Mage::getSingleton('customer/session');