Magento的2.如何解决与检索类别和产品属性的错误

问题描述 投票:0回答:1

我已经调整了下面的代码执行两个不同的功能,将获取和检索Magento的2模板中的一些类别和产品属性。但是,代码不正确执行,我只能得到一个白色的屏幕上看到。任何想法这哪里出了问题?我没有得到任何语法错误,看看。

任何想法受到了高度评​​价。

代码如下:

<?php

$objectManager      = \Magento\Framework\App\ObjectManager::getInstance();
$product            = $objectManager->get('Magento\Framework\Registry')->registry('current_product');

$offertelink        = $product->getData('offerte_link');
$image              = $block->getUrl('pub/media/catalog').'product'.$product->getData('grootmateriaal_extra_image_int');

$_categoryHelper    = $this->helper('Custom\Config\Helper\Category');
$_productHelper     = $this->helper('Custom\Config\Helper\Product');

$attributes = array_slice(explode(',', $_categoryHelper->getCurrentCategoryData('category_attributes')),0, 6);
$getIconsValues = $product->getResource()->getAttribute('icons')->getFrontend()->getValue($product);

function getCategoryAttributes($_categoryHelper) {

    if($_categoryHelper->getCurrentCategoryData('category_attributes')):
        foreach($attributes as $attr):
        $attr = $_productHelper->getAttributeByCode(trim($attr));

        echo '<div class="pd-icon pd-icon-'.$attr.'">';
            echo $_productHelper->getAttributeText($product, $attr);
        echo '</div>';

    endforeach; endif;

    echo '<div class="pd-icon-button-container">';
        echo '<a class="button large cta" href="<?php echo $offertelink; ?>"><span>';
        echo __('Ask');
        echo '</span></a>';
    echo '</div>';

    echo '<div class="pd-icon-button-container">';
        echo '<a class="button open" href="#amasty-downloads"><span>';
        echo __('Download');
        echo '</span></a>';
    echo '</div>';

    if ($image):
        echo '<div class="pd-icon-image-container">';
            echo '<img src="'.$image.'" alt="">';
        echo '</div>';
    endif;
}

function getProductAttributes($_categoryHelper) {
$iconen_array = explode(',', $getIconsValues);
    foreach($iconen_array as $a):
        echo '<div class="pd-icon pd-icon-'.strtolower(trim($a)).'">';
        $attr = $product->getResource()->getAttribute(strtolower(trim($a)));
        echo $attr->getFrontend()->getValue($product);
        echo '</div>';
    endforeach;
}

?>

<div class="pd-icon-container">

    <?php if($product->hasData('icons')): ?>

    <?php if ( empty ($getIconsValues) ):
        getCategoryAttributes($objectManager); ?>

    <?php else:
        getProductAttributes($objectManager); ?>
    <?php endif; ?>

    <?php else:
        getCategoryAttributes($objectManager); ?>

    <?php endif; ?>

</div>
php function magento2
1个回答
0
投票

我没有检查你的代码now.I可以给你更喜欢一些建议来避免直接使用对象管理器使用来获取数据。在这里,我可以告诉你用于获取类别列表中的例子,你可以改变你的方式,你也可以添加属性功能一样得到。

推荐链接,避免直接使用对象管理器:[https://devdocs.magento.com/guides/v2.3/extension-dev-guide/object-manager.html][1]

使用依赖注入可以使用下面的例子为获取类的代码:

希望你熟悉创建新模块

示例文件路径:应用程序/代码/ YourVendorName / YourModuleName /座/ YourCustomBlock.php

<?php
namespace YourVendorName\YourModuleName\Block;
class YourCustomBlock extends \Magento\Framework\View\Element\Template
{ 
    protected $_categoryCollectionFactory;

    protected $_categoryHelper;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
        \Magento\Catalog\Helper\Category $categoryHelper,
        array $data = []
    ) {
        $this->_categoryCollectionFactory = $categoryCollectionFactory;
        $this->_categoryHelper = $categoryHelper;
        parent::__construct($context, $data);
    }

    public function getCategoryCollection($isActive = true, $level = false, $sortBy = false, $pageSize = false) {
        $collection = $this->_categoryCollectionFactory->create();
        $collection->addAttributeToSelect('*');

        // select only active categories
        if ($isActive) {
            $collection->addIsActiveFilter();
        }

        // select categories of certain level
        if ($level) {
            $collection->addLevelFilter($level);
        }

        // sort categories by some value
        if ($sortBy) {
            $collection->addOrderField($sortBy);
        }

        // set pagination
        if ($pageSize) {
            $collection->setPageSize($pageSize); 
        } 

        return $collection;
    }

    public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true) {
        return $this->_categoryHelper->getStoreCategories($sorted = false, $asCollection = false, $toLoad = true);
    }
}

现在,我们可以在视图(一个.phtml)文件中使用的功能如下

// get the list of all categories
$categories = $block->getCategoryCollection(); 
foreach ($categories as $category) {
    echo $category->getId() . '<br />';
    echo $category->getName() . '<br />';
}

// get categories sorted by category name
$categories = $block->getCategoryCollection(true, false, 'name', false);
foreach ($categories as $category) { 
    echo $category->getId() . '<br />';
    echo $category->getName() . '<br />';
}

// get current store’s categories
$categories = $block->getStoreCategories();
foreach ($categories as $category) { 
    echo $category->getId() . '<br />';
    echo $category->getName() . '<br />';
}

如果回答对你有帮助,请不要忘记打勾。

© www.soinside.com 2019 - 2024. All rights reserved.