Deleting categories in magento - magento

I want to delete empty categories, using the following code:
`<?php
require "app/Mage.php";
umask(0);
Mage::app();
$categoryCollection = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('level', array('gteq' => 2));
foreach($categoryCollection as $category) {
if ($category->getProductCount() === 0) {
print_r ($category);
echo "<br><hr><br>";
$category->delete();
}
}
echo 'End!';
?>`
Upon executing this code, it crashes at delete.
print_r gives the following result:
Mage_Catalog_Model_Category Object (
[_eventPrefix:protected] => catalog_category
[_eventObject:protected] => category
[_cacheTag:protected] => catalog_category
[_useFlatResource:protected] => 1
[_designAttributes:Mage_Catalog_Model_Category:private] => Array (
[0] => custom_design
[1] => custom_design_from
[2] => custom_design_to
[3] => page_layout
[4] => custom_layout_update
[5] => custom_apply_to_products
)
[_treeModel:protected] =>
[_defaultValues:protected] => Array (
)
[_storeValuesFlags:protected] => Array (
)
[_lockedAttributes:protected] => Array (
)
[_isDeleteable:protected] => 1
[_isReadonly:protected] =>
[_resourceName:protected] => catalog/category_flat
[_resource:protected] =>
[_resourceCollectionName:protected] => catalog/category_flat_collection
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array (
[entity_id] => 53
[level] => 4
[path] => 1/2/27/39/53
[position] => 3
[is_active] => 1
[is_anchor] => 1
[product_count] => 0
)
[_hasDataChanges:protected] => 1
[_origData:protected] => Array (
[entity_id] => 53
[level] => 4
[path] => 1/2/27/39/53
[position] => 3
[is_active] => 1
[is_anchor] => 1
)
[_idFieldName:protected] => entity_id
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array (
)
[_syncFieldsMap:protected] => Array (
)
)
What am I doing wrong, and how should I do it?
Any help would be greatly appreciated.

<?php
require "app/Mage.php";
umask(0);
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$categoryCollection = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('level', array('gteq' => 2));
foreach($categoryCollection as $category) {
if ($category->getProductCount() === 0) {
print_r ($category->entity_id);
echo "<br><hr><br>";
$category->delete();
}
}
echo 'End!';
?>
You have to add this line
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
you write this code print_r ($category); so its reruns the array. That is not error.

Related

Push into session issue in Laravel

