Magento cart / session data outside magento - session

This might get a little confusing as I have tried everything to make this work. All I want is a link in my brand site (domain.com) which shows the qty in my magento 1.5.1 cart (domain.com/shop) I quite easily pulled in product data and navigation blocks but no matter what I do, cart qty is always 0 from outside magento.
The main way I tried was just in my brand site to go:
require_once $_SERVER['DOCUMENT_ROOT'].'/shop/app/Mage.php';
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));
// trying everything
Mage::getSingleton('checkout/cart')->getItemsCount(); // returns 0
Mage::helper('checkout/cart')->getItemsCount(); // returns 0
Mage::getSingleton('customer/session')->isLoggedIn(); // returns blank
Mage::helper('checkout/cart')->getCart()->getItemsCount(); // returns 0
Mage::helper('checkout/cart')->getCart()->getQuote()->getItemsCount(); // returns blank
Then, when none of those worked, I created a template in Magento just to give me the cart qty as a block which returns the block fine but still zero in the cart!
$block = $this->layout->createBlock('core/template');
$block->setTemplate('page/html/cartForBrand.phtml');
return $block->renderView();
and the block in magento is simply
Mage::getSingleton('core/session', array('name'=>'frontend'));
$cart = Mage::getModel('checkout/cart')->getQuote()->getData()['items_qty'];
I've seen a lot of people having similar issues: / session_cookie_management, .domain.com cookie_domain(even though that's subdomain specific), I've read and tried everything I could find for 2 days. Constantly deleting session and cache directories and clearing cache and cookies with magento caching disabled.
This is the first question I've posted on this site after using it for years, I've been stuck on this for 3 days! Pulling my hair out!

I copied your code and tested it on Magento 1.5, 1.6 and 1.7.
I placed the code in a PHP file called test.php in the Magento root directory. This is the code I used:
umask(0);
require_once 'app/Mage.php';
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));
var_dump(array(
"Mage::getSingleton('checkout/cart')->getItemsCount()" =>
Mage::getSingleton('checkout/cart')->getItemsCount()
)); // returns number of items (w/o qty)
var_dump(array(
"Mage::helper('checkout/cart')->getSummaryCount()" =>
Mage::helper('checkout/cart')->getSummaryCount()
)); // returns number according to configuration
var_dump(array(
"Mage::getSingleton('customer/session')->isLoggedIn()" =>
Mage::getSingleton('customer/session')->isLoggedIn()
)); // returns bool true|false
The Magento instances use the local test host names magento15.dev, magento16.dev and magento17.dev.
The I requested the corresponding Magento instances and placed a product in the cart (tested with a configurable and a simple product), and then updated the quantity of the product in the cart.
Between each step I reloaded the test.php file in the browser.
The result is always the same: it works as expected. Each call returns the same values as on the Magento site.
So this means your code is correct, it might be your domain and/or setup (is the browser sending the Magento frontend cookie when you request your test script)?

