【问题标题】:Make Magento "Continue Shopping" button redirect to the last-added-to-cart product's category使 Magento 的“继续购物”按钮重定向到最后添加到购物车的产品类别
【发布时间】:2012-08-31 21:19:26
【问题描述】:

购物车页面上的“继续购物”按钮无法正常工作。

当我点击按钮时,然后转到主页。

我想去上一个分类页面。

【问题讨论】:

    标签: magento continue


    【解决方案1】:

    您描述的按钮确实有效。返回首页可能是 Magento 的标准行为之一。

    要回答您的问题,您可以执行以下操作。

    请注意,如果产品存在于多个类别中,这将重定向到它所附加的第一个类别。

    这些代码已在 Magento 1.7.0.0 上成功测试。

    PHP 代码如下:

    <?php
        $lastProductAddedToCartId = Mage::getSingleton('checkout/session')->getLastAddedProductId();
        if($lastProductAddedToCartId) {
            $productCategoryIdsArray = Mage::getModel('catalog/product')->load($lastProductAddedToCartId)->getCategoryIds();
            $continueShoppingCategoryUrl = Mage::getModel('catalog/category')->load($productCategoryIdsArray[0])->getUrl();
        }
    ?>
    

    HTML 按钮代码为:

    <button type="button" title="Continue Shopping" class="button btn-continue" onclick="setLocation('<?php echo (isset($continueShoppingCategoryUrl)) ? $continueShoppingCategoryUrl : $this->getContinueShoppingUrl(); ?>')"><span><span>Continue Shopping</span></span></button>
    

    例如,如果您将 PHP 代码放在 template/checkout/cart.phtml 文件的开头,则上述代码可以正常工作,这不是最佳做法。

    最佳做法是:

    1) 您可以在按钮的setLocation() PHP 参数中调用您自己的 Helper,如下所示:

    setLocation('<?php echo (Mage::helper('myhelper')->getContinueShoppingCategoryUrl()) ? Mage::helper('myhelper')->getContinueShoppingCategoryUrl() : $this->getContinueShoppingUrl(); ?>')
    

    2) 或(IMO 不太好),重写Mage_Checkout_Block_Cart::getContinueShoppingUrl() 方法。

    【讨论】:

    • 感谢您的详细描述。但是当我点击“继续购物”按钮时,转到产品详细信息页面。我想去产品分类页面。
    • 请看我的其他回答。仅作记录:仅当您在添加产品后被重定向到购物车 right 时,它才会进入类别页面。如果您在其他任何时候去购物车,Magento 无法分辨出最后添加的产品是哪一个,如果其中有多个产品则更是如此。如果项目不属于同一类别,Magento 如何判断要转到哪个类别?请尝试将产品添加到购物车并在添加后立即单击“继续购物”。它会起作用的。
    【解决方案2】:

    在对我的第一个答案发表评论后,我制作了一个更复杂的脚本,该脚本作为第一个答案更需要资源,因此我将其作为单独的答案发布。请注意,两者在 PHP 端和 PHTML 端确实不同。

    这是我在 Magento CE 1.7.0.0 上成功测试的内容。 下面的场景在我提供的注释代码中按预期工作。


    目录配置

    A 类(家具)

    • 产品 1(沙发)

    B 类(电子)

    • 产品 2(桌面)
    • 产品 3(笔记本电脑)

    场景

    a) 将产品 1 添加到购物车 => “继续购物”在添加后立即重定向到类别 A

    b) 导航网站并返回购物车而不添加新产品 => “继续购物”重定向到类别 A

    c) 将产品 2 添加到购物车 => “继续购物”在添加后立即重定向到类别 B

    d) 导航网站并返回购物车而不添加新产品 =>“继续购物”重定向到主页,因为购物车中的产品不属于同一类别。

    e) 从购物车中删除产品 1 =>“继续购物”总是重定向到类别 B

    f) 将产品 3 添加到购物车 => “继续购物”总是重定向到类别 B


    PHP 代码

    <?php
        $continueShoppingCategoryUrl = false;
    
        /**
         * If we are on the cart page just after we added an item to the cart,
         * we use its category for "Continue Shopping" redirect
         */
        $lastProductAddedToCartId = Mage::getSingleton('checkout/session')->getLastAddedProductId();
        if($lastProductAddedToCartId) {
            $productCategoryIdsArray = Mage::getModel('catalog/product')->load($lastProductAddedToCartId)->getCategoryIds();
            $continueShoppingCategoryUrl = Mage::getModel('catalog/category')->load($productCategoryIdsArray[0])->getUrl();
        }
    
        /**
         * Otherwise, if we are on the cart page at any other moment, we make sure
         * that all items do belong to the same category and, if this is
         * the case, we use this unique category for "Continue Shopping" redirect
         * 
         * If all cart items do not belong to the same category, we are
         * compelled to let Magento process in its standard way because we 
         * cannot tell which category is the one to redirect to!
         */
        if(!$continueShoppingCategoryUrl) {
            $allCategoryIds = array();
            $cartItems = Mage::helper('checkout/cart')->getQuote()->getAllVisibleItems();
            foreach($cartItems as $cartItem) {
                $productCategoryIds = Mage::getModel('catalog/product')->load($cartItem->getProductId())->getCategoryIds();
                $allCategoryIds = array_merge($allCategoryIds, $productCategoryIds);
            }
            $allCategoryIds = array_unique($allCategoryIds);
            if(count($allCategoryIds) === 1) {
                $continueShoppingCategoryUrl = Mage::getModel('catalog/category')->load(reset($allCategoryIds))->getUrl();
            }
        }
    ?>
    

    HTML 按钮代码

    <button type="button" title="Continue Shopping" class="button btn-continue" onclick="setLocation('<?php echo ($continueShoppingCategoryUrl) ? $continueShoppingCategoryUrl : $this->getContinueShoppingUrl(); ?>')"><span><span>Continue Shopping</span></span></button>
    

    最佳实践提醒

    同样,最佳做法是在按钮上调用 Helper,而不是在 cart.phtml 模板中使用原始 PHP 代码。

    【讨论】:

    • 我知道这是一年前的事了,但你会碰巧知道如何按商店过滤吗?正如您上面提到的,它属于产品所在的第一个类别,即使在不同的商店中也是如此。我尝试了多种过滤方法,但似乎都不起作用。
    猜你喜欢
    • 1970-01-01
    • 2014-08-30
    • 2023-04-05
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多