在magento 2中设置属性值

时间:2019-02-08 11:02:38

标签: magento2

我以编程方式在magento 2中创建了名为my_shipping_charge的自定义属性。我为此属性设置默认值“ 0”。我创建新产品时效果很好。但是,如果我想为已创建的产品设置此属性,该怎么办?请帮我解决这个问题。

1 个答案:

答案 0 :(得分:3)

对于已经创建的产品,如果产品集合较大,则我们必须手动更新,可以在root用户中运行文件。 在此根文件中,我们可以加载所有产品集合并为所有产品设置自定义属性值并保存。

根文件夹中的

自定义文件如下:

<?php
use \Magento\Framework\App\Bootstrap;
require __DIR__ . "/app/bootstrap.php";
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$instance = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$product_collections = $instance ->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collections = $product_collections->create();
$shippingCharge = "custom value";
foreach ($collections as $product) {
    $product->setMyShippingCharge($shippingCharge);
    $product->save();
}
?>

$ shippingCharge将是必须更新的自定义值。 在终端中运行根文件并重新索引。然后从管理面板中检查