本文整理汇总了PHP中Magento\Core\Helper\Data::jsonEncode方法的典型用法代码示例。如果您正苦于以下问题:PHP Data::jsonEncode方法的具体用法?PHP Data::jsonEncode怎么用?PHP Data::jsonEncode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Core\Helper\Data
的用法示例。
在下文中一共展示了Data::jsonEncode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testJsonEncodeDecode
public function testJsonEncodeDecode()
{
$data = ['one' => 1, 'two' => 'two'];
$jsonData = '{"one":1,"two":"two"}';
$this->assertEquals($jsonData, $this->_helper->jsonEncode($data));
$this->assertEquals($data, $this->_helper->jsonDecode($jsonData));
}
示例2: addAdditionalFieldsToResponseFrontend
/**
* Set data for response of frontend saveOrder action
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function addAdditionalFieldsToResponseFrontend(\Magento\Framework\Event\Observer $observer)
{
/* @var $order \Magento\Sales\Model\Order */
$order = $this->_coreRegistry->registry('directpost_order');
if ($order && $order->getId()) {
$payment = $order->getPayment();
if ($payment && $payment->getMethod() == $this->_payment->getCode()) {
/** @var \Magento\Checkout\Controller\Action $controller */
$controller = $observer->getEvent()->getData('controller_action');
$request = $controller->getRequest();
$response = $controller->getResponse();
$result = $this->_coreData->jsonDecode($response->getBody('default'));
if (empty($result['error'])) {
$payment = $order->getPayment();
//if success, then set order to session and add new fields
$this->_session->addCheckoutOrderIncrementId($order->getIncrementId());
$this->_session->setLastOrderIncrementId($order->getIncrementId());
$requestToAuthorizenet = $payment->getMethodInstance()->generateRequestFromOrder($order);
$requestToAuthorizenet->setControllerActionName($request->getControllerName());
$requestToAuthorizenet->setIsSecure((string) $this->_storeManager->getStore()->isCurrentlySecure());
$result['directpost'] = array('fields' => $requestToAuthorizenet->getData());
$response->clearHeader('Location');
$response->representJson($this->_coreData->jsonEncode($result));
}
}
}
return $this;
}
示例3: _redirect
/**
* Don't actually redirect if we've got AJAX request - return redirect URL instead.
*
* @param string $path
* @param array $arguments
* @return $this|\Magento\Backend\App\AbstractAction
*/
protected function _redirect($path, $arguments = array())
{
if ($this->getRequest()->isXmlHttpRequest()) {
$this->getResponse()->representJson($this->_coreHelper->jsonEncode(array('_redirect' => $this->getUrl($path, $arguments))));
return $this;
} else {
return parent::_redirect($path, $arguments);
}
}
示例4: getSelectedResourcesJson
/**
* Return an array of selected resource ids. If everything is allowed then iterate through all
* available resources to generate a comprehensive array of all resource ids, rather than just
* returning "Magento_Adminhtml::all".
*
* @return string
*/
public function getSelectedResourcesJson()
{
$selectedResources = $this->_selectedResources;
if ($this->isEverythingAllowed()) {
$resources = $this->_resourceProvider->getAclResources();
$selectedResources = $this->_getAllResourceIds($resources[1]['children']);
}
return $this->_coreHelper->jsonEncode($selectedResources);
}
示例5: getCountriesWithStatesRequired
/**
* Returns the list of countries, for which region is required
*
* @param boolean $asJson
* @return array
*/
public function getCountriesWithStatesRequired($asJson = false)
{
$value = trim($this->_config->getValue(self::XML_PATH_STATES_REQUIRED, \Magento\Store\Model\ScopeInterface::SCOPE_STORE));
$countryList = preg_split('/\\,/', $value, 0, PREG_SPLIT_NO_EMPTY);
if ($asJson) {
return $this->_coreHelper->jsonEncode($countryList);
}
return $countryList;
}
示例6: getPriceFormat
/**
* Get prices javascript format json
*
* @param null|int|string|Store $store
* @return string
*/
public function getPriceFormat($store = null)
{
$this->_localeResolver->emulate($store);
$priceFormat = $this->_localeFormat->getPriceFormat();
$this->_localeResolver->revert();
if ($store) {
$priceFormat['pattern'] = $this->_storeManager->getStore($store)->getCurrentCurrency()->getOutputFormat();
}
return $this->_coreData->jsonEncode($priceFormat);
}
示例7: getAllRatesByProductClassJson
/**
* Get all tax rates JSON for all product tax classes.
*
* @return string
*/
public function getAllRatesByProductClassJson()
{
$result = array();
foreach ($this->productTaxClassSource->getAllOptions() as $productTaxClass) {
$taxClassId = $productTaxClass['value'];
$taxRate = $this->calculationService->getDefaultCalculatedRate($taxClassId, $this->currentCustomer->getCustomerId(), $this->getStore()->getId());
$result["value_{$taxClassId}"] = $taxRate;
}
return $this->coreHelper->jsonEncode($result);
}
示例8: getUsedDefaultForPaths
/**
* Get paths of where current template is used as default
*
* @param bool $asJSON
* @return string
*/
public function getUsedDefaultForPaths($asJSON = true)
{
/** @var $template \Magento\Email\Model\BackendTemplate */
$template = $this->getEmailTemplate();
$paths = $template->getSystemConfigPathsWhereUsedAsDefault();
$pathsParts = $this->_getSystemConfigPathsParts($paths);
if ($asJSON) {
return $this->_coreHelper->jsonEncode($pathsParts);
}
return $pathsParts;
}
示例9: build
/**
* Duplicating downloadable product data
*
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Catalog\Model\Product $duplicate
* @return void
*/
public function build(\Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product $duplicate)
{
if ($product->getTypeId() !== \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) {
//do nothing if not downloadable
return;
}
$data = array();
/** @var \Magento\Downloadable\Model\Product\Type $type */
$type = $product->getTypeInstance();
foreach ($type->getLinks($product) as $link) {
/* @var \Magento\Downloadable\Model\Link $link */
$linkData = $link->getData();
$data['link'][] = array('is_delete' => false, 'link_id' => null, 'title' => $linkData['title'], 'is_shareable' => $linkData['is_shareable'], 'sample' => array('type' => $linkData['sample_type'], 'url' => $linkData['sample_url'], 'file' => $this->encoder->jsonEncode(array(array('file' => $linkData['sample_file'], 'name' => $linkData['sample_file'], 'size' => 0, 'status' => null)))), 'file' => $this->encoder->jsonEncode(array(array('file' => $linkData['link_file'], 'name' => $linkData['link_file'], 'size' => 0, 'status' => null))), 'type' => $linkData['link_type'], 'link_url' => $linkData['link_url'], 'sort_order' => $linkData['sort_order'], 'number_of_downloads' => $linkData['number_of_downloads'], 'price' => $linkData['price']);
}
/** @var \Magento\Downloadable\Model\Sample $sample */
foreach ($type->getSamples($product) as $sample) {
$sampleData = $sample->getData();
$data['sample'][] = array('is_delete' => false, 'sample_id' => null, 'title' => $sampleData['title'], 'type' => $sampleData['sample_type'], 'file' => $this->encoder->jsonEncode(array(array('file' => $sampleData['sample_file'], 'name' => $sampleData['sample_file'], 'size' => 0, 'status' => null))), 'sample_url' => $sampleData['sample_url'], 'sort_order' => $sampleData['sort_order']);
}
$duplicate->setDownloadableData($data);
}
示例10: checkRegisterCheckout
/**
* Check Captcha On Checkout Register Page
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function checkRegisterCheckout($observer)
{
$formId = 'register_during_checkout';
$captchaModel = $this->_helper->getCaptcha($formId);
$checkoutMethod = $this->_typeOnepage->getQuote()->getCheckoutMethod();
if ($checkoutMethod == \Magento\Checkout\Model\Type\Onepage::METHOD_REGISTER) {
if ($captchaModel->isRequired()) {
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$result = array('error' => 1, 'message' => __('Incorrect CAPTCHA'));
$controller->getResponse()->representJson($this->_coreData->jsonEncode($result));
}
}
}
return $this;
}
示例11: _formatError
/**
* Format error data according to required format.
*
* @param string $errorMessage
* @param string $trace
* @param int $httpCode
* @param string $format
* @return array|string
*/
protected function _formatError($errorMessage, $trace, $httpCode, $format)
{
$errorData = [];
$message = ['code' => $httpCode, 'message' => $errorMessage];
$isDeveloperMode = $this->_appState->getMode() == State::MODE_DEVELOPER;
if ($isDeveloperMode) {
$message['trace'] = $trace;
}
$errorData['messages']['error'][] = $message;
switch ($format) {
case self::DATA_FORMAT_JSON:
$errorData = $this->_coreHelper->jsonEncode($errorData);
break;
case self::DATA_FORMAT_XML:
$errorData = '<?xml version="1.0"?>' . '<error>' . '<messages>' . '<error>' . '<data_item>' . '<code>' . $httpCode . '</code>' . '<message><![CDATA[' . $errorMessage . ']]></message>' . ($isDeveloperMode ? '<trace><![CDATA[' . $trace . ']]></trace>' : '') . '</data_item>' . '</error>' . '</messages>' . '</error>';
break;
}
return $errorData;
}
示例12: _beforeSave
/**
* Serialize info for Resource Model to save
* For new model check and set available cookie key
*
* @return $this
*/
protected function _beforeSave()
{
parent::_beforeSave();
// Setting info
$info = array();
foreach ($this->getData() as $index => $value) {
if (!in_array($index, $this->_unserializableFields)) {
$info[$index] = $value;
}
}
$this->setInfo($this->_coreData->jsonEncode($info));
if ($this->isObjectNew()) {
$this->setWebsiteId($this->_storeManager->getStore()->getWebsiteId());
// Setting cookie key
do {
$this->setKey($this->mathRandom->getRandomString(self::KEY_LENGTH));
} while (!$this->getResource()->isKeyAllowed($this->getKey()));
}
return $this;
}
示例13: setResponseAfterSaveOrder
/**
* Set data for response of frontend saveOrder action
*
* @param EventObserver $observer
* @return $this
*/
public function setResponseAfterSaveOrder(EventObserver $observer)
{
/* @var $order \Magento\Sales\Model\Order */
$order = $this->_coreRegistry->registry('hss_order');
if ($order && $order->getId()) {
$payment = $order->getPayment();
if ($payment && in_array($payment->getMethod(), $this->_paypalHss->getHssMethods())) {
/* @var $controller \Magento\Framework\App\Action\Action */
$controller = $observer->getEvent()->getData('controller_action');
$result = $this->_coreData->jsonDecode($controller->getResponse()->getBody('default'));
if (empty($result['error'])) {
$this->_view->loadLayout('checkout_onepage_review', true, true, false);
$html = $this->_view->getLayout()->getBlock('paypal.iframe')->toHtml();
$result['update_section'] = array('name' => 'paypaliframe', 'html' => $html);
$result['redirect'] = false;
$result['success'] = false;
$controller->getResponse()->clearHeader('Location');
$controller->getResponse()->representJson($this->_coreData->jsonEncode($result));
}
}
}
return $this;
}
示例14: render
/**
* Convert data to JSON.
*
* @param object|array|int|string|bool|float|null $data
* @return string
*/
public function render($data)
{
return $this->_helper->jsonEncode($data);
}
示例15: execute
/**
* Search for attributes by part of attribute's label in admin store
*
* @return void
*/
public function execute()
{
$this->storeManager->setCurrentStore(\Magento\Store\Model\Store::ADMIN_CODE);
$this->getResponse()->representJson($this->coreHelper->jsonEncode($this->attributeList->getSuggestedAttributes($this->getRequest()->getParam('label_part'))));
}