add product to wishlist in magento API - magento

I have checked in magento soap API but I can not find API for add product to wish list so I am developing own API but I don't know what is the problem with my code so please help me to find solution.
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once '../app/Mage.php';
Mage::app();
$customer_id = $_GET['customer_id'];
$product_id = $_GET['product_id'];
$customer = Mage::getModel('customer/customer');
$wishlist = Mage::getModel('wishlist/wishlist');
$product = Mage::getModel('catalog/product');
$customer->load($customer_id);
$wishlist->loadByCustomer($customer_id);
$res = $wishlist->addNewItem($product->load($product_id));
if($res)
{
$status =1;
$message = "your product has been added in wishlist";
}
else
{
$status =0;
$message = "wrong data send";
}
$result = array("status" =>$status,"message"=>$message);
header('Content-type: application/json; charset=utf-8');
echo json_encode($result);
?>

There is a similar question you might want to check.
In your code you are not extending Magento API in any way but creating a standalone hackish script that inserts items to wishlist. I suggest that you take a look on how to develop a magento extension or if you need to create a backend (CLI) script, at least use the same structure as in shell/abstract.php and extend the shell base class - you will save yourself loads of headaches.
Moreover, your code is not secure, maintainable and removes all the benefits of using Magento as an ecommerce platform/framework (security, authorization & authentication etc)
Inchoo have quite some blog posts that could give you an idea where to begin
http://inchoo.net/magento/magento-api-v2/
http://inchoo.net/magento/extending-the-magento-api/

/* error_reporting(E_ALL);
ini_set("display_errors", 1);*/
You Have Comment This line And Write this
require_once './app/Mage.php';

Related

Add SKU to end of Magento URL

I have searched for days trying to find a way of doing this but cannot find an answer, hopefully someone here can help me.
Essentially we want to insert the SKU into the product page url
Example
SKU = ST500DM002
So
domain.co.uk/seagate-barracuda-500gb-sata3.html
Would become
domain.co.uk/seagate-barracuda-500gb-sata3-ST500DM002.html
I had a look at this How to customize product URL?
It kind of works but it creates a path like this http://www.domain. co.uk/catalog/product/ST500DM002/seagate-barracuda-500gb-sata3.html rather than add it onto the end
Also we have a lot of SKUs with # in them which causes problems and throws 404
Is there a way to programmatically add the SKU and strip out #
Thanks
Copy the code and paste it to the file "script.php" and move it to magento root folder and try to run it in browser by using the following url,
http://localhost.com/script.php
<?php
require 'app/Mage.php';
Mage::app();
$amount = 0;
$model = Mage::getModel('catalog/product');
$products = $model->getCollection();
foreach ($products as $product) {
$model->load($product->getId());
$urlkey = $product->getUrlKey();
$sku = $product->getSku();
$product->setUrlKey($urlkey."/".$sku)->save();
set_time_limit();
$amount++;
}
When you run the file, All the urls will be rewritten with the new url keys.
You can find updated keys everywhere.
Create observer to catch event catalog_product_save_after_handler
$product = $observer->getProduct();
$urlkey = $product->getUrlKey();
$sku = $product->getSku();
if(substr($urlkey, strlen($sku) * (-1)) != $sku){
$product->setUrlKey($urlkey."/".$sku)->save();
}

magento - access cart in external PHP file

