English 中文(简体)
Magento: 删除产品,301 改用现有的相关网页,例如母体类别。
原标题:Magento: Delete products and 301 redirect to relevant existing page, e.g., it s parent category
  • 时间:2012-05-01 02:15:41
  •  标签:
  • magento

在Magento被删除的产品中,有301个永久转子可供选择?

我有5 000种产品。 4 000人已经出售,永远不会回落。 我想删除这些产品b/c 我不再需要这些产品,并希望清理/减少我的数据库,但我需要301份将其长期转至适当的网页,以保持《全球环境展望》的判断一。

显然,我不想再活掉所删除的产品页,甚至不去掉储存信息,我不想再活404页。

理想的情况是,我能够出口URL产品及其最低类别网页URL,以便我能够通过一个接驳文件创建301个转头。 但是,从Magento只出口一类的身份证,而不是类别名称或URLs的出口,因此我如何从方案上获得这些证件? 还是其他解决办法?

问题回答

I would create a plugin that leverages observers and Magento s URL Rewrite Manager.

This plugin would observe the catalog_controller_product_delete event which returns array(’product’ ⇒ $product) to the Observer s $event var upon product delete - this would create automatic redirects upon deletion.

您可以利用这一途径获得有关该产品包括该产品的最后一刻细节,然后做以下工作:

Mage::getModel( core/url_rewrite )
    ->setIsSystem(0)
    ->setStoreId($storeId)   
    ->setOptions( RP )  //301 redirect perm
    ->setTargetPath($product->getUrlPath() .  .html )
    ->setRequestPath($newpage->getUrlPath() .  .html )
    ->save();

The following solution will only work if your products have only 1 category. This code will work deleting at admin product grid and admin product view.

This code will make a redirect to the category the product is in. Except if that category has no products after deleting, it will use the parent category of the product category instead. This creates a problem because many of the rewrites will still link to a category without products. If this is not a problem for you use the following solution.

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 BeoService_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) ){
            $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 )  //301 redirect perm
                ->setTargetPath($newpage .  .html )
                ->setRequestPath($product->getUrlPath() .  .html )
                ->save(); 
        }
    }
}

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>

To get the data you want, you can simply run this query

SELECT prod.request_path, cat.request_path FROM `core_url_rewrite` prod
LEFT JOIN core_url_rewrite cat ON prod.category_id = cat.category_id AND cat.id_path LIKE  category/% 
WHERE prod.id_path LIKE  product/%  AND prod.category_id IS NOT NULL

要想增加大量固定的转口,就必须放慢速度。

这一功能只能由大家来调整。 因此,希望每个人都能帮助实现这一目标。

<?php
class Meet_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();
    }

}




相关问题
Magento addFieldToFilter allow NULLs

When using the Magento collection method addFieldToFilter is it possible to allow filtering by NULL values? I want to select all the products in a collection that have a custom attribute even if no ...

Get product link from Magento API

I am new to Magento and using their API. I need to be able to get the product url from the API call. I see that I can access the url_key and url_path, but unfortunately that s not necessarily what ...

magento extension installation

I want to install a Magento extension in WAMP, but not from the Magento connect system. How can I do this? I have the module (extension) code and I already installed the sample data in the Magento ...

Link to a specific step in onepage checkout

Is it possible to redirect the browser to nth step in the onepage checkout? If so, how would one go about doing it? I m working on a payment module and have a sort of "cancel" action that i would ...

How do I filter a collection by a YesNo type attribute?

I have a ‘featured’ attribute, which has a Yes/No select-list as the admin input. I presume that the values for Yes and No are 1 and 0, as they are for every other Yes/No list. However, if I try and ...

热门标签