Magento 2 extension to hide price or add to cart button

This blogpost provides a free magento 2 extension to hide price and addtocart button on category page, product page, wish list page and compare page. We have a different features in this extension:-

  • Hide price
  • Hide add to cart button
  • Show price for certain customer Id
  • Show price for certain product Id
  • Enable hide price for selected customer group
  • Allow you to add custom Html from backend to replace add to cart button
  • Allow you to add custom css for replaced add to cart

Extension Configuration page Admin Screen

Hide Price on Compare Page

Hide Price on Category page

Hide Price on Product Page

Hide Price on Wishlist Page

Versions: I’ve tested the module with the version 2.2.2

Click here to download the module

Let me know if you found any issue or have any query.

Magento 2:- Add a Grouped product programmatically

Follow my previous blogpost to create a simple product with magento that will give you a basic idea of creating products. In this blogpost we will continue from that post only. So to create product as grouped, we simply link some products to it.
In previous post code you also need to change type ID as grouped. Grouped product are added to link as related product are linked, below is the code that you need to add before saving product.

$childrenIds = array(53,54,55);
        $associated = array();
        $position = 0;
        foreach($childrenIds as $productId){
            $position++;
            //You need to load each product to get what you need in order to build $productLink
            $linkedProduct = $productRepository->getById($productId);
            
            /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
            $productLink = $objectManager->create(\Magento\Catalog\Api\Data\ProductLinkInterface::class);

            $productLink->setSku('sku')
                ->setLinkType('associated')
                ->setLinkedProductSku($linkedProduct->getSku())
                ->setLinkedProductType($linkedProduct->getTypeId())
                ->setPosition($position)
                ->getExtensionAttributes()
                ->setQty(0);

            $associated[] = $productLink;
        }
        $product->setProductLinks($associated);

In below image you can see the data added to our product:-

Magento 2:- Add a bundled product programmatically

Follow my previous blogpost to create a simple product with magento that will give you a basic idea of creating products. In this blogpost we will continue from that post only. So to create product as bundled, we need to set two more attribute value i.e, price_view and price_type.
In previous post code you also need to change typeId as bundles. After creating a product with type bundled we have to add a link and option information for the product.
Firstly lets see the modified attribute:-

 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE); 
 $product->setPriceType(1); 
 $product->setPriceView(1);

To add a link use the below code.

$link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterface::class);
$link->setPosition(0);
$link->setSku('MH01-XS-Orange');
$link->setIsDefault(false);
$link->getQty(1);
$link->setPrice(12);
$link->setPriceType(\Magento\Bundle\Api\Data\LinkInterface::PRICE_TYPE_FIXED);
 

To add link to option add below code:-

$optionRepository = $objectManager->create(\Magento\Bundle\Api\ProductOptionRepositoryInterface::class);
$option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterface::class);
$option->setTitle('Size');
$option->setType('radio');
$option->setRequired(true);
$option->setPosition(1);
$option->setProductLinks(array($link));
$optionRepository->save($product, $option);

In below image you can see the data added to our product:-

Magento 2:- Create a downloadable product programmatically

Follow my previous blogpost to create a simple product with magento that will give you a basic idea of creating products. In this blogpost we will continue from that post only. So to create product as downloadable, we need to set weight as NO and add details under downloadable Information tab.
In previous post code just set typeId as downloadable and product has weight to NO. After creating a product with type downloadable we have to add a link and sample data information for the product. To add a link use the below code.

$link_repository = $objectManager->create('Magento\Downloadable\Api\LinkRepositoryInterface');
$link_interface = $objectManager->create('\Magento\Downloadable\Api\Data\LinkInterface');
$link_interface->setTitle('first downloable product');
$link_interface->setPrice(9);
$link_interface->setNumberOFDownloads(10);
$link_interface->setIsShareable(1);
$link_interface->setLinkType('url');
$link_interface->setLinkUrl('http://www.w3solver.com');
$link_interface->setSampleType('url');
$link_interface->setSampleUrl('http://w3solver.com');
$link_interface->setIsUnlimited(0);
$link_interface->setSortOrder(0);
$link_repository->save('sku', $link_interface); // param1 is the sku of your product

To add data under sample section:-

$sample_repository = $objectManager->create('Magento\Downloadable\Api\SampleRepositoryInterface');
$sample_interface = $objectManager->create('\Magento\Downloadable\Api\Data\SampleInterface');
$sample_interface->setTitle('first downloable product');
$sample_interface->setSampleType('url');
$sample_interface->setSampleUrl('http://www.w3solver.com/download/qrcode.zip');
$sample_interface->setSortOrder(0);
$sample_repository->save('sku', $sample_interface);

In below you can see the data added to our product:-

Magento 2 Complete Guide to add a product programmatically

There are several scenario when we might need to add our product programmatically in Magento. This blog-post will provide you a detail guide to add the product in database programmatically.
You can follow below posts for creating different product of various type:-

Adding a simple product

Below code will add a new product to magento 2 with SKU as “W3PRO1”, comments are properly given to make the code self-explainable. We have added product with ‘default’ attribute set.

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
        $product = $objectManager->create('\Magento\Catalog\Model\Product');
        $product->setSku('W3PRO1'); // Set your sku here
        $product->setName('Sample Product'); // Name of Product
        $product->setAttributeSetId(4); // Attribute set id
        $product->setStatus(1); // Status on product enabled/ disabled 1/0
        $product->setProductHasWeight(1); // if product has weight
        $product->setWeight(10); // weight of product
        $product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
        $product->setTaxClassId(0); // Tax class id
        $product->setTypeId('simple'); // Type of product (simple/virtual/downloadable/configurable)
        $product->setPrice(100);  // price of product
        $product->setStockData(
                     array(
                        'use_config_manage_stock' => 0,
                        'manage_stock' => 1,
                        'is_in_stock' => 1,
                        'qty' => 99
                         )
                     );
        $product->setNewsFromDate('11-11-2017'); // set the data from which product will be marked as new
        $product->setNewsToDate('11-12-2017'); // set the data till when product will be marked as new
        $product->setCountryOfManufacture('AL'); // Set country of manufacture

        $product->save();
Note: It is not recommended to create object using object manager, you should inject dependency through constructor and then further use them for object creation 

Add Related, Upsell and Crosssell to product

Above code is the basic example for adding product, now if we want to add related, upsell or crosssell product then we need to add below code before saving the products. Below we have just given example for related. Crosssell and upsell will also be created in the same way.

$this->_productLink = $objectManager->create('\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory ');
$skuLinks = '24-MB01,24-MB04,24-MB03';
 $skuLinks = explode(",",$skuLinks);
foreach($skuLinks as $skuLink) {
    $linkData = $this->_productLink->create();
    $linkData->setSku($product->getSku())
             ->setLinkedProductSku($skuLink)
             ->setLinkType("related");
    $linkDataAll[] = $linkData;
}
if($linkDataAll) {
   /* this will set related products detail, similarly we can add upsell and crossell */
   $product->setProductLinks($linkDataAll); 
}

Add Customizable Options

$this->_productCustomOption = $objectManager->create('\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory');

$customOption = $this->_productCustomOption->create()->setTitle('Text')
                     ->setType('area')
                     ->setIsRequire(true)
                     ->setSortOrder(1)
                     ->setPrice(1.00)
                     ->setPriceType('fixed')
                     ->setMaxCharacters(50)
                     ->setProductSku($product->getSku());

$customOptions[] = $customOption;
$product->setOptions($customOptions);

Add website design, Schedule design update, gift options

$product->setWebsiteIds(array(1));  //add website id

/* Add design Attribute*/
$product->setPageLayout('1column');  
$product->setOptionsContainer('container1');
$product->setCustomLayoutUpdate('');

/* Add Schedule Design Update*/
$product->setCustomDesignFrom('1-6-2017');
$product->setCustomDesignTo('1-6-2017');
$product->setCustomDesign(1);
$product->setCustomLayout('1column');

/* Add Gift Options*/
 $product->setGiftMessageAvailable(1);

Magento2 color swatch on product compare page

Magento has swatches available for configurable product. They appears on product detail and configurable page but to show them on compare page we need to make several customization.
You need to create custom module, and I am considering that you knows basic of creating module in magento2. To proceed further add a layout file with the name as catalog_product_compare_index.xml and add below code in the file created.

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <css src="Magento_Swatches::css/swatches.css"/>
    </head>
    <body>
        <referenceContainer name="content">
            <referenceBlock name="catalog.compare.list">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">W3solver_Compareswatch::product/compare/list.phtml</argument>
                </action>
                <block class="Magento\Framework\View\Element\RendererList" name="category.product.type.details.renderers" as="details.renderers">
                    <block class="Magento\Swatches\Block\Product\Renderer\Listing\Configurable" as="configurable" template="Magento_Swatches::product/listing/renderer.phtml" />
                </block>
            </referenceBlock>
        </referenceContainer>

    </body>
</page>

You need to add one function in your overriden list.phtml on line 63

<?php if($_item->getTypeId() == "configurable"):
        echo $block->getProductDetailsHtml($_item);
    endif;
?>