7

I want to add a Special Price for a product from date to date programmatically by code but don't know how.

I can set in admin but I want to use code to save multiple products.

I use Enterprise Edition and don't have the date fields. Instead the content staging feature is used to schedule price changes.

enter image description here

in EE using scheduled: enter image description here

in Scheduled: enter image description here

2

Try using Magento\Staging\Model\Entity\Update\Save to schedule a new update:

$this->stagingUpdateSave->execute(
    [
        'entityId' => 1234,           // <-- your product id
        'stagingData' => [
            'mode' => 'save',
            'name' => 'Test Special Price Update',
            'start_time' => 'Feb 25, 2017 12:00 AM'
            'end_time' => 'Feb 26, 2017 12:00 AM'
         ],
        'entityData' => [
            'product' => [
                'price' => '1.00',
            ],
        ],
    ]
);
  • Question is to display special price from and to in advance price modal. OP provided reference of Schedule start date and end date. – HungryDB Jan 27 '17 at 4:47
2

In vendor/magento/module-catalog-staging/etc/adminhtml/di.xml

add comments to special_from_date and special_to_date

<type name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav">
        <arguments>
            <argument name="attributesToEliminate" xsi:type="array">
                <item name="news_from_date" xsi:type="string">news_from_date</item>
                <item name="news_to_date" xsi:type="string">news_to_date</item>
                <item name="custom_design_from" xsi:type="string">custom_design_from</item>
                <item name="custom_design_to" xsi:type="string">custom_design_to</item>
                <!--<item name="special_from_date" xsi:type="string">special_from_date</item>
                <item name="special_to_date" xsi:type="string">special_to_date</item> -->
            </argument>
        </arguments>
    </type>

This will show special from and special to date in advance price modal. Now you can override above di.xml file in your own module.

  • It's not working in Enterprise Edition. – Hafiz Arslan Mar 22 at 11:51
-1

Now run below code

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $_product = $objectManager->create('\Magento\Catalog\Model\Product');
        $_product->setName('Test Product');
        $_product->setTypeId('simple');
        $_product->setAttributeSetId(4);
        $_product->setSku('test-SKU');
        $_product->setWebsiteIds(array(1));
        $_product->setVisibility(4);
        $_product->setPrice(array(15));
        $_product->setImage('/testimg/test.jpg');
        $_product->setSmallImage('/testimg/test.jpg');
        $_product->setThumbnail('/testimg/test.jpg');
        $_product->setStockData(array(
                'use_config_manage_stock' => 0,
                'manage_stock' => 1,
                'min_sale_qty' => 1, 
                'max_sale_qty' => 2, 
                'is_in_stock' => 1, 
                'qty' => 100 
            )
        );
        $_product->setCustomAttributes(array(
                'special_from_date' => "01/12/2016", 
                'special_to_date' => "11/12/2016", 
                'special_price' => 12, 
            )
        );

        $_product->save();

see below pic. In my case it's working fine.

enter image description here

  • thanks for advice, but i use Enterprise Edition version, it don't have field to set setSpecialFromDate or setSpecialToDate – Init Dec 6 '16 at 5:05
  • @Init execute my updated code and let me know it is working or not? – Bojjaiah Dec 6 '16 at 5:22
  • @Init have you tried? – Bojjaiah Dec 6 '16 at 6:15
  • thanks, but i use Enterprise Edition, it don't have field Special Price From, your pic is CE version – Init Dec 6 '16 at 7:13
  • see images updated in my question – Init Dec 6 '16 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.