Your code is good, but by default Magento's "frontend" cookie isn't accessible outside of Magento (and so you can't access session data). You'll need to change the cookie's path in Admin > System > Configuration > Web > Session Cookie Management > Cookie Path. Try setting that to /

If all you want is the qty why instantiate magento at all. Just set a client side cookie (ie cart_qty) and then read this cookie on your main site header.

Think you forgot:
Mage::app()->setCurrentStore(1); // replace 1 with your store id
after
Mage::app()

In Your Code: Instead of:
Mage::app();
use
Mage::app()->loadArea('frontend');
& u will get your output...

Related

I duplicated products magento and my urls are product-1.html product-2.html

I have made my magento store (community)
I have used duplicate product on a lot of products and I didn't get that the URL key field had to be changed for every product i thought i would solve it self.
Now i have about 100 products and unfortunately they are now named example:
/HaircolorBlack.html , /haircolorblack-1.html, /haircolorblack-2.html and so on.
I wonder if there are any way to easy make magento re create the url after what the products meta title is?
This would be so helpful.
I saw this two year old post about approx the same thing but I didn't wanted to use that script since I don't know if it might break anything in my magento since its been so much updates. Here is link: Clearing URL keys in Magento
Also I need total moron instructions on where to put the script to. Sorry ;)
Thanks a lot.
S
You can create a one off script using magento itself.. add this script somewhere in your magento root folder (beside index.php):
<?php
require_once "app/Mage.php";
Mage::app('admin'); // must be admin to do changes
function convertToUrlKey($string) {
return $string; // I WILL LEAVE YOU TO SOLVE THIS PART
}
foreach(Mage::getModel('catalog/product')->getCollection() as $product):
$metaTitle = $product->getMetaTitle();
$urlKey = convertToUrlKey($metaTitle);
try {
$product->setUrlKey($urlKey);
$product->save();
echo "Assigned url key {$urlKey} to {$product->getName()}.\n";
} catch(Exception $ex) {
echo "ERROR: {$ex->getMessage()}";
}
endforeach;
Obviously this is not a complete solution as you will have to re-create the convertToUrlKey function for the correct behavior of converting 'Black Hair Color' to 'black-hair-color' and also ensure that there will be no duplicate url keys generated..
You can run the script by browsing to it or running through SSH:
$ cd magentoProject
$ php -f changeUrlkeys.php
Good luck!

Magento reindexing table

I am updating the product information like quantity,shippingindays,outofstock values etc through mysql and the value gets updated but unless reindexing is done, it is not reflecting in the frontend.What to do to make the reflection in front end instantaneously or can i update the reindexing table directly, is it a good practise to do that? and also if the product goes out of stock how this reflection is instantaneous??
Try this code when you load the frontend to reindex everything. You can check the run method inside Mage file for getSingleton and all.
require_once 'app/Mage.php';
$app = Mage::app('default');
$indexingProcesses = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($indexingProcesses as $process) {
$process->reindexEverything();
}
Hope this may help.

Shipping Methods are not working in magento 1.7

I activated flat shipping in the shipping options and whenever you do checkout it says “Sorry, no quotes are available for this order at this time.”Any one please help me since i am a beginner in magento.
EDIT
I have tried other shipping methods too its also showing the same. we have upgraded Magento ver. 1.4.1.1 to magento 1.7. Any help would be greatly appreciated..
EDIT 2
After upgrading community folder contains only Phoenix folder. After that i added Biebersdorffolder by seeing an error in checkout page. I don't the the purpose of folders AW and RocketWeb. Since i am not familiar with magento.
If the image is not visible i have added the image in this url
http://i47.tinypic.com/14smix1.jpg
do you have any Checkout files modified on your project? Or maybe custom checkout.
I will list the code locations you need to check to make sure your Shipping works correctly.
So to begin with: Magento has very special work process with Shipping rates - it's called "collectRates" and is some modification of pattern "composite".
To make sure that everything works correctly on the code basis you need to first check the Onepage.php model (app/code/core/Mage/Checkout/Model/Type/Onepage.php): lines 330, 341, 556, 616; There should be code
{address}->setCollectShippingRates(true)
where {address} is current address variable.
This code is significant for future "collectRates" processes, because just when the Onepage Checkout page is initializing the "collectRates" process has already been processed, and the flag "collect_shipping_rates" is set to "false". If it's not set back to true, the next "collectRates" process will not be performed.
After you check the location above, and it still doesn't work - you might want to add some logging to Mage_Checkout_Block_Onepage_Shipping_Method_Available::getShippingRates method. If method is properly executed and return some rates from $this->getAddress()->getGroupedAllShippingRates() call, there might be some issues with .phtml template on your locan theme (default path to .phtml is app/design/frontend/base/default/template/checkout/onepage/shipping_method/available.phtml).
Here's how you can log the outcome from $this->getAddress()->getGroupedAllShippingRates() call (Mage_Checkout_Block_Onepage_Shipping_Method_Available::getShippingRates method):
$groups = $this->getAddress()->getGroupedAllShippingRates();
$printGroupes = array();
foreach ($groups as $code => $groupItems) {
foreach ($groupItems as $item) {
$printGroupes[$code][] = $item->getData();
}
}
Mage::log($printGroupes, null,'special.log');
Please note, that the result array with all rates will be logged into "special.log" under var/logs folder.
I wish I could be more of help, but the issue requires some digging into code, the debugger will be even more helpful than logging, if you can get hold of one.
Cheers!
Price should be entered for Flat Rate shipping to work
The problem might be of the country selection
Ship to applicable countries is set to "Specific Country" and you selected "Canada".
So you will see this shipping method only if you select Canada on the frontend during the checkout.
Or
You can make this to All Countries and check whether its working or not.

