Import categories in magento through script - php

Hello I have just started to learn magento. Now I was trying to import category in magento through script.
my magento code looks like
<?php
require_once 'businessclasses.php';
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
$count = 0;
echo "<pre>";
$data= new getCSV();
$rows=$data->getRootCategories(); // Gets the list of root categories.
foreach($rows as $row) {
echo $categoryName = $row['d']; // Name of Category
// Create category object
$category = Mage::getModel('catalog/category');
$category->setStoreId(1); // 'US-Store' store is assigned to this category
$rootCategory['name'] = $categoryName;
$rootCategory['path'] = "23/25"; // this is the catgeory path
$rootCategory['display_mode'] = "PRODUCTS";
$rootCategory['is_active'] = 1;
$category->addData($rootCategory);
try {
$category->save();
echo $rootCategoryId = $category->getId();
}
catch (Exception $e){
echo $e->getMessage();
}
}
?>
This code runs without an error and also this line
echo $rootCategoryId = $category->getId();
prints unique IDs for each category (loop) but when i see at the admin pannel it shows me nothing. Like no category imported.
I have reffered this Question. but it is not helping.
any help would be appreciated. Thank you.

Hey guys I got an answer.
$rootCategory['path'] = "1/23/25"; // this is the catgeory path <-- I was giving this path wrong.

Well you've got your answer yourself. However, if anyone looking for some other tools for category and product and other import/export, I recommend Magmi http://sourceforge.net/projects/magmi/, awesome tool saved me a lot of time .

Related

How to fetch Media Assets of a Product from Akeneo 5

I try to fetch the media assets from products in
I am using https://github.com/akeneo/api-php-client-ee (v6) and know about https://api.akeneo.com/api-reference-50.html
This is what I came up so far:
$searchBuilder = new \Akeneo\Pim\ApiClient\Search\SearchBuilder();
$searchBuilder->addFilter('enabled', '=', true);
$searchFilters = $searchBuilder->getFilters();
$products = $client->getProductApi()->all(100, ['search' => $searchFilters]);
foreach ($products as $product) {
foreach($product['values']['assets'] as $assetData) {
foreach($assetData['data'] as $code) {
echo $code;
$asset = $client->getProductMediaFileApi()->all();
var_dump($asset);
}
}
What I have tried / Questions:
I get a code like 1234_00 (if 1234 is the product number), but I do not know how to fetch the specific file from the product media file api. Do I have to filter here? How?
I tried to $client->getAssetMediaFileApi()->download($code) but the $code I have does not seem to be the full asset code (I get a 404 not found error)
How can I find out which assets are related to a specific product to download them or get the download URL?
This works - "bilder" is the Asset Family code in our case.
foreach ($products as $product) {
foreach($product['values']['assets'] as $assetData) {
foreach($assetData['data'] as $code) {
echo $code;
$assets = $client->getAssetManagerApi()->get('bilder', $code);
foreach($assets['values']['media'] as $dataLine) {
$download = $client->getAssetMediaFileApi()->download($dataLine['data']);
file_put_contents('/tmp/' . basename($dataLine['data']), $download->getBody());
}
foreach($assets['values']['variation_image'] as $dataLine) {
$download = $client->getAssetMediaFileApi()->download($dataLine['data']);
file_put_contents('/tmp/' . basename($dataLine['data']), $download->getBody());
}
}
I found the main clue, by looking at the Akeneo Admin Panel and which requests it does :-)

Output from plugin to appear on specific pages with WordPress

I am currently trying to do research on how to output a result from a WordPress custom plugin to the frontend of WordPress. I have trying adding filters, do actions, add actions. But nothing seems to work. I know the add_action works for child themes because I'm currently using a script to do so.
The script below works for the child theme but not in a plugins functions.php page.
function displayPages()
{
$pageIDs = "List of page ids";
if(is_page($pageIDs))
{
include_once 'script.js';
}
}
add_action('wp_footer', 'displayPages')
Here is the current code I was trying use to fix the problem with the plugin.
function getPageIDs()
{
include_once($_SERVER['DOCUMENT_ROOT'].'/stage/wp-config.php' );
global $wpdb;
$row = $wpdb->get_row( 'SELECT pages FROM schemalocalbusiness WHERE id = 1');
$pageIDs = $row->pages;
$pageIDsArray = explode(",", $pageIDs);
foreach($pageIDsArray as $perma)
{
echo ' ' . esc_url( get_permalink($perma) ) . '<br>';
}
}
function includeShemaOnPage()
{
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(getPageID() == $acturla_link)
{
print 'SUCCESS';
}
}
add_action('wp_footer', 'includeShemaOnPage');
Any and all help would be much appreciated! Thank you in advance!

Replace Product Names with a Loop

I have a Magento webshop with several products in it. All of them begin with the word PRODUCT,
for example
PRODUCT 001.
I want to replace the word "PRODUCT" with "Article".
I tried to write a small script, but it's not working at all. Here is what I've got so far:
<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
foreach($_productCollection as $_product)
{
$name = $_product->getName();
$new = str_replace("PRODUCT","Article", $name);
$_product->setName($new);
$_product->save();
};
?>
I did not really know how to run the script, so I added this into a CMS page and opened it:
{{block type="core/template" template="script.phtml"}}
What's wrong with it?
Create a file Replacename.php inside your magento root folder
and write the below code in that file and then execute it with below URL http://www.yourdomain.com/Replacename.php
<?php
require_once('app/Mage.php');
umask(0);
Mage::app();
$_productCollection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToSelect('*');
foreach($_productCollection as $_product)
{
try
{
$name = $_product->getName();
$new = str_replace("PRODUCT","Article", $name);
$_product->setName($new);
$_product->save();
}
catch(Exception $e){
echo $e->getMessage();
}
}

Select Random File From Directory & Display

I'm uning a php script to get a random news article from a directory and to display it on my website, this works fine, however when I try to recreate it to show something else at random, the new one doesn't work - Here is my code:
<?php
function random_unit($unit_dir = 'sections/units')
{
$units = glob($unit_dir . '/*.php');
$unit = array_rand($units);
return $units[$unit];
}
$unit_1 = random_unit("sections/units");
?>
<?php $unit_1 = file_get_contents( $unit_1 ); ?>
<?php echo $unit_1; ?> // Echo's The Contents Of Selected Random File
Can anyone see where I might have gone wrong?

PHP + Wordpress: Auto list image name into dropdown slection

I am creating wordpress theme option panel and want to use some icons. I have one directory dedicated for icon into my theme folder. What I want to do is if user add any new image into that folder it will automatic appear into dropdown selection list into theme option panel.
Is there any way to do this in PHP with Wordpress? I believe that is possible as I saw one theme has the same option but it was so complex so couldn't figured out that and don't remember theme name too now.
I have to use it with below type of code
$video_tax = array(-1 => 'Choose a category');
$video_terms = get_terms('video_category');
if ($video_terms) {
foreach ($video_terms as $video_term) {
$video_tax[$video_term->term_id] = $video_term->name;
}
}
You might want to start by having a look at scandir. This will list all the contents of a folder on your system. From there it would just be a matter of putting the correct path, url or whatever you want in the the value of your options.
EDIT: Here's some sample code from one of my plugins:
function icons_meta(){
global $post;
$custom = get_post_custom($post->ID);
$link = $custom["icon"][0];
$files = scandir(PATH."/icons");
$selected = '';
echo "<select name='icon'>";
foreach($files as $file){
if($file == $link){
$selected = 'selected="selected"';
} else {
$selected = '';
}
echo "<option value='$file' $selected>".$file."</option>";
}
echo "</select>";
}

Categories