December 14, 2018

cm-mini

Thinking Magento

Magento - The MetaDescription

Many have claimed that they have the ultimate metadescription patch for Magento and as far as I'm concerned. Many have tried and many have failed.

For me the following is what made successful use of the metadescription.

<?php 
$content = htmlspecialchars($this->getDescription());
$content = str_replace("\n",'',$content);
$content = str_replace("\r",'',$content);
$content = preg_replace('/([\s]{2,})/',' ',$content);
$content = substr($content,0,250); ?>
<meta name="description" content="" />

Feel free to update your head.phtml file with it and see if it makes a difference for you.

Showing Percentage Saved On Product Page - Magento

So something that isn't included on the frontend of Magento. You Saved xx%. So, here's a little something you can copy and paste into your view.phtml

<?php
$_finalPrice = $this->helper('tax')->getPrice($_product, $_product->getFinalPrice());
$_regularPrice = $this->helper('tax')->getPrice($_product, $_product->getPrice());
if ($_regularPrice != $_finalPrice):
$getpercentage = number_format($_finalPrice / $_regularPrice * 100, 2);
$finalpercentage = 100 - $getpercentage;

echo '<div class"savingbox">YOU SAVE '.number_format($finalpercentage, 0).'% </div>' ;

endif; ?>

Show Quantity Sold On Product Page - Magento

If you want to have it so that it only shows quantity sold on the product page on items that are currently on sale.. use the following., There will also be an alert for customers when there is only 1 remaining in stock.Then use the following code.

$_finalPrice = $this->helper('tax')->getPrice($_product, $_product->getFinalPrice());
$_regularPrice = $this->helper('tax')->getPrice($_product, $_product->getPrice());
if ($_regularPrice != $_finalPrice):
$sku = nl2br($_product->getSku());
$to = $_product->getResource()->formatDate(time());
$from = $_product->getResource()->formatDate(time() - 60 * 60 * 24 * 1);
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addOrderedQty($from, $to, true)
->addAttributeToFilter('sku', $sku)
->setOrder('ordered_qty', 'desc')
->getFirstItem();

$product = $_productCollection;
echo 'Already Bought Today '.(int)$product->ordered_qty;

endif;

if ((int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()==1): ?>

<p style="color:#990000; padding:5px 0; text-align:right;"><strong>ONLY 1 LEFT IN STOCK!</strong></p>

<?php endif; ?>
 

Or if you want to show stock sold on the product page for the entire duration the product has been active, use the following

<?php
 $sku = nl2br($_product->getSku());
 $product = Mage::getResourceModel('reports/product_collection')
 ->addOrderedQty()
 ->addAttributeToFilter('sku', $sku)
->setOrder('ordered_qty', 'desc')
->getFirstItem();

echo 'Already Bought '.(int)$product->ordered_qty; ?>
and finally if you want to just just on the page how many have been sold of the product for today, use the following

<?php
$sku = nl2br($_product->getSku());
$to = $_product->getResource()->formatDate(time());
$from = $_product->getResource()->formatDate(time() - 60 * 60 * 24 * 1);
$product = Mage::getResourceModel('reports/product_collection')
->addOrderedQty($from, $to, true)
->addAttributeToFilter('sku', $sku)
->setOrder('ordered_qty', 'desc')
->getFirstItem();
echo 'Quantity Ordered Today '.(int)$product->ordered_qty; ?>

Just copy and paste into your view.phtml file, into a suitable place :)

Magento - Multiple Domains Using a Single SSL

The following is for version 1.3X - See below for changes to work in 1.4x - 1.7

This tutorial assumes you have already successfully setup a multi domain website.

Today we were confronted with a problem that quite literally left us all stumped. How on earth do we get several domains to use the same secure url. Being not so bright we forgot that sessions and cookies are going to be stored for the one url and that if you transfer directly over to the secure url, you're going to be left without a paddle and each time it redirects to an empty shopping cart.

1.3x

So to combat this you'll need to create a directory in your root directory that has the same name as the url. http://www.store2.com

Example being

httpdocs is your main directory for your magento installation

httpdocs/store2 is going to be where you will place the following.

A copy of  your index.php file with two adjustments. The following needs to be adjusted to look like this;

$mageFilename = '../app/Mage.php';
Mage::run('WEBSITE CODE', 'website');

Also make a copy of your .htaccess file and place that in the folder.

Now go to your web configuration settings.
Switch to website 2.

After switching to website to, on the secure base_link_url and set it to {{secure_base_url}}store2/
(store2 being the directory you created)

Thats it, now everything will correctly go to the secure url.

1.4x - 1.7x

httpdocs is your main directory for your magento installation

httpdocs/store2 is going to be where you will place the following.

A copy of  your index.php AND also the .htaccess file in the root directory

The index.php file in

$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'store2';
/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'website';

In the WEB configuration of store2 (Use the top left drop down to get there)

Set Use SID's to YES

Set secure base_link_url and set it to {{secure_base_url}}store2/
(store2 being the directory you created)

SAVE

Now test out to see if adding an item to your cart and pressing checkout will take you to the correct SSL OnePageCheckout

Magento - Change Place Order Button for Paypal Button

It recently occurred to us that people seem to be very confused by the place order button on the one page checkout, when Paypal is selected as prefered payment method. Normally when checking out with paypal you expect a Proceed to Paypal button to avoid any confusion. Magento already sends an order notification email once you press the place order button so anyone in their right mind could think they have struck gold and that the order has already been accepted without paying. So, here is my new solution that will allow you to change the place order button for a proceed to paypal button, if paypal is selected.

For our example I'll be using the paypal standard method.

Firstly open up your template folder and go to Checkout/Onepage/review.phtml and replace your current checkout button with the following.

<span id="paypal-buttons" class="a-center" style="display:none;">
<input type="image" src="/" onclick="review.save();" value="" />
</span>
<span id="review-buttons-container" class="a-center" style="display:block;">
<input type="image" src="/" onclick="review.save();" value="" />
</span>
Once you've done this, you can then add this javascript to the base of the review.phtml file. This is the trigger for the change in button.
<script language="javascript">
function checkoutbuttons()
{
if (document.getElementById("p_method_paypal_standard").checked == true) {
document.getElementById("paypal-buttons").style.display = "block";
document.getElementById("review-buttons-container").style.display = "none";
}
if (document.getElementById("p_method_paypal_standard").checked == false) {
document.getElementById("paypal-buttons").style.display = "none";
document.getElementById("review-buttons-container").style.display = "block";
}
}
</script>
Finally, you'll need to open up methods.phtml, also in the checkout/onepage folder and replace the onlick function to the following.
 onclick="payment.switchMethod(''); checkoutbuttons()"
Save both the files and you'll be set.