magento 获取自定义产品属性和属性值

在magento系统中经常要自定义自己的产品属性,在后台自定义的产品属性如何获取呢,下面根据属性类型的不同分别说明

产品属性要想在前台获取到需要设置属性的Used in Product Listing 为true


1、下拉列表的产品属性

如定义了一个表示产品的硬件支持类型的下拉列表属性support_hardware就可以这样获取

 $attributes['support_hardware']=$product->getAttributeText('support_hardware');

2、文本类型的产品属性

如定义了一个属性叫

$attributes['version_number']=$product->getData('version_number');


当得到产品对象后我们可以获取产品的各种属性,如果我们要获取满足一定属性条件的产品集呢?这时候就需要根据产品属性对产品过滤了

magento提供的根据属性过滤的接口有两种,一种是addAttributeToFilter,另一种是直接操作数据库通过getSelect()->where()的方法

1、addAttributeToFilter接口

比如我们想要获取APP产品支持的系统版本(属性name是system_version)在4.2以上的的产品集,就可以这么做

$_productCollection = Mage::getResourceModel('catalog/product_collection')
	                    ->setStoreId(1)
	                    ->addAttributeToSelect('*')
			    ->addAttributeToFilter('system_version',array('gteq'=>45));//45这个值是怎么得到的呢,这个值是system_version为4.2时对应的属性值,关于怎么获取属性的值在后面会讲


2、通过getSelect()->where()直接操作数据库

通过这种方式要求对数据库结构属性,通常我们会用

$_productCollection->getSelectSql()
来帮助我们写sql语句
$_productCollection->getSelect()->where('age',array('gteq'=>45));

怎么通过where写复杂的查询数据库语句会在另一篇中讲解


获取产品后通常还要加上对产品做产品是否是激活,是否在当前商店的判断

$product->isSalable()

下面讲下怎么获去属性的值

1、假设我们知道attribute的ID为149,就可以这样获取属性的value和label

$attributeOption=Mage::getResourceModel('eav/entity_attribute_option_collection')
                                    ->setPositionOrder('asc')
                                    ->setAttributeFilter(149)
                                    ->setStoreFilter()
                                    ->load();
        $attributeOptionArray=$attributeOption->toOptionArray();
	echo "<hr>";
	print_r($attributeOptionArray);

2、怎么根据attribute的name来得到attribute对象还在研究中


在实际应用时通常要获取可用来过滤产品的的属性,也就是filterable attributes,获取的方法如下:

protected function _getFilterableAttributes(){
        $layer = Mage::getModel("catalog/layer");
        $rootCategory=Mage::getModel('catalog/category')->load(Mage::app()->getStore()->getRootCategoryId());
        $layer->setCurrentCategory($rootCategory);
        $attributes = $layer->getFilterableAttributes();
        $this->_filterableAttributesExists=array();
        foreach ($attributes as $attribute) {
            //echo   $attribute->getAttributeCode(),"---",$attribute->getId(),"</br>";
            $this->_filterableAttributes[$attribute->getAttributeCode()]=$attribute->getId();
        }
        krsort($this->_filterableAttributes);
        return $this->_filterableAttributes;
    }



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Magento 2中发送多个附件的电子邮件,您需要对`Magento\Framework\Mail\Template\TransportBuilder`类进行扩展。 下面是一个示例代码,它可以让您在Magento 2中发送多个附件的电子邮件: 1. 创建 `Vendor\Module\Model\Mail\Template\TransportBuilder.php` 文件并添加以下代码: ```php <?php namespace Vendor\Module\Model\Mail\Template; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Exception\MailException; use Magento\Framework\Mail\Template\TransportBuilder as MagentoTransportBuilder; use Magento\Framework\Mail\TransportInterfaceFactory; use Magento\Framework\Translate\Inline\StateInterface; use Magento\Store\Model\StoreManagerInterface; class TransportBuilder extends MagentoTransportBuilder { /** * @var array */ protected $attachments = []; /** * @param array $attachments * @return $this */ public function addMultipleAttachment($attachments = []) { foreach ($attachments as $attachment) { if (file_exists($attachment['path'])) { $this->attachments[] = [ 'type' => $attachment['type'], 'name' => $attachment['name'], 'path' => $attachment['path'] ]; } } return $this; } /** * @param null|string|array $to * @param array $templateVars * @param null|string $templateOptions * @param null|string $transportOptions * * @throws MailException * * @return TransportInterfaceFactory */ public function getTransport( $to = null, array $templateVars = [], $templateOptions = null, $transportOptions = null ) { if (!empty($this->attachments)) { foreach ($this->attachments as $attachment) { $this->message->createAttachment( file_get_contents($attachment['path']), $attachment['type'], \Zend_Mime::DISPOSITION_ATTACHMENT, \Zend_Mime::ENCODING_BASE64, $attachment['name'] ); } } return parent::getTransport($to, $templateVars, $templateOptions, $transportOptions); } } ``` 2. 创建 `Vendor_Module` 模块的 `di.xml` 文件并添加以下代码: ```xml <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Framework\Mail\Template\TransportBuilder" type="Vendor\Module\Model\Mail\Template\TransportBuilder" /> </config> ``` 3. 在您的模块中使用以下代码发送多个附件的电子邮件: ```php <?php namespace Vendor\Module\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\Mail\Template\TransportBuilder; use Magento\Framework\Translate\Inline\StateInterface; use Magento\Store\Model\StoreManagerInterface; class SendEmail extends Action { /** * @var TransportBuilder */ protected $transportBuilder; /** * @var StateInterface */ protected $inlineTranslation; /** * @var StoreManagerInterface */ protected $storeManager; /** * @param Context $context * @param TransportBuilder $transportBuilder * @param StateInterface $inlineTranslation * @param StoreManagerInterface $storeManager */ public function __construct( Context $context, TransportBuilder $transportBuilder, StateInterface $inlineTranslation, StoreManagerInterface $storeManager ) { $this->transportBuilder = $transportBuilder; $this->inlineTranslation = $inlineTranslation; $this->storeManager = $storeManager; parent::__construct($context); } /** * @return void */ public function execute() { $attachmentOne = [ 'name' => 'Attachment One', 'path' => 'path/to/attachment/one.pdf', 'type' => 'application/pdf' ]; $attachmentTwo = [ 'name' => 'Attachment Two', 'path' => 'path/to/attachment/two.pdf', 'type' => 'application/pdf' ]; try { $this->inlineTranslation->suspend(); $this->transportBuilder->setTemplateIdentifier('your_email_template_id') ->setTemplateOptions([ 'area' => 'frontend', 'store' => $this->storeManager->getStore()->getId() ]) ->setTemplateVars([]) ->setFrom([ 'email' => 'sender@example.com', 'name' => 'Sender Name' ]) ->addTo('recipient@example.com', 'Recipient Name') ->addMultipleAttachment([$attachmentOne, $attachmentTwo]) ->getTransport() ->sendMessage(); $this->inlineTranslation->resume(); $this->messageManager->addSuccess(__('Your email was sent successfully.')); } catch (\Exception $e) { $this->inlineTranslation->resume(); $this->messageManager->addError(__('There was an error sending your email. Please try again later.')); } return $this->_redirect('*/*/index'); } } ``` 以上代码将会发送带有两个附件的电子邮件。您可以根据自己的需要更改附件的数量和详细信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值