本文整理汇总了PHP中Magento\Catalog\Model\Product::getExtensionAttributes方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::getExtensionAttributes方法的具体用法?PHP Product::getExtensionAttributes怎么用?PHP Product::getExtensionAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Catalog\Model\Product
的用法示例。
在下文中一共展示了Product::getExtensionAttributes方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: assertEqualsSpecificAttributes
/**
* @param \Magento\Catalog\Model\Product $expectedProduct
* @param \Magento\Catalog\Model\Product $actualProduct
*/
protected function assertEqualsSpecificAttributes($expectedProduct, $actualProduct)
{
$expectedBundleProductOptions = $expectedProduct->getExtensionAttributes()->getBundleProductOptions();
$actualBundleProductOptions = $actualProduct->getExtensionAttributes()->getBundleProductOptions();
$this->assertEquals(count($expectedBundleProductOptions), count($actualBundleProductOptions));
$expectedBundleProductOptionsToCompare = [];
foreach ($expectedBundleProductOptions as $expectedBundleProductOption) {
$expectedBundleProductOptionsToCompare[$expectedBundleProductOption->getTitle()]['type'] = $expectedBundleProductOption->getType();
foreach ($expectedBundleProductOption->getProductLinks() as $productLink) {
$expectedBundleProductOptionsToCompare[$expectedBundleProductOption->getTitle()]['product_links'][] = $productLink->getSku();
}
}
$actualBundleProductOptionsToCompare = [];
foreach ($actualBundleProductOptions as $actualBundleProductOption) {
$actualBundleProductOptionsToCompare[$actualBundleProductOption->getTitle()]['type'] = $actualBundleProductOption->getType();
foreach ($actualBundleProductOption->getProductLinks() as $productLink) {
$actualBundleProductOptionsToCompare[$actualBundleProductOption->getTitle()]['product_links'][] = $productLink->getSku();
}
}
$this->assertEquals(count($expectedBundleProductOptions), count($actualBundleProductOptions));
foreach ($expectedBundleProductOptionsToCompare as $key => $expectedBundleProductOption) {
$this->assertEquals($expectedBundleProductOption['type'], $actualBundleProductOptionsToCompare[$key]['type']);
$expectedProductLinks = $expectedBundleProductOption['product_links'];
$actualProductLinks = $actualBundleProductOptionsToCompare[$key]['product_links'];
sort($expectedProductLinks);
sort($actualProductLinks);
$this->assertEquals($expectedProductLinks, $actualProductLinks);
}
}
示例2: getTargetLink
/**
* Retrieve product link by its ID (or first link if ID is not specified)
*
* @param Product $product
* @param int|null $linkId
* @return Link|null
*/
protected function getTargetLink(Product $product, $linkId = null)
{
$links = $product->getExtensionAttributes()->getDownloadableProductLinks();
if ($linkId !== null) {
return isset($links[$linkId]) ? $links[$linkId] : null;
}
// return first link
return reset($links);
}
示例3: afterLoad
/**
* Add stock item information to the product's extension attributes
*
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Catalog\Model\Product
*/
public function afterLoad(\Magento\Catalog\Model\Product $product)
{
$productExtension = $product->getExtensionAttributes();
if ($productExtension === null) {
$productExtension = $this->productExtensionFactory->create();
}
// stockItem := \Magento\CatalogInventory\Api\Data\StockItemInterface
$productExtension->setStockItem($this->stockRegistry->getStockItem($product->getId()));
$product->setExtensionAttributes($productExtension);
return $product;
}
示例4: build
/**
* Duplicating bundle options and selections
*
* @param Product $product
* @param Product $duplicate
* @return void
*/
public function build(Product $product, Product $duplicate)
{
if ($product->getTypeId() != Type::TYPE_BUNDLE) {
//do nothing if not bundle
return;
}
$bundleOptions = $product->getExtensionAttributes()->getBundleProductOptions();
$duplicatedBundleOptions = [];
foreach ($bundleOptions as $key => $bundleOption) {
$duplicatedBundleOptions[$key] = clone $bundleOption;
}
$duplicate->getExtensionAttributes()->setBundleProductOptions($duplicatedBundleOptions);
}
示例5: afterLoad
/**
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Catalog\Model\Product
*/
public function afterLoad(\Magento\Catalog\Model\Product $product)
{
if ($product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
return $product;
}
$productExtension = $product->getExtensionAttributes();
if ($productExtension === null) {
$productExtension = $this->productExtensionFactory->create();
}
$productExtension->setConfigurableProductOptions($this->getOptions($product));
$productExtension->setConfigurableProductLinks($this->getLinkedProducts($product));
$product->setExtensionAttributes($productExtension);
return $product;
}
示例6: assertEqualsSpecificAttributes
/**
* @param \Magento\Catalog\Model\Product $expectedProduct
* @param \Magento\Catalog\Model\Product $actualProduct
*/
protected function assertEqualsSpecificAttributes($expectedProduct, $actualProduct)
{
$expectedProductLinks = $expectedProduct->getExtensionAttributes()->getDownloadableProductLinks();
$expectedProductSamples = $expectedProduct->getExtensionAttributes()->getDownloadableProductSamples();
$actualProductLinks = $actualProduct->getExtensionAttributes()->getDownloadableProductLinks();
$actualProductSamples = $actualProduct->getExtensionAttributes()->getDownloadableProductSamples();
$this->assertEquals(count($expectedProductLinks), count($actualProductLinks));
$this->assertEquals(count($expectedProductSamples), count($actualProductSamples));
$expectedLinksArray = [];
foreach ($expectedProductLinks as $link) {
$expectedLinksArray[] = $link->getData();
}
foreach ($actualProductLinks as $actualLink) {
$this->assertContains($expectedLinksArray, $actualLink->getData());
}
$expectedSamplesArray = [];
foreach ($expectedProductSamples as $sample) {
$expectedSamplesArray[] = $sample->getData();
}
foreach ($actualProductSamples as $actualSample) {
$this->assertContains($expectedSamplesArray, $actualSample->getData());
}
}
示例7: getTargetLink
/**
* Retrieve product link by its ID (or first link if ID is not specified)
*
* @param Product $product
* @param int|null $linkId
* @return Link|null
*/
protected function getTargetLink(Product $product, $linkId = null)
{
$links = $product->getExtensionAttributes()->getDownloadableProductLinks();
if ($linkId !== null) {
if (!empty($links)) {
foreach ($links as $link) {
if ($link->getId() == $linkId) {
return $link;
}
}
}
return null;
}
// return first link
return reset($links);
}
示例8: getTargetSample
/**
* Retrieve product sample by its ID (or first sample if ID is not specified)
*
* @param Product $product
* @param int|null $sampleId
* @return Sample|null
*/
protected function getTargetSample(Product $product, $sampleId = null)
{
$samples = $product->getExtensionAttributes()->getDownloadableProductSamples();
if ($sampleId) {
/* @var $sample \Magento\Downloadable\Model\Sample */
if ($samples) {
foreach ($samples as $sample) {
if ($sample->getId() == $sampleId) {
return $sample;
}
}
}
return null;
}
// return first sample
return $samples[0];
}
示例9: afterLoad
/**
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Catalog\Model\Product
*/
public function afterLoad(\Magento\Catalog\Model\Product $product)
{
if ($product->getTypeId() != \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) {
return $product;
}
$productExtension = $product->getExtensionAttributes() ?: $this->productExtensionFactory->create();
$links = $this->linkRepository->getLinksByProduct($product);
if ($links !== null) {
$productExtension->setDownloadableProductLinks($links);
}
$samples = $this->sampleRepository->getSamplesByProduct($product);
if ($samples !== null) {
$productExtension->setDownloadableProductSamples($samples);
}
$product->setExtensionAttributes($productExtension);
return $product;
}
示例10: afterInitialize
/**
* Prepare product to save
*
* @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject
* @param \Magento\Catalog\Model\Product $product
*
* @return \Magento\Catalog\Model\Product
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function afterInitialize(\Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject, \Magento\Catalog\Model\Product $product)
{
if ($downloadable = $this->request->getPost('downloadable')) {
$product->setDownloadableData($downloadable);
$extension = $product->getExtensionAttributes();
if (isset($downloadable['link']) && is_array($downloadable['link'])) {
$links = [];
foreach ($downloadable['link'] as $linkData) {
if (!$linkData || isset($linkData['is_delete']) && $linkData['is_delete']) {
continue;
} else {
$links[] = $this->getLinkBuilder()->setData($linkData)->build($this->getLinkFactory()->create());
}
}
$extension->setDownloadableProductLinks($links);
}
if (isset($downloadable['sample']) && is_array($downloadable['sample'])) {
$samples = [];
foreach ($downloadable['sample'] as $sampleData) {
if (!$sampleData || isset($sampleData['is_delete']) && (bool) $sampleData['is_delete']) {
continue;
} else {
$samples[] = $this->getSampleBuilder()->setData($sampleData)->build($this->getSampleFactory()->create());
}
}
$extension->setDownloadableProductSamples($samples);
}
$product->setExtensionAttributes($extension);
if ($product->getLinksPurchasedSeparately()) {
$product->setTypeHasRequiredOptions(true)->setRequiredOptions(true);
} else {
$product->setTypeHasRequiredOptions(false)->setRequiredOptions(false);
}
}
return $product;
}
示例11: processBundleOptionsData
/**
* @param \Magento\Catalog\Model\Product $product
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function processBundleOptionsData(\Magento\Catalog\Model\Product $product)
{
$bundleOptionsData = $product->getBundleOptionsData();
if (!$bundleOptionsData) {
return;
}
$options = [];
foreach ($bundleOptionsData as $key => $optionData) {
if ((bool) $optionData['delete']) {
continue;
}
$option = $this->optionFactory->create(['data' => $optionData]);
$option->setSku($product->getSku());
$option->setOptionId(null);
$links = [];
$bundleLinks = $product->getBundleSelectionsData();
if (empty($bundleLinks[$key])) {
continue;
}
foreach ($bundleLinks[$key] as $linkData) {
if ((bool) $linkData['delete']) {
continue;
}
$link = $this->linkFactory->create(['data' => $linkData]);
if ((int) $product->getPriceType() !== \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC) {
if (array_key_exists('selection_price_value', $linkData)) {
$link->setPrice($linkData['selection_price_value']);
}
if (array_key_exists('selection_price_type', $linkData)) {
$link->setPriceType($linkData['selection_price_type']);
}
}
$linkProduct = $this->productRepository->getById($linkData['product_id']);
$link->setSku($linkProduct->getSku());
$link->setQty($linkData['selection_qty']);
if (array_key_exists('selection_can_change_qty', $linkData)) {
$link->setCanChangeQuantity($linkData['selection_can_change_qty']);
}
$links[] = $link;
}
$option->setProductLinks($links);
$options[] = $option;
}
$extension = $product->getExtensionAttributes();
$extension->setBundleProductOptions($options);
$product->setExtensionAttributes($extension);
return;
}
示例12: getExtensionAttributes
/**
* {@inheritdoc}
*/
public function getExtensionAttributes()
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getExtensionAttributes');
if (!$pluginInfo) {
return parent::getExtensionAttributes();
} else {
return $this->___callPlugins('getExtensionAttributes', func_get_args(), $pluginInfo);
}
}
示例13: afterInitialize
/**
* Prepare product to save
*
* @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject
* @param \Magento\Catalog\Model\Product $product
*
* @return \Magento\Catalog\Model\Product
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function afterInitialize(\Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject, \Magento\Catalog\Model\Product $product)
{
if ($downloadable = $this->request->getPost('downloadable')) {
$product->setDownloadableData($downloadable);
$extension = $product->getExtensionAttributes();
if (isset($downloadable['link']) && is_array($downloadable['link'])) {
$links = [];
foreach ($downloadable['link'] as $linkData) {
if (!$linkData || isset($linkData['is_delete']) && (bool) $linkData['is_delete']) {
continue;
} else {
unset($linkData['link_id']);
// TODO: need to implement setLinkFileContent()
$link = $this->linkFactory->create(['data' => $linkData]);
if (isset($linkData['type'])) {
$link->setLinkType($linkData['type']);
}
if (isset($linkData['file'])) {
$link->setLinkFile($linkData['file']);
}
if (isset($linkData['file_content'])) {
$link->setLinkFileContent($linkData['file_content']);
}
$link->setId(null);
if (isset($linkData['sample']['type'])) {
$link->setSampleType($linkData['sample']['type']);
}
if (isset($linkData['sample']['file'])) {
$link->setSampleFile($linkData['sample']['file']);
}
if (isset($linkData['sample']['url'])) {
$link->setSampleUrl($linkData['sample']['url']);
}
if (isset($linkData['sample']['file_content'])) {
$link->setSampleFileContent($linkData['file_content']);
}
$link->setStoreId($product->getStoreId());
$link->setWebsiteId($product->getStore()->getWebsiteId());
$link->setProductWebsiteIds($product->getWebsiteIds());
if (!$link->getSortOrder()) {
$link->setSortOrder(1);
}
if (null === $link->getPrice()) {
$link->setPrice(0);
}
if ($link->getIsUnlimited()) {
$link->setNumberOfDownloads(0);
}
$links[] = $link;
}
}
$extension->setDownloadableProductLinks($links);
}
if (isset($downloadable['sample']) && is_array($downloadable['sample'])) {
$samples = [];
foreach ($downloadable['sample'] as $sampleData) {
if (!$sampleData || isset($sampleData['is_delete']) && (bool) $sampleData['is_delete']) {
continue;
} else {
unset($sampleData['sample_id']);
$sample = $this->sampleFactory->create(['data' => $sampleData]);
$sample->setId(null);
$sample->setStoreId($product->getStoreId());
if (isset($sampleData['type'])) {
$sample->setSampleType($sampleData['type']);
}
if (isset($sampleData['sample_url'])) {
$sample->setSampleUrl($sampleData['sample_url']);
}
if (!$sample->getSortOrder()) {
$sample->setSortOrder(1);
}
$samples[] = $sample;
}
}
$extension->setDownloadableProductSamples($samples);
}
$product->setExtensionAttributes($extension);
if ($product->getLinksPurchasedSeparately()) {
$product->setTypeHasRequiredOptions(true)->setRequiredOptions(true);
} else {
$product->setTypeHasRequiredOptions(false)->setRequiredOptions(false);
}
}
return $product;
}