0

Magento 2.1.1.

I have to add downloadable samples to bundle products. The problem for me is adding tab to product form, I guess when I can insert data into form, saving would be mych easier to implement.

So far I have done:

etc\adminhtml\di.xml:

<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">

<type name="Magento\Downloadable\Block\Adminhtml\Catalog\Product\Edit\Tab\Downloadable">
    <plugin name="Vendor::removeConstrainsForBundlesSamples"
            type="Vendor\BundleSamples\Plugin\DownloadableConstrains"
            sortOrder="10"
            disabled="false"/>
</type>


<type name="Magento\Downloadable\Block\Adminhtml\Catalog\Product\Edit\Tab\Downloadable\Samples">
    <plugin name="Vendor::removeConstrainsForBundlesSamples"
            type="Vendor\BundleSamples\Plugin\SamplesConstrains"
            sortOrder="10"
            disabled="false"/>
</type>

Plugins:

<?php 
   namespace Vendor\BundleSamples\Plugin;

use Magento\Bundle\Model\Product\Type as BundleType;
use Magento\Downloadable\Block\Adminhtml\Catalog\Product\Edit\Tab\Downloadable as MagentoDownloadable;

class DownloadableConstrains
{

public function afterIsDownloadable(MagentoDownloadable $subject, $results) 
{
    if ($results == false && $subject->getProduct()->getTypeId() == BundleType::TYPE_CODE)
    {
        $results = true;
        return $results;
    }

    return $results;

}



<?php 
   namespace Vendor\BundleSamples\Plugin;

use Magento\Bundle\Model\Product\Type as BundleType;
use Magento\Downloadable\Block\Adminhtml\Catalog\Product\Edit\Tab\Downloadable \Samples as MagentoDownloadableSamples;

class SamplesConstrains
{

public function afterIsDownloadable(MagentoDownloadableSamples $subject, $results) 
{
    if ($results == false && $subject->getProduct()->getTypeId() == BundleType::TYPE_CODE)
    {
        $results = true;
        return $results;
    }

    return $results;

}

My layout file - view\adminghtml\layout\catalog_product_bundle.xml:

<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
    <referenceContainer name="product_form">
      <block class="Magento\Downloadable\Block\Adminhtml\Catalog\Product\Edit\Tab\Downloadable" name="bundle_downloadable_items" template="Vendor_BundleSamples::product/edit/downloadable.phtml">
        <arguments>
          <argument name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Downloadable Information</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="opened" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="string">133</item>
            <item name="canShow" xsi:type="boolean">true</item>
            <item name="componentType" xsi:type="string">fieldset</item>
          </argument>
        </arguments>
        <block class="Magento\Downloadable\Block\Adminhtml\Catalog\Product\Edit\Tab\Downloadable\Samples" name="bundle_downloadable_samples" template="Vendor_BundleSamples::product/edit/downloadable/samples.phtml">
          <!--<arguments>
            <argument name="config" xsi:type="array">
              <item name="parentComponent" xsi:type="string">product_form.product_form.block_gallery.block_gallery</item>
            </argument>
          </arguments>-->
        </block>
      </block>
    </referenceContainer>

I have also templates, but they are very long and I don't want make a mess here (as they are messy by themselves), so I just point out the ones in core I have used in provided paths in my module:

vendor/magento/module-downloadable/view/adminhtml/templates/product/edit/downloadable.phtml

(here I have removed lines 210-216 in order to prevent types changing)

vendor/magento/module-downloadable/view/adminhtml/templates/product/edit/downloadable/samples.phtml

That is all - for now I have this tab displaying in bundle product, but when I want to add file it is not uploading - i guess that tab is not properly added as it look like this (difference in samples section) this

If this tab is added for simple or virtual section I bet it is possible to use as well here. Saving that after product save can be done via some plugins or obrservers if default classes would be enable to complete this task.

1 Answer 1

0

For Magento 2.1.1 Downloadable Information tab is added via Ui, but older version file are not removed, what can be really misleading. Downloadable product in 2.1.1. no longer uses these blocks, instead take a look at vendor/magento/module-downloadable/etc/adminhtml/di.xml and files pointed out here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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