How to dynamically change number of varnish servers with magento2?

I am wondering how I can dynamically increase and decrease the number of varnish instances for a Magento2 installation as needed – with an aws autoscaling group, for example.

Configuring them from the client side is easy: I can just use a load balancer. But Magento2 also needs to be aware of each instance, and this seems to be a bit more tricky. Updating the configuration file would require an app:config:import, which is a bit of a hassle in a production multi-server environment.

The Magento documentation suggests this command:

magento setup:config:set --http-cache-hosts=192.0.2.100,192.0.2.155:6081 

(https://devdocs.magento.com/guides/v2.2/config-guide/varnish/use-multiple-varnish-cache.html)

Is it an appropriate way to update the varnish servers continuously in an operational / live / production system?

Magento2 set order status after payment gateway response

I’m implementing a payment gateway and I successfully created the payment method. But now, I need to get the new order status and pass it to admin order grid. For that I wrote a controller that receives a post from the payment gateway and a model, but not is happening in the order grid. Status is stuck at payment review all the time. I posted my code bellow to see if someone can help me find the reason why it is not working.

Controller:

vendor/module/Controller/Notification/Index.php

<?php   namespace Vendor\Module\Controller\Notification;   class Index extends \Magento\Framework\App\Action\Action {  protected $  _logger;  protected $  _helper;  protected $  _notification;  protected $  _modelOrder;  public function __construct(\Magento\Payment\Model\Method\Logger $  logger, \Magento\Sales\Model\Order $  modelOrder, \Vendor\Module\Helper\Abstract $  helper, \Vendor\Module\Model\Notification $  notification, \Magento\Framework\App\Action\Context $  context) {     $  this->_notification = $  notification;     $  this->_logger = $  logger;     $  this->_helper = $  helper;     $  this->_modelOrder = $  modelOrder;     parent::__construct($  context); }  public function execute() {     $  this->_helper->initApiLib();        try{       if (\Api\Helpers\Xhr::hasPost()) {          $  notification = $  this->_notification->transactionNotification();//I called the Api class in Model and here it returns an array containing the id of the order and id of status.          $  reference = $  notification->getReference(); // it returns the order id         $  this->_modelOrder->loadByIncrementId($  reference);         $  status = $  transaction->getStatus(); //it returns the id of status in payment gateway          $  this->_notification->setStatusOrder($  this->_modelOrder->loadByIncrementId($  reference), $  status);                     } else {             throw new \InvalidArgumentException($  _POST);         }          } catch (Exception $  e) {         die($  e->getMessage());         }    }  } 

And my model

vendor/module/Model/Notification.php

<?php   namespace Vendor\Module\Model;     class Notification   {  public function __construct(\Vendor\Module\Helper\Abstract $  helper) {     $  this->_helper = $  helper; }   public function transactionNotification(){       $  transaction =      \Api\Services\Transactions\Notification::check(         \Api\Configuration\Configure::getAccountCredentials()     );       if($  transaction){         return $  transaction;     } }      public function setStatusOrder(\Magento\Sales\Model\Order $  order){     if($  status == 3){         if($  order->getStatus() != 'processing') {             $  order->setStatus('processing');             $  order->setState('processing');             $  order->addStatusHistoryComment('Success', 'processing');             $  order->setTotalPaid($  order->getGrandTotal());             $  order->setBaseTotalPaid($  order->getGrandTotal());             $  invoice = $  order->getInvoiceCollection()->getLastItem();             $  this->_invoice->load($  invoice->getId());             $  this->_invoice->setTransactionId($  transaction->getCode());             $  this->_invoice->pay();             $  this->_invoice->save();         }     }elseif($  status == 7){         $  invoice = $  order->getInvoiceCollection()->getLastItem();         $  this->_invoice->load($  invoice->getId());         $  this->_invoice->cancel();         $  order->cancel();         $  order->addStatusHistoryComment('Cancelled', 'canceled');     }     $  order->save();   }  } 

Magento2, show unselectable configurable option for out of stock simple products

I’m trying to add an unselectable out of stock option with details to a configurable product drop down.

This is so that customers know when product options will be in stock.

I’ve worked out how to show out of stock configurable options using di injection of the code below.

  • How do we disable the product from being selected in knockoutjs?
  • How do we add the preorder date to the name of the option?
  • Ideally the solution doesn’t use deprecated classes

I’m trying to achieve something like this:

goal example

Here’s the simple product’s attributes:

configuration so far

Here’s the code I’ve got so far, which allows the simple product to show even if it’s out of stock: It is using deprecated class StockRegistryInterface

use Magento\Catalog\Helper\Image as ImageHelper; use Magento\ConfigurableProduct\Helper\Data as ConfigurableHelper; use Magento\CatalogInventory\Api\StockRegistryInterface;  /**  * Class Data  * Helper class for getting options  */ class Data extends ConfigurableHelper {     /**      * @var \Magento\CatalogInventory\Api\StockRegistryInterface      */     protected $  stockRegistry;      /**      * Data constructor.      *      * @param \Magento\Catalog\Helper\Image $  imageHelper      * @param \Magento\CatalogInventory\Api\StockRegistryInterface $  stockRegistry // This is deprecated      */     public function __construct(         ImageHelper $  imageHelper,         StockRegistryInterface $  stockRegistry     )     {         parent::__construct($  imageHelper);         $  this->stockRegistry = $  stockRegistry;     }      /**      * Get Options for Configurable Product Options      *      * @param \Magento\Catalog\Model\Product $  currentProduct      * @param array $  allowedProducts      * @return array      */     public function getOptions($  currentProduct, $  allowedProducts)     {         $  options = [];         foreach ($  allowedProducts as $  product) {             $  productId = $  product->getId();             //$  stockItem = $  this->stockRegistry->getStockItem($  productId, $  product->getStore()->getWebsiteId());             //if ($  stockItem->getQty() < 1) continue;             $  images = $  this->getGalleryImages($  product);             if ($  images) {                 foreach ($  images as $  image) {                     $  options['images'][$  productId][] =                         [                             'thumb' => $  image->getData('small_image_url'),                             'img' => $  image->getData('medium_image_url'),                             'full' => $  image->getData('large_image_url'),                             'caption' => $  image->getLabel(),                             'position' => $  image->getPosition(),                             'isMain' => $  image->getFile() == $  product->getImage(),                         ];                 }             }             foreach ($  this->getAllowAttributes($  currentProduct) as $  attribute) {                 $  productAttribute = $  attribute->getProductAttribute();                 $  productAttributeId = $  productAttribute->getId();                 $  attributeValue = $  product->getData($  productAttribute->getAttributeCode());                 $  options[$  productAttributeId][$  attributeValue][] = $  productId;                 $  options['index'][$  productId][$  productAttributeId] = $  attributeValue;             }         }         return $  options;     } } 

Magento2 custom module installed but not working

have created a module and it works fine on staging and locally but not on live. The module is installed correctly and enabled. The live config.php and the staging config.php are the same. I used the live db on staging and it is working fine which means is not a db problem. Also cleared the cache. In my custom module I have a before plugin for Magento\Checkout\Controller\Onepage\Success execute() and overriten Iways\PayPalPlus\Controller\Order\Create.php. Looks like the code is never executed. Does anyone know what might be the problem?

how to pass json ecoded array in custom magento2 API

I have created custom API that excepting ‘mixed’ type parameter and returning the ‘string’ as I defined. In interface I define parameter type ‘mixed’. As I am trying to pass array as json_encode, it giving me Warning: json_decode() expects parameter 1 to be string, array given in error.` Please let me know how can I pass json array as parameter and return the same.

Here is my code –
etc/webapi.xml

<?xml version="1.0" ?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">     <route method="POST" url="/V1/managecustomers">         <service class="Namespace\Modulename\Api\ManageCustomerInterface" method="createUpdate"/>         <resources>             <resource ref="anonymous"/>         </resources>     </route> </routes> 

etc/di.xml –

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">     <preference for="Namespace\Modulename\Api\ManageCustomerInterface" type="Netzwelt\Customer\Model\Managecustomer"/> </config> 

Namespace\Modulename\Api\ManageCustomerInterface.php :-

namespace Namespace\Modulename\Api;

interface ManageCustomerInterface {     /**      *       * @param mixed $  data      * @return string      */     public function createUpdate($  data); } 

Namespace\Modulename\Model\Managecustomer.php – :

namespace Namespace\Modulename\Model;  use Namespace\Modulename\Api\ManageCustomerInterface;  class Managecustomer implements ManageCustomerInterface {     /**      *       * @param mixed $  data      * @return string      */     public function createUpdate($  data) {         $  data = json_decode($  data);         //echo "<pre>"; print_r($  data); die;         return "Hello";      } } 

If i am passing ‘data’ parameter as string like – $ customerData = [ 'data' => "Abcd" ];

Then its running fine. But If i am trying to pass ‘data’ as array like-

$  customerData = [     'data' => [         "email" => "user@example.com",         "firstname" => "John",         "lastname" => "Doe",         "storeId" => 1,         "websiteId" => 1,         "address"=>[             "street" => "phase-1",             "phone_no"=> "321654213"         ]     ] ]; 

Then its giving error – Warning: json_decode() expects parameter 1 to be string, array given in error.`

Here is my testapi.php file code –

$  ch = curl_init("http://localhost/mage23/index.php/rest/all/V1/managecustomers"); curl_setopt($  ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($  ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($  ch, CURLOPT_POSTFIELDS, json_encode($  customerData)); curl_setopt($  ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));  $  result = curl_exec($  ch);  echo '<pre>';print_r($  result); 

Please let me know how can I pass array as parameter in API.