Product description page not shown, error 404

using magento 1.7.0.2 here, having already tested lot of recommended solutions (reindexing, clearing cache, assuring no url rewrite is done, etc) I give up and ask the question.
We are importing a bunch of products (around 150) into the demo store, together with 2 new categories and lot of new attributes (around 200). After some issues we finally managed to get the products shown both on backend and frontend, great.
But when clicking on any newly imported product we get the 404 error page from Magento with the "Whoops, our bad...". After 2 days searching and trying different approaches still haven't found a solution. Reindexing, clearing cache, checking the url rewrite option, changing 'localhost' for '127.0.0.1' on the core_config_data table, and some other solutions that worked for others, no use for us.
Any clue?
Other than the solutions found we have checked that the products are enabled, visibility is set for both catalog and search, there is a website assigned and quantity is bigger than zero. Also we manually created a product with all the correct attributes values and its description page is visible. Comparing (both using backend and database tables/fields) with the imported products averything seems to be exactly the same.
I'm self answering. Thanks to #sulman that pointed in the correct direction.
Yes, visibility was ok for all the products and it was correctly settled as for both Catalog and Search. But the problem was that in our import script we didn't check for the existence and correct assignment of the attribute to the imported product. Therefore the attribute existed but it was not linked to the already existing visbility attribute.
Here a sample code in case is needed, see the opening if (isset($attributeId)) that checks if the attribute exists or not.
if (isset($attributeId))
{
$entityAttributeModel = Mage::getModel('catalog/product_attribute_api');
$attributeList = $entityAttributeModel->items($attributeSetId);
$attributeLink = $this->search($attributeList, 'attribute_id', $attributeId);
if (sizeof($attributeLink) == 0)
{
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeToGroup($this->getEntityTypeId(), $attributeSetId, $groupId, $attributeId);
}
$model = Mage::getModel('catalog/resource_eav_attribute');
$attribute = $model->load($attributeId);
}

Magento Ajax add to cart not working for subsites

I've written a small script that is called via ajax for adding products to the cart.
$request = Mage::app()->getRequest();
$session = Mage::getSingleton('core/session', array('name'=>'frontend'));
$cart = Mage::helper('checkout/cart')->getCart();
foreach($pids as $pid){
if(!pid || $pid == ''){continue;}
$product = Mage::getModel('catalog/product')->load($pid);
$cart->addProduct($product, $qty);
}
$session->setCartWasUpdated(true);
$cart->save();
I have a multi site setup and this script works fine when it is run under the main site but when I run it under one of the subsites it doesn't add it to the cart.
I've tried having the addtocart.php in the root of the subsite (and including the main sites mage.php) and have also tried adding it to the root of the main site, But nothing works.
Do I need to specify the website id somewhere?
Thanks
First take the easy step of setting a cookie domain prefixed with a single period. This acts like a wildcard.
The default behaviour is to not share carts between stores. In your 'small script' make sure the correct store is chosen the first time you initialise the app.
Mage::app($storeId);
Sometimes when crossing domains you need to include the SID as an URL parameter. I'm not sure how you would find that value, perhaps from the referrer page..?

Resources