Trying to access the magento cart from an external php file, I have loaded Mage and am able to access products & categories but for some reason I'm unable to access the cart information.
Shop is located in www.domain.com/shop/
PHP File is located in www.domain.com/file.php
Magento cookie setting is set to '/'
I have looked at and tried many example of how to get the information and none of them have worked, the code I have at the moment is:
<?php
require_once '/home/admin/public_html/shop/appMage.php';
Mage::app();
Mage::getSingleton('checkout/cart', array('name' => 'frontend'));
$cartItemsCount = Mage::getSingleton('checkout/cart')->getItemsCount();
$cartTotal = Mage::helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal());
echo 'You have '. $cartItemsCount . ' item(s) in your cart. <a class="cartgo" href="'.Mage::helper('checkout/cart')->getCartUrl().'">Checkout</a>';
if($cartTotal > 0){ echo '<span>[£'.$cartTotal.']</span>'; }
echo '</a>';
?>
It works perfectly fine within the magento site but not from this external file for some reason. It returns a 0 even though there is a product in the cart.
Any pointers?
Try
// Mage init
require_once '../../app/Mage.php';
umask(0);
Mage::init('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
// Get customer session
$session = Mage::getSingleton('customer/session');
// Get cart instance
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$cartItemsCount = $cart->getItemsCount();
see magento 1.8 add product to cart using php

How to call a joomla module in virtuemart shop.browse page

How can a joomla module be called inside the virtuemart shop.browse page. I tried the following code inside the browse_layouttable.tpl.php but it is not working for me. I am using joomla1.5.23 and the virtuemart version is 1.1.8. Any suggestion?
<?php
$modules =& JModuleHelper::getModules('logo');
foreach ($modules as $module)
{
echo JModuleHelper::renderModule($module)
}
?>
Try this
$document = &JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$Module = &JModuleHelper::getModule('mod_fmDataGrid');
$Params = "param1=bruno\n\rparam2=chris"; //if you want to pass params
$Module->params = $Params;
echo $renderer->render($Module);
I would suggest you use "Modules Anywhere" from NoNumber:
http://extensions.joomla.org/extensions/core-enhancements/coding-a-scripts-integration/embed-a-include/6402
After you install this plugin you can add module positions directly in your browse page PHP using this example:
{modulepos mynewposition}
This is a must have plugin for all Joomla users, because you can use it throughout your site.

Updating Meta Title in Magento via PHP script causes unwanted events

On our Magento 1.5.0.1 I wrote a PHP script to update all of our product meta titles to be based on the name and SKU of each product. The script is run by calling PHP via SSH.
After running the script I noticed in our M2E Pro (the eBay syncing module) that every single product appears to have become disabled.
Looking at the actual products they aren't disabled, so it appears my script is somehow faking the event.
Out of our 3,000+ products, though, one of them was disabled and subsequently M2E Pro removed the listing from eBay.
Here is my script:
<?php
set_time_limit(0);
define('MAGENTO', "/home/discount/public_html");
require_once MAGENTO . '/app/Mage.php';
error_reporting(E_ALL);
ini_set('display_errors', '1');
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$storeId = Mage::app()->getStore('default')->getId();
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('meta_title');
$total = count($products);
$count = 1;
foreach ($products as $product)
{
$mt = sprintf("%s [%s]", $product->getName(), $product->getSku());
$sku = $product->getSku();
if ($product->getMetaTitle() != $mt)
{
$percent = $count / $total;
echo $sku." ".$percent."\n";
$product->setMetaTitle($mt);
$product->save();
}
}
?>
I'd like to know what I need to do to my script to make it correctly update the meta title without causing odd events?
Stab in the dark here, but it's a good chance the M2E extension has an event listener setup for model/product saves, and in that event listener does things depending on product state.
By itself this isn't a problem, but when you load an EAV model via its collection it loads the bare minimum of attributes. Since you haven't explicitly loaded product status via addAttributeToSelect this may be confusing the M2E extension. Trying adding the status attribute. If that doesn't work try addAttributeToSelect('*') — M2E may be checking other attributes as well to decide when to pull things.
Finally, when you load an EAV model via a collection their afterLoad isn't called. It's possible the M2E extension (naively) assumes something that happens in afterLoad will have happened, so you may want to call it explicitly for each product.
$product->afterLoad();
Short of that, you'll need to reverse engineer M2E to the point in the code where it's doing it's above logging and determine why it thinks it needs to disable a product. Since this is a commercial extension, I'd reach out to the developers. If you pay for something you deserve some level of support.
<?php
//increase the max execution time
#ini_set('max_execution_time', -1);
//memory_limit
#ini_set('memory_limit', -1);
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Start Despaly All Product Meta Title And Description
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$categories = Mage::getModel('catalog/category')
->getCollection()
// magic is prepared here..
->addAttributeToSelect('*')
// then the magic happens here:
->addAttributeToFilter('level', array('eq'=>4))
->load();
if (count($categories) > 0):
foreach($categories as $category):
$catId = $category->getId();
$category = Mage::getModel('catalog/category')->load($catId);
$resource = Mage::getResourceModel('catalog/category');
//if($catId==1465): //If Update Specific category Value by Id
$CategoryName = $category->getName();
$metaTitle = "Buy ".$CategoryName." Test Title";
$metaDescription = "Shop your favourite ".$CategoryName." Test Description";
$category->setData('meta_title', $metaTitle);
$resource->saveAttribute($category, 'meta_title');
$category->setData('meta_description', $metaDescription);
$resource->saveAttribute($category, 'meta_description');
$check = $category->getMetaTitle();
echo "<pre>";
print_r($catId);
echo "<pre>";
print_r($check);
echo "\n";
//endif;
endforeach;
else: echo "No Results";
endif;
?>

Checking for Magento login on external page

I'm hitting a wall here while trying to access items from Magento on an external page (same server, same domain, etc, etc). I want to see if the user is logged into Magento before showing them certain parts on the site.
Keep in mind that this code exists outside of Magento.
Mage::app("default");
Mage::getSingleton("core/session", array("name" => "frontend"));
if (empty($session))
{
$session = Mage::getSingleton("customer/session");
}
if($session->isLoggedIn())
echo "hi";
$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
echo $cart;
$cart returns 0, where I definitely have products in my cart. isLoggedIn() also returns false. What am I doing wrong here? Is there an option in Magento that I need to turn on or off to be able to access this information outside of Magento?
Using the code above, I created a php file in the Magento folder. From there, added the number of items in the cart and whether you were logged in or not to an array and encoded it as json. I used some jquery on my external page to grab the file and pull the data I needed.
Not quite the ideal situation, but it works for now.
Are you including Mage.php (which defines getSingleton)?
require_once ($_SERVER['DOCUMENT_ROOT'] . '/app/Mage.php');
What does $session have in it after the getSingleton() call?
print_r ($session);
EDIT: I tried this on my end and was not able to get accurate isLoggedIn() or getItemsCount() data. When I dumped out $session its showing all the fields as 'protected.'
I'm not interested in requiring the user to log in...I merely want to access data from the already logged in session.
Anyone else have any thoughts on this? All the examples out there seem to be pre 1.4.x.
require_once "app/Mage.php";
umask(0);
Mage::app();
// require_once $_SERVER['DOCUMENT_ROOT'] . "/mage1/app/Mage.php";
// Customer Information
$firstname = "krishana";
$lastname = "singh";
$email = "krish.bhati#gmail.com";
$password = "myverysecretpassword";
// Website and Store details
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteId;
$customer->setStore($store);
$mageRunCode = isset ( $_SERVER ['MAGE_RUN_CODE'] ) ? $_SERVER ['MAGE_RUN_CODE'] : '';
$mageRunType = isset ( $_SERVER ['MAGE_RUN_TYPE'] ) ? $_SERVER ['MAGE_RUN_TYPE'] : 'store';
$app = Mage::app ( $mageRunCode, $mageRunType );
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton('customer/session');
$session->start();
$customer->loadByEmail($email);
$customer_id= $customer->getId();
if($customer_id){
Mage_Core_Model_Session_Abstract_Varien::start();
$session->login($email, $password);
$session->setCustomerAsLoggedIn($session->getCustomer());
echo $session->isLoggedIn() ? $session->getCustomer()->getName().' is online!' : 'not logged in';
}

Resources