【问题标题】:Magento bundle products: child option sort order issueMagento 捆绑产品:子选项排序顺序问题
【发布时间】:2012-12-17 03:52:16
【问题描述】:

我遇到了一个问题,其中捆绑产品的子选项/产品在交易电子邮件中没有正确排序。

排序顺序很重要,在这种情况下,因为产品型号是选项之一,需要首先显示。

在template/bundle/email/order/items/order/default.phtml中,我们有视图逻辑来吐出产品细节:

<?php $_item = $this->getItem() ?>
<?php $_order=$this->getOrder() ?>

<?php $parentItem = $this->getItem() ?>
<?php $items = array_merge(array($parentItem), $parentItem->getChildrenItems()); ?>

<?php foreach ($items as $_item): ?>

// etc...

像往常一样,子产品的排序顺序在管理员中定义,并在产品视图、购物车、结帐等中正常显示......但在电子邮件中显示时订单会被修改。

奇怪的是顺序也不是按字母顺序排列的。其实,这似乎有点武断——奇怪……

无论如何,我应该如何对 $items 数组进行排序以匹配预期的排序顺序?

注意 = 这是在 admin->catalog->product->bundle items->position 中设置的值。


更新

这是我最终确定的辅助方法:

/**
 * @codepool   Local
 * @package    Namespace
 * @module     Module
 */

class Namespace_Module_Helper_Product_Sort extends Mage_Core_Helper_Abstract
{

    /*
    * @param $item : bundle product 
    * @return array
    */
    public function sortBundleChildrenByPosition( $item )
    {
        $sorted = array();
        $sorted[] = $item;

        //Mage::log('Namespace_Module_Helper_Product_Bundle::sortChildrenByPosition /n'.print_r($item->debug(), true));

        // grab the product (bundle parent)
        $_product = $item->getProduct();

        // grab child order items
        $_children = $item->getChildrenItems();

        // grab sort order for options
        $optionCollection = $_product->getTypeInstance(true)->getOptionsCollection($_product);

        // iterate over options, comparing title / label, if they match, toss into return array
        foreach ($optionCollection as $option){

            //Mage::log('$option:'.print_r($option->debug(), true));
            $title = $option['default_title'];

            foreach ($_children as $child){
                $prodOption = $child->getProductOptions();
                $bundleSelectionAttributes = unserialize($prodOption['bundle_selection_attributes']);
                //Mage::log('$_children=>$child:product_options[bundle_selection_attributes]'.print_r($bundleSelectionAttributes, true));

                if ($bundleSelectionAttributes['option_label'] == $title){
                    //Mage::log('$bundleSelectionAttributes[option_label] == '.$title.'. $sorted[] ='. print_r($child->debug(), true) );
                    $sorted[] = $child;
                    continue; 
                }
            }

        }

        return $sorted;
    }

}

我在 template/bundle/email/order/items/order/default.phtml 中调用它(或任何需要排序的捆绑包):

<?php 
    $_item = $this->getItem(); 
    $_order=$this->getOrder(); 

    $parentItem = $this->getItem(); 

    //$items = array_merge( array($parentItem), $parentItem->getChildrenItems()); 

    $items = $this->helper('Namespace_Module/Product_Sort')->sortBundleChildrenByPosition( $_item );
?>

...我 1000% 确定有一些内置方法可以做到这一点,而且可能更干净。但经过几个小时的搜寻,我找不到它。

这行得通。就是这样。

【问题讨论】:

    标签: magento bundle product transactional


    【解决方案1】:

    一种解决方法:

    • 循环遍历所有 $items 并创建一个包含 id(作为键)和名称(作为值)的数组,假设它名为 $sortedPrd。
    • 对新数组的 ($sortedPrd) 值进行排序(按字母顺序)
    • 循环通过$sortedPrd 并使用它的密钥从$items 中提取给定的产品

    更新

    这里是一些获取捆绑选项(子产品)位置的代码:

    $optionCollection = $_product->getTypeInstance(true)->getOptionsCollection($_product);
    foreach ($optionCollection as $prdOptions){
    
      $position = $prdOptions->getPosition();
    }
    

    有关更多信息,请参阅此问题:Magento: how to load product along its all data as it is used in admin

    【讨论】:

    • 是的 = 我认为这可能是一个不错的选择...您是否知道我可以如何获取子产品的“sortOrder”值?它是 EAV 吗?
    • 确保您可以使用它来获取所有捆绑选项:$_product->getTypeInstance(true)->getOptionsCollection($_product);
    • 你可以循环遍历每一个并通过调用$options->getPosition();得到位置。 (或者你可以 var_dump($option->getData()) 查看所有选项)
    • 或者你可以使用 usort 排序;)
    • @Bosworth99 感谢您在上面分享您的实现,这将有助于未来有问题的人
    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多