2

I have created a module in which all the things is done just 2 things are remaining .

How to add Dynamic base url in Magento 2 theme JS file. ex : http://127.0.0.1/projectname how to get that dynamically.

How to pass success or error in JSON format using Module -> Model

it would be really helpful.

<?php
namespace Namespcae\Modulename\Controller\Index;

use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;

class Test extends \Magento\Framework\App\Action\Action
{
protected $_configReader;
protected $_pageFactory;
protected $customer;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\App\DeploymentConfig\Reader $configReader,
    \Namespcae\Modulename\Model\Customer $customer,
    \Magento\Framework\View\Result\PageFactory $pageFactory)
{
    $this->_pageFactory = $pageFactory;
    $this->_configReader = $configReader;
    $this->customer = $customer;
    return parent::__construct($context);
}

public function execute()
{   
   $productIds = [5];

    /** Contains the id of the store in which you would like to enable/disable the product */
    $storeId = 0;

    /**
     * You can put any number of product attributes here. However, in the scope of this code we are going to
     * only enable/disable the product.
     */
    $attributes = [
        'status' => Status::STATUS_ENABLED
    ];

    $this->productAction->updateAttributes($productIds, $attributes, $storeId);
}
}

How can i send response success or error in JSON format.

6
  • @RahulBarot is right, this question is similar, you can find your answer there, but I didn't get JSON part of your question, why do you want json response from model, we always want response from controller, but I don't understand why from Model? Commented Jul 13, 2020 at 6:12
  • Actually i have executed my data from model if i move that t controller that would be good i will do that and from that point how to return JSON response from that controller part on success or failure.@shoaib
    – Prits
    Commented Jul 13, 2020 at 6:18
  • how to get base url in Theme js file not in any module js file @RahulBarot
    – Prits
    Commented Jul 13, 2020 at 6:21
  • Updated my controller file please check @ShoaibMunir
    – Prits
    Commented Jul 13, 2020 at 6:45

1 Answer 1

1

For json response from your controller just use array and user echo or return in json format. Example code:

public function execute()
{   
    $response = ["success"=>"", "error"=>""];
    $config = $this->_configReader->load();
    $response["base_url"] = $this->getBaseUrl();
    $response["custom_data_1"] = "My data";
    $response["custom_array_1"] = [123,456,789];
    //Condition to check if error
    $response["error"] = "Error occured";
    //Success check
    $response["success"] = "Successfully done";
    return json_encode($response);
}

if return json_encode($response); not work for you then you can try echo json_encode($response);

In your controller you can also use $this->messageManager->addSuccessMessage("Success message here") or $this->messageManager->addErrorMessage("Error message here")

For base url part, you can use this code in any of the js

require([
    'jquery',
    'mage/url'
], function($,url){
    url.setBaseUrl(BASE_URL);
    varBaseUrl = url.build('');
    //Your other code here
});
22
  • Give me error " Type Error occurred when creating object " @Shoaib
    – Prits
    Commented Jul 13, 2020 at 11:39
  • For URL part " Cannot read property 'indexOf' of undefined "
    – Prits
    Commented Jul 13, 2020 at 11:42
  • 1
    I have updated my answer, this is tested code, it should work now Commented Jul 13, 2020 at 13:36
  • 1
    Use try catch, if it works it wont go to catch, add success code in try Commented Jul 13, 2020 at 18:41
  • 1
    You don't have to pass id, you need to assign html in div with class, something like this jQuery(".mainProduct .name").html(result.product.name) for this you need to set data of product in your controller response Commented Jul 14, 2020 at 5:01

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.