Each item contains a list of addons as checkboxes. Every time I check an addon I want to push addon_id into the addons array. It's pushing an addon_id to session but it's not pushing it properly.
This code adds item to cart (Notice: I am creating an empty addons array):
$cart = session()->get('cart', []);
if(isset($cart[$request->item_id])) {
$cart[$request->item_id]['qty']++;
} else {
$cart[$request->item_id] = [
'id' => $request->item_id,
'category_id' => $request->category_id,
'price' => $request->item_price,
'item_name' => $request->item_name,
'qty' => 1,
'addons' => [],
];
}
session()->put('cart', $cart);
And this code pushes addons to items:
if(isset($request->item_id)) {
session()->push('cart.addons', $request->addon_id);
}
$cart = session()->get('cart', []);
return response()->json([
'message' => 'Addon added',
'cart' => $cart,
]);
This is how addons are being pushed now:
Array
(
[59] => Array
(
[id] => 59
[category_id] => 27
[price] => 3.78
[item_name] => Simple Sandwich
[qty] => 1
[addons] => Array
(
)
)
[57] => Array
(
[id] => 57
[category_id] => 27
[price] => 5.99
[item_name] => Cheese Burger
[qty] => 1
[addons] => Array
(
)
)
[addons] => Array
(
[0] => 31
)
[addons] => Array
(
[0] => 61,
[1] => 60,
[2] => 31
)
)
I need them to be pushed like this:
Array
(
[59] => Array
(
[id] => 59
[category_id] => 27
[price] => 3.78
[item_name] => Simple Sandwich
[qty] => 1
[addons] => Array
(
[0] => 31
)
)
[57] => Array
(
[id] => 57
[category_id] => 27
[price] => 5.99
[item_name] => Cheese Burger
[qty] => 1
[addons] => Array
(
[0] => 61,
[1] => 60,
[2] => 31
)
)
)
I tried to find a solution but nothing seems to be working the way I need it.
Help is very much appreciated.
Initially you are making an associative array where item_id is the key, but when you are appending in addons you are not specifying against which item_id the addons array should be updated, so you will have to change
if(isset($request->item_id)) {
session()->push('cart.addons', $request->addon_id);
}
to something like this:
if(isset($request->item_id)) {
$cart = session()->get('cart');
if(isset($cart[$request->item_id]) {
array_push($cart[$request->item_id]['addons'],$request->addon_id);
session()->put('cart',$cart);
}
}

How can I check that file returned by laravel-medialibrary getUrl method really exists?

In laravel 8 app I use spatie/laravel-medialibrary 9
and how can I check that file returned by getUrl really exists under storage in code :
foreach (Auth::user()->getMedia('avatar') as $mediaImage) {
\Log::info( varDump($mediaImage, ' -1 $mediaImage::') );
return $mediaImage->getUrl();
}
?
$mediaImage var has data like:
Array
(
[id] => 11
[model_type] => App\Models\User
[model_id] => 1
[uuid] => 2fb4fa16-cbdc-4902-bdf5-d7e6d738d91f
[collection_name] => avatar
[name] => b22de6791bca17184093c285e1c4b4b5
[file_name] => avatar_111.jpg
[mime_type] => image/jpg
[disk] => public
[conversions_disk] => public
[size] => 14305
[manipulations] => Array
(
)
[custom_properties] => Array
(
)
[generated_conversions] => Array
(
)
[responsive_images] => Array
(
)
[order_column] => 1
[created_at] => 2021-12-30T14:08:11.000000Z
[updated_at] => 2021-12-30T14:08:11.000000Z
[original_url] => http://127.0.0.1:8000/storage/Photos/11/avatar_111.jpg
[preview_url] =>
)
Looks like nothing about file under storage...
Thanks!
Method :
File::exists($mediaImage->getPath());
helped me!

Linking arrays according to common values

I have some array like
0: [id: '1',department: 'xyz',date: '10-10-2019',time: '12:50']
1: [id: '1',department: 'xyz',date: '11-10-2019',time: '10:30']
2: [id: '2',department: 'abc',date: '09-09-2019',time: '09:50']
3: [id: '2',department: 'abc',date: '07-07-2019',time: '03:20']
I want them to be merged according to the id and department
so the 0 and 1 array will be merged together and the output should be something like
0:[id: '1',department: 'xyz',[[date: '10-10-2019',time: '12:50'],[date: '11-10-2019',time: '10:30']]]
and the 2 and 3 array will be merged together and the output should be something like
1:[id: '2',department: 'abc',[[date: '09-09-2019',time: '09:50'],[date: '07-07-2019',time: '03:20']]]
how can I do it?
Try this..
$input = [
0 =>['id' => '1','department' => 'xyz','date' => '10-10-2019','time' => '12:50'],
1 => ['id' => '1','department' => 'xyz','date' => '11-10-2019','time' => '10:30'],
2 => ['id' => '2','department' => 'abc','date' => '09-09-2019','time' => '09:50'],
3 =>['id' => '2','department' => 'abc','date' => '07-07-2019','time' => '03:20']];
$output = [];
$id_array = [];
foreach ($input as $values)
{
$id_array [] = $values['id'];
}
$unique_id_array = array_unique($id_array);
foreach($input as $key => $in)
{
if(array_key_exists($key,$unique_id_array))
{
$output[] = [
'department' => $in['department'],
'id' => $in['id']
];
}
}
foreach($input as $in)
{
foreach($output as $key => $out)
{
if($out['id'] == $in['id'] && $out['department'] == $in['department'])
{
$output[$key]['date_time'][] = ['date' =>$in['date'],'time' => $in['time']];
}
}
}
print_r($output);die();
The output is going to be
Array
(
[0] => Array
(
[department] => xyz
[id] => 1
[date_time] => Array
(
[0] => Array
(
[date] => 10-10-2019
[time] => 12:50
)
[1] => Array
(
[date] => 11-10-2019
[time] => 10:30
)
)
)
[1] => Array
(
[department] => abc
[id] => 2
[date_time] => Array
(
[0] => Array
(
[date] => 09-09-2019
[time] => 09:50
)
[1] => Array
(
[date] => 07-07-2019
[time] => 03:20
)
)
)
)
Hello i think this will works fine,and it's easy and best way to achieve your output.
i tried many codes in my system with data given by you,just try this
i hope this will work for you as well.
<?php
$inputArr = [0 =>['id' => '1','department' => 'xyz','date' => '10-10-2019','time' => '12:50'],1 => ['id' => '1','department' => 'xyz','date' => '11-10-2019','time' => '10:30'],2 => ['id' => '2','department' => 'abc','date' => '09-09-2019','time' => '09:50'],3 =>['id' => '2','department' => 'abc','date' => '07-07-2019','time' => '03:20']];
print_r($inputArr);
$second_copy = $inputArr;
foreach ($inputArr as $key => $single)
{
$goalArr[$single['id']] = getRow($single['id'],$single['department'],$second_copy);
}
function getRow($id,$department,$second_copy)
{
$returnArr['id'] = $id;
$returnArr['department'] = $department;
foreach ($second_copy as $key => $single)
{
if($id == $single['id'] && $department == $single['department'])
{
$returnArr['date'][] = $single['date'];
$returnArr['time'][] = $single['time'];
}
}
return $returnArr;
}
echo "<br>Output is: <br>";
print_r($goalArr);
?>

Reverse output from simplexml

Cannot reverse output, tried with array_reverse and usort.
I'm trying to import products from an XML to Magento with magmi datapump, works fine but I need the output in reverse order for Magento to link simple products with configurable products,
Any ideas?
$xml = simplexml_load_file("23.xml") or die("Error: Cannot create object");
foreach($xml->wapiitems->record as $book) {
$item = $book->fields->itemno;
if (strlen($item) <= 6) {
$type = "configurable";
$ca = "color,size";}
else {
$type = "simple";
$ca = "";}
$newProductData = array(
'sku' => (string)$book->fields->itemno, // name
'type' => (string)$type, // sku
'color' => (string)$book->subtables->descriptions->record->fields->variant1name, // special price
'size' => (string)$book->subtables->descriptions->record->fields->variant3name, // price
'attribute_set' => 'Default', // attribute_set
'store' => 'admin',
'name' => (string)$book->subtables->descriptions->record->fields->description, // full description
'configurable_attributes' => (string)$ca // short description
);
//$dp->ingest($newProductData);
echo "</br>";
print_r ($newProductData);
$newProductData=null; //clear memory
unset($newProductData); //clear memory
}
unset($xml);
$dp->endImportSession(); // end import
My output is:
Array ( [sku] => 90349 [type] => configurable [color] => [size] => [attribute_set] => Default [store] => admin [name] => [configurable_attributes] => color,size )
Array ( [sku] => 903490101004 [type] => simple [color] => Red [size] => 4 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => )
Array ( [sku] => 903490101005 [type] => simple [color] => Black [size] => 5 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => )
Array ( [sku] => 903490101006 [type] => simple [color] => Black [size] => 6 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => )
But I need this:
Array ( [sku] => 903490101006 [type] => simple [color] => Black [size] => 6 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => )
Array ( [sku] => 903490101005 [type] => simple [color] => Black [size] => 5 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => )
Array ( [sku] => 903490101004 [type] => simple [color] => Red [size] => 4 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => )
Array ( [sku] => 90349 [type] => configurable [color] => [size] => [attribute_set] => Default [store] => admin [name] => [configurable_attributes] => color,size )
Not sure how Magmi Datapump is linking the simple and configurable products based on your example, but assuming that all that is required for your problem is that configurable products get imported after the simple products, you could do something like this:
Create an intermediate array of product records after pulling them out of XML by changing $newProductData = array(...) to $newProductData[] = array(...)
Now use something like this to sort your intermediate array by product type:
usort($newProductData, function($a, $b)
{
if ($a['type'] == 'configurable' && $b['type'] == 'simple') {
return 1;
} else if ($a['type'] == 'simple' && $b['type'] == 'configurable') {
return -1;
} else {
return strnatcmp($a['sku'], $b['sku']);
}
});
Finally, iterate over the sorted array and complete the import:
foreach ($newProductData as $data) {
$dp->ingest($data);
}

CodeIgniter, variable declaration

I have a problem with undefined variable. In view/somefolder/somefile.php they echo some variabla (<?php echo $news;?> ) . I tried everything to find where this variable is defined (Find in Path at PHPStorm), but without result. When I put print_r($GLOBALS) to test, I got
[_ci_cached_vars:protected] => Array
(
[unread] => 0
[unans] => 0
[ques_unread] => 0
[is_loged] =>
[level] =>
[avatar] => media/img/avatar.png
[username] =>
[root_cat] => 0
[logErr] =>
[radio_list] => Array
(
)
[entitet] => 1
[current_modul] => infograda
[tree] => <ul></ul>
[category] => stdClass Object
(
[id] => 153
[title] => HRONIKA
[description] =>
[parent_id] => 0
[order] => 52
[module] => infograda
[expanded] => 0
[content_type_id] => 1
)
[module] => infograda
[active] =>
[additionHtml] =>
[news] => Array
(
[0] => stdClass Object
(
[id] => 1574
[content_id] => 1574
[module] => infograda
[order] =>
[title] => Title
[text] => <div class="uvod_holder">
in Codeigniter, a view is loaded from a controller. For example:
Controller:
// this file resides in application\controllers\mycontroller.php
class Mycontroller extends CI_Controller{
function mypage() {
$data = array('news' => 'some news');
$this->load->view('page', $data);// will load views\page.php and pass the $data array
}
}
View
<!--this file resides in application\views\page.php -->
<html>
<head></head>
<body><?= $news; ?></body>
</html>
To make this work, your URL will say http://localhost/index.php/mycontroller/mypage (if you're working locally)
Your variable is being set in the controller that loads that view.

Resources