سؤال

Anyone have a solution for 301 permanent redirects for deleted products in Magento?

I have a store with 5000 products. 4000 have been sold and will never be back in stock. I want to delete those products b/c I no longer need them and want to clean up/decrease my database, but I need to 301 permanently redirect them to appropriate pages to keep the SEO juice I've built up.

To be clear, I do not want the deleted product pages to be live any more, not even with an Out of Stock message, and I do not want 404 Page Not Founds.

Also required is if product can assign disable category then redirect to home page not an 404 page redirect it is important part.

My actual requirement is if i have product deleted then product can be 301 redirect to parent category. so how do I get these programmaticly? Or other solution to this?

هل كانت مفيدة؟

المحلول

In ./app/code/local/INPUTANAME/RewriteAfterDelete/etc/config.xml put

    <?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <INPUTANAME_RewriteAfterDelete>
            <version>0.0.1</version>
        </INPUTANAME_RewriteAfterDelete>
    </modules>
    <global>
        <models>
            <inputaname_rewriteafterdelete>
                <class>INPUTANAME_RewriteAfterDelete_Model</class>
            </inputaname_rewriteafterdelete>
        </models>
        <events>
            <catalog_product_delete_after>
                <observers>
                    <inputaname_rewriteafterdelete>
                        <class>inputaname_rewriteafterdelete/observer</class>
                        <method>rewriteDelete</method>
                        <type>singleton</type>
                    </inputaname_rewriteafterdelete>
                </observers>
            </catalog_product_delete_after>
        </events>
    </global>
</config>

In ./app/code/local/INPUTANAME/RewriteAfterDelete/Model/Observer.php

<?php
class INPUTANAME_RewriteAfterDelete_Model_Observer
{
    public function rewriteDelete(Varien_Event_Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        $storeId = Mage::app()->getStore()->getStoreId();
        $categoryIds = $product->getCategoryIds();
        if(count($categoryIds) ){
            $categoryIds = $this->getActiveCategoryIds($categoryIds);
        }
        $requestPath = '';
        if($product->getUrlPath() != ""){
            $requestPath = $product->getUrlPath();
        }else{
            $requestPath = $product->getUrlKey();
        }
        if(count($categoryIds) ){
            $firstCategoryId = $categoryIds[0];
            $category = Mage::getModel('catalog/category')->load($firstCategoryId);
            if($category->getProductCount()){
                $newpage = $category->getUrlPath();
            }else{
                $newpage = $category->getParentCategory()->getUrlPath();
            }
            Mage::getModel('core/url_rewrite')
                ->setIsSystem(0)
                ->setStoreId($storeId)   
                ->setOptions('RP') 
                ->setTargetPath($newpage)
                ->setIdPath(uniqid(delete_))
                ->setRequestPath($requestPath)
                ->save(); 
        }else{
            Mage::getModel('core/url_rewrite')
                ->setIsSystem(0)
                ->setStoreId($storeId)   
                ->setOptions('RP') 
                ->setTargetPath('index.php')
                ->setIdPath(uniqid(delete_))
                ->setRequestPath($requestPath)
                ->save(); 
        }
    }

    protected function getActiveCategoryIds($catIds){
        $catCollection = Mage::getResourceModel('catalog/category_collection')
             ->addAttributeToSelect('entity_id')
             ->addAttributeToFilter('entity_id', $catIds)
             ->addIsActiveFilter();
        return $catCollection->getAllIds();
    }

}

In ./app/etc/modules/INPUTANAME_RewriteAfterDelete.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <INPUTANAME_RewriteAfterDelete>
            <version>0.0.1</version>
        </INPUTANAME_RewriteAfterDelete>
    </modules>
    <global>
        <models>
            <inputaname_rewriteafterdelete>
                <class>INPUTANAME_RewriteAfterDelete_Model</class>
            </inputaname_rewriteafterdelete>
        </models>
        <events>
            <catalog_product_delete_after>
                <observers>
                    <inputaname_rewriteafterdelete>
                        <class>inputaname_rewriteafterdelete/observer</class>
                        <method>rewriteDelete</method>
                        <type>singleton</type>
                    </inputaname_rewriteafterdelete>
                </observers>
            </catalog_product_delete_after>
        </events>
    </global>
</config>

نصائح أخرى

some corrections & changes i had to implement to meets answer

  1. use catalog_product_delete_before event because getting $product->getCategoryIds() after product was deleted is returning an empty array
  2. use a query for product visibility before creating the rewrite because a rewrite is not needed for invisible products
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top