本文整理汇总了PHP中Magento\Catalog\Model\Product::setData方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::setData方法的具体用法?PHP Product::setData怎么用?PHP Product::setData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Catalog\Model\Product
的用法示例。
在下文中一共展示了Product::setData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: afterLoad
/**
* Set inventory data to custom attribute
*
* @param Product $object
* @return $this
*/
public function afterLoad($object)
{
$stockItem = $this->stockRegistry->getStockItem($object->getId(), $object->getStore()->getWebsiteId());
$object->setData($this->getAttribute()->getAttributeCode(), ['is_in_stock' => $stockItem->getIsInStock(), 'qty' => $stockItem->getQty()]);
return parent::afterLoad($object);
}
示例2: testGetCustomAttributes
public function testGetCustomAttributes()
{
$priceCode = 'price';
$colorAttributeCode = 'color';
$interfaceAttribute = $this->getMock('\\Magento\\Framework\\Api\\MetadataObjectInterface');
$interfaceAttribute->expects($this->once())->method('getAttributeCode')->willReturn($priceCode);
$colorAttribute = $this->getMock('\\Magento\\Framework\\Api\\MetadataObjectInterface');
$colorAttribute->expects($this->once())->method('getAttributeCode')->willReturn($colorAttributeCode);
$customAttributesMetadata = [$interfaceAttribute, $colorAttribute];
$this->metadataServiceMock->expects($this->once())->method('getCustomAttributesMetadata')->willReturn($customAttributesMetadata);
$this->model->setData($priceCode, 10);
//The color attribute is not set, expect empty custom attribute array
$this->assertEquals([], $this->model->getCustomAttributes());
//Set the color attribute;
$this->model->setData($colorAttributeCode, "red");
$attributeValue = new \Magento\Framework\Api\AttributeValue();
$attributeValue2 = new \Magento\Framework\Api\AttributeValue();
$this->attributeValueFactory->expects($this->exactly(2))->method('create')->willReturnOnConsecutiveCalls($attributeValue, $attributeValue2);
$this->assertEquals(1, count($this->model->getCustomAttributes()));
$this->assertNotNull($this->model->getCustomAttribute($colorAttributeCode));
$this->assertEquals("red", $this->model->getCustomAttribute($colorAttributeCode)->getValue());
//Change the attribute value, should reflect in getCustomAttribute
$this->model->setData($colorAttributeCode, "blue");
$this->assertEquals(1, count($this->model->getCustomAttributes()));
$this->assertNotNull($this->model->getCustomAttribute($colorAttributeCode));
$this->assertEquals("blue", $this->model->getCustomAttribute($colorAttributeCode)->getValue());
}
示例3: testGetObsoleteGetters
/**
* @covers \Magento\Catalog\Model\Product::getCalculatedFinalPrice
* @covers \Magento\Catalog\Model\Product::getMinimalPrice
* @covers \Magento\Catalog\Model\Product::getSpecialPrice
* @covers \Magento\Catalog\Model\Product::getSpecialFromDate
* @covers \Magento\Catalog\Model\Product::getSpecialToDate
* @covers \Magento\Catalog\Model\Product::getRequestPath
* @covers \Magento\Catalog\Model\Product::getGiftMessageAvailable
* @dataProvider getObsoleteGettersDataProvider
* @param string $key
* @param string $method
*/
public function testGetObsoleteGetters($key, $method)
{
$value = uniqid();
$this->assertEmpty($this->_model->{$method}());
$this->_model->setData($key, $value);
$this->assertEquals($value, $this->_model->{$method}());
}
示例4: removeTierPrice
/**
* @param \Magento\Catalog\Model\Product $product
* @param int|string $customerGroupId
* @param int $qty
* @param int $websiteId
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function removeTierPrice(\Magento\Catalog\Model\Product $product, $customerGroupId, $qty, $websiteId)
{
$prices = $product->getData('tier_price');
// verify if price exist
if ($prices === null) {
throw new NoSuchEntityException(__('This product doesn\'t have tier price'));
}
$tierPricesQty = count($prices);
foreach ($prices as $key => $tierPrice) {
if ($customerGroupId == 'all' && $tierPrice['price_qty'] == $qty && $tierPrice['all_groups'] == 1 && intval($tierPrice['website_id']) === intval($websiteId)) {
unset($prices[$key]);
} elseif ($tierPrice['price_qty'] == $qty && $tierPrice['cust_group'] == $customerGroupId && intval($tierPrice['website_id']) === intval($websiteId)) {
unset($prices[$key]);
}
}
if ($tierPricesQty == count($prices)) {
throw new NoSuchEntityException(__('Product hasn\'t group price with such data: customerGroupId = \'%1\'' . ', website = %2, qty = %3', [$customerGroupId, $websiteId, $qty]));
}
$product->setData('tier_price', $prices);
try {
$this->productRepository->save($product);
} catch (\Exception $exception) {
throw new CouldNotSaveException(__('Invalid data provided for tier_price'));
}
}
示例5: testGetIsSalableHasDataIsSaleable
public function testGetIsSalableHasDataIsSaleable()
{
$typeInstanceMock = $this->getMock('Magento\\Catalog\\Model\\Product\\Type\\Simple', [], [], '', false);
$this->model->setTypeInstance($typeInstanceMock);
$this->model->setData('is_saleable', true);
$this->model->setData('is_salable', false);
self::assertTrue($this->model->getIsSalable());
}
示例6: validate
/**
* Implode data for validation
*
* @param \Magento\Catalog\Model\Product $object
* @return bool
*/
public function validate($object)
{
$attributeCode = $this->getAttribute()->getAttributeCode();
$data = $object->getData($attributeCode);
if (is_array($data)) {
$object->setData($attributeCode, implode(',', array_filter($data)));
}
return parent::validate($object);
}
示例7: initialize
/**
* Initialize product before saving
*
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Catalog\Model\Product
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function initialize(\Magento\Catalog\Model\Product $product)
{
$productData = $this->request->getPost('product');
unset($productData['custom_attributes']);
unset($productData['extension_attributes']);
if ($productData) {
$stockData = isset($productData['stock_data']) ? $productData['stock_data'] : [];
$productData['stock_data'] = $this->stockFilter->filter($stockData);
}
foreach (['category_ids', 'website_ids'] as $field) {
if (!isset($productData[$field])) {
$productData[$field] = [];
}
}
$wasLockedMedia = false;
if ($product->isLockedAttribute('media')) {
$product->unlockAttribute('media');
$wasLockedMedia = true;
}
$product->addData($productData);
if ($wasLockedMedia) {
$product->lockAttribute('media');
}
if ($this->storeManager->hasSingleStore()) {
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]);
}
/**
* Check "Use Default Value" checkboxes values
*/
$useDefaults = $this->request->getPost('use_default');
if ($useDefaults) {
foreach ($useDefaults as $attributeCode) {
$product->setData($attributeCode, false);
}
}
$links = $this->request->getPost('links');
$links = is_array($links) ? $links : [];
$linkTypes = ['related', 'upsell', 'crosssell'];
foreach ($linkTypes as $type) {
if (isset($links[$type])) {
$links[$type] = $this->jsHelper->decodeGridSerializedInput($links[$type]);
}
}
$product = $this->productLinks->initializeLinks($product, $links);
/**
* Initialize product options
*/
if (isset($productData['options']) && !$product->getOptionsReadonly()) {
// mark custom options that should to fall back to default value
$options = $this->mergeProductOptions($productData['options'], $this->request->getPost('options_use_default'));
$product->setProductOptions($options);
}
$product->setCanSaveCustomOptions((bool) $this->request->getPost('affect_product_custom_options') && !$product->getOptionsReadonly());
return $product;
}
示例8: prepareProducts
protected function prepareProducts()
{
$product = $this->product->loadByAttribute('sku', 'simple');
$product->load($product->getId());
$this->product = $product;
$this->product->setData('test_attribute', 'test_attribute_value')->save();
$this->productSecond = clone $this->product;
$this->productSecond->setId(null)->setUrlKey('product-second')->save();
$this->productThird = clone $this->product;
$this->productThird->setId(null)->setUrlKey('product-third')->setData('test_attribute', 'NO_test_attribute_value')->save();
}
示例9: afterInitialize
/**
* @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject
* @param \Magento\Catalog\Model\Product $result
* @return \Magento\Catalog\Model\Product
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterInitialize(\Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject, \Magento\Catalog\Model\Product $result)
{
$productData = $this->request->getPost('product');
/**
* Create Permanent Redirect for old URL key
*/
if ($result->getId() && isset($productData['url_key_create_redirect'])) {
$result->setData('save_rewrites_history', (bool) $productData['url_key_create_redirect']);
}
return $result;
}
示例10: addMediaDataToProduct
/**
* @param Product $product
* @param array $mediaEntries
* @return void
*/
public function addMediaDataToProduct(Product $product, array $mediaEntries)
{
$attrCode = $this->getAttribute()->getAttributeCode();
$value = [];
$value['images'] = [];
$value['values'] = [];
foreach ($mediaEntries as $mediaEntry) {
$mediaEntry = $this->substituteNullsWithDefaultValues($mediaEntry);
$value['images'][$mediaEntry['value_id']] = $mediaEntry;
}
$product->setData($attrCode, $value);
}
示例11: testReindexRowAfterEdit
/**
*
*/
public function testReindexRowAfterEdit()
{
$this->indexer->reindexAll();
$this->productApple->setData('name', 'Simple Product Cucumber');
$this->productApple->save();
$products = $this->search('Apple');
$this->assertCount(0, $products);
$products = $this->search('Cucumber');
$this->assertCount(1, $products);
$this->assertEquals($this->productApple->getId(), $products[0]->getId());
$products = $this->search('Simple Product');
$this->assertCount(5, $products);
}
示例12: testReindexRowAfterEdit
/**
* @depends testReindexAll
*/
public function testReindexRowAfterEdit()
{
$this->productFirst->setData('name', 'Simple Product Third');
$this->productFirst->save();
$products = $this->search('Simple Product First');
$this->assertCount(0, $products);
$products = $this->search('Simple Product Third');
$this->assertCount(1, $products);
$this->assertEquals($this->productFirst->getId(), $products[0]->getId());
$products = $this->search('Simple Product');
$this->assertCount(2, $products);
$this->assertEquals($this->productFirst->getId(), $products[0]->getId());
$this->assertEquals($this->productSecond->getId(), $products[1]->getId());
}
示例13: testTierPrices
/**
* testGetTierPrices
* testSetTierPrices
*
* @dataProvider pricesDataProvider
*/
public function testTierPrices($priceScope, $expectedWebsiteId)
{
// establish the behavior of the mocks
$this->scopeConfigMock->expects($this->any())->method('getValue')->will($this->returnValue($priceScope));
$this->websiteMock->expects($this->any())->method('getId')->will($this->returnValue($expectedWebsiteId));
$this->tpFactory->expects($this->any())->method('create')->will($this->returnCallback(function () {
return $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\TierPrice');
}));
// create sample TierPrice objects that would be coming from a REST call
$tp1 = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\TierPrice');
$tp1->setValue(10);
$tp1->setCustomerGroupId(1);
$tp1->setQty(11);
$tp2 = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\TierPrice');
$tp2->setValue(20);
$tp2->setCustomerGroupId(2);
$tp2->setQty(22);
$tps = [$tp1, $tp2];
// force the product to have null tier prices
$this->product->setData($this::KEY_TIER_PRICE, null);
$this->assertNull($this->product->getData($this::KEY_TIER_PRICE));
// set the product with the TierPrice objects
$this->model->setTierPrices($this->product, $tps);
// test the data actually set on the product
$tpArray = $this->product->getData($this::KEY_TIER_PRICE);
$this->assertNotNull($tpArray);
$this->assertTrue(is_array($tpArray));
$this->assertEquals(sizeof($tps), sizeof($tpArray));
for ($i = 0; $i < sizeof($tps); $i++) {
$tpData = $tpArray[$i];
$this->assertEquals($expectedWebsiteId, $tpData['website_id'], 'Website Id does not match');
$this->assertEquals($tps[$i]->getValue(), $tpData['price'], 'Price/Value does not match');
$this->assertEquals($tps[$i]->getValue(), $tpData['website_price'], 'WebsitePrice/Value does not match');
$this->assertEquals($tps[$i]->getCustomerGroupId(), $tpData['cust_group'], 'Customer group Id does not match');
$this->assertEquals($tps[$i]->getQty(), $tpData['price_qty'], 'Qty does not match');
}
// test with the data retrieved as a REST object
$tpRests = $this->model->getTierPrices($this->product);
$this->assertNotNull($tpRests);
$this->assertTrue(is_array($tpRests));
$this->assertEquals(sizeof($tps), sizeof($tpRests));
for ($i = 0; $i < sizeof($tps); $i++) {
$this->assertEquals($tps[$i]->getValue(), $tpRests[$i]->getValue(), 'REST: Price/Value does not match');
$this->assertEquals($tps[$i]->getCustomerGroupId(), $tpRests[$i]->getCustomerGroupId(), 'REST: Customer group Id does not match');
$this->assertEquals($tps[$i]->getQty(), $tpRests[$i]->getQty(), 'REST: Qty does not match');
}
}
示例14: testReset
/**
* @magentoAppArea frontend
*/
public function testReset()
{
$model = $this->_model;
$this->_assertEmpty($model);
$this->_model->setData('key', 'value');
$this->_model->reset();
$this->_assertEmpty($model);
$this->_model->setOrigData('key', 'value');
$this->_model->reset();
$this->_assertEmpty($model);
$this->_model->addCustomOption('key', 'value');
$this->_model->reset();
$this->_assertEmpty($model);
$this->_model->canAffectOptions(true);
$this->_model->reset();
$this->_assertEmpty($model);
}
示例15: testReindexRowAfterEdit
/**
*
*/
public function testReindexRowAfterEdit()
{
$this->testReindexAll();
$this->productApple->setData('name', 'Simple Product Cucumber');
$this->productApple->save();
$products = $this->search('Apple');
$this->assertCount(0, $products);
$products = $this->search('Cucumber');
$this->assertCount(1, $products);
$this->assertEquals($this->productApple->getId(), $products[0]->getId());
$products = $this->search('Simple Product');
$this->assertCount(5, $products);
$this->assertEquals($this->productApple->getId(), $products[0]->getId());
$this->assertEquals($this->productBanana->getId(), $products[1]->getId());
$this->assertEquals($this->productOrange->getId(), $products[2]->getId());
$this->assertEquals($this->productPapaya->getId(), $products[3]->getId());
$this->assertEquals($this->productCherry->getId(), $products[4]->getId());
}