Skip to content

Instantly share code, notes, and snippets.

View tuanphpvn's full-sized avatar

christian tuanphpvn

View GitHub Profile
@tuanphpvn
tuanphpvn / MAGENTO - add Random product to category.php
Last active August 3, 2016 15:29
Random n product to category #Magento, #Category, #Product, #CreateData
<?php
$addRamdomProductToCategory = function($n, $arrCategoryId) {
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$productModel = Mage::getModel('catalog/product');
$productIds = Mage::getModel('catalog/product')->getCollection()->getAllIds();
foreach($arrCategoryId as $catId) {
$arrProductIdIndexs = array_rand($productIds, $n);
@tuanphpvn
tuanphpvn / MAGENTO - Set attribute for category by level.php
Last active August 4, 2016 00:29
Add display setting for category #Magento #Category #DisplaySettings
<?php
/**
* RootCategoryName is the name of root for store.
* Level: Level 1 child of root, Level2 child of level 1, Level is relative with rootCategoryLevel
* Display Mode:
*
* @param $rootCategoryName
* @param $level
* @param $displayMode
@tuanphpvn
tuanphpvn / MAGENTO - get -all id of parent cateogry.php
Last active August 4, 2016 00:30
Get all category id by Level 2 (Level -1 is root category)
<?php
$getAllIdOfRootCategory = function($catname) {
$rootCategoryObj = Mage::getModel('catalog/category')->loadByAttribute('name', $catname);
$collection = Mage::getModel('catalog/category')
->getCollection()
->addPathsFilter($rootCategoryObj->getPath().'/');
$arr = array();
foreach($collection as $catObj) {
@tuanphpvn
tuanphpvn / MAGENTO - create category recursive.php
Last active August 4, 2016 00:36
Create list category recursive #Magento #Category #AutoWork
<?php
/**
* Note that this function do not support:
* - Create root category if it is not exits
* - The category name must do not duplicate in same root category
* - The name of root category must unique
* @param $arrKey
* @param $rootCategoryName
* @throws Exception
*/
@tuanphpvn
tuanphpvn / delete all children of the category.php
Last active August 4, 2016 00:37
Delete all category Level 2 (Root is Level 1)
<?php
$deleteAllCatChildren = function($catname) {
$rootCategoryObj = Mage::getModel('catalog/category')->loadByAttribute('name', $catname);
$collection = Mage::getModel('catalog/category')
->getCollection()
->addPathsFilter($rootCategoryObj->getPath().'/');
foreach($collection as $catObj) {
@tuanphpvn
tuanphpvn / SWAP EQUAL RESULT.php
Last active August 4, 2016 00:41
Find permutation of an array with total #ALGORITHM
<?php
$swapEqualWithResult = function($arr, $total) {
$result = array();
for($i = 0; $i < count($arr); $i++) {
for($j = 0; $j < count($arr); $j++) {
for($k = 0; $k < count($arr); $k++) {
if($arr[$i] + $arr[$j] + $arr[$k] === $total)
{
$result = array($arr[$i], $arr[$j], $arr[$k]);
@tuanphpvn
tuanphpvn / MAGENTO - delete all products.php
Last active August 4, 2016 00:43
Delete all product belong to category
<?php
$deleteAllProduct = function() {
$products = Mage::getModel('catalog/product')->getCollection();
foreach ($products as $product) {
try {
$product->delete();
} catch(Exception $e) {
echo "Product #".$product->getId()." could not be remvoved: ".$e->getMessage();
}
@tuanphpvn
tuanphpvn / MAGENTO - update attribute set option value.php
Last active August 5, 2016 00:53
Save a list of option for Attribute Model #Magento #EAV #OPTION
<?php
$updateAttributeOptionValue = function() {
$attribute_model = Mage::getModel('eav/entity_attribute');
$attribute_id = $attribute_model->getIdByCode('catalog_product', 'color');
$attribute = $attribute_model->load($attribute_id);
$arrColor = array(
'Brown',
@tuanphpvn
tuanphpvn / MAGENTO- Create multiple attribute set by array.php
Last active August 4, 2016 00:52
Create list attribute set by Array #Magento #Attributeset
<?php
$createAttributeSet = function($arrAttributeSetName) {
$entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
$attributeSetDefault = Mage::getModel('eav/entity_attribute_set')->getCollection()
->addFieldToFilter('attribute_set_name', 'Default')
->addFieldToFilter('entity_type_id', $entityTypeId)
->setPageSize(1)->getFirstItem();
@tuanphpvn
tuanphpvn / MAGENTO - create category by fastimport extension.php
Last active August 4, 2016 00:53
create category by fastimport extension #Magento #IMport #Category
<?php
$createCategory = function() {
$data = array();
$arrKey = array(
'Men',
'Men/Shirts',
'Men/New Arrivals',
'Men/Tees, Knits and Polos',