Magento 2: come aggiungere il testo segnaposto ai campi stradali in checkout?


10

Nel back-end ho impostato l'indirizzo su 3 righe.

Vorrei mettere un segnaposto diverso in ogni campo:

  • strada
  • Edificio / appartamento
  • La zona

In questo modo l'utente può inserire i dati in un modo più strutturato.

Una domanda simile può essere trovata qui:

Magento 2 - Come influenzare l'indirizzo stradale nei moduli di checkout con argomenti layout xml / ui

Tuttavia, le risposte non forniscono una soluzione per includere segnaposto nei campi degli indirizzi.

Quello che voglio ottenere è impostare un segnaposto diverso per ciascun campo dell'indirizzo stradale .

Il mio codice:

app / code / Jsp / Placeholder / etc / module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Jsp_Placeholder" setup_version="2.0.0" />
</config>

app / code / Jsp / Placeholder / registration.php:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
  \Magento\Framework\Component\ComponentRegistrar::MODULE,
  'Jsp_Placeholder',
  __DIR__
);

app / code / Jsp / Placeholder / etc / di.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <type name="Magento\Checkout\Block\Checkout\AttributeMerger">
    <plugin name="shippingAddress" type="Jsp\Placeholder\Plugin\Checkout\Block\Checkout\AttributeMerger\Plugin"/>
  </type>
</config>

app / code / Jsp / Placeholder / Plugin / Checkout / Block / Checkout / AttributeMerger / Plugin.php:

<?php
namespace Jsp\Placeholder\Plugin\Checkout\Block\Checkout\AttributeMerger;
class Plugin {
  public function afterMerge(\Magento\Checkout\Block\Checkout\AttributeMerger $subject, $result)
  {
    if (array_key_exists('street', $result)) {
      $result['street']['children'][0]['placeholder'] = __('Calle y número exterior');
      $result['street']['children'][1]['placeholder'] = __('Interior / Edificio / Depto.');
      $result['street']['children'][2]['placeholder'] = __('Colonia');
    }
    return $result;
  }
}

Dopo aver aggiunto questo modulo hai fatto questi passaggi: 1. attiva il modulo: modulo sudo bin / magento: abilita Jsp_Placeholder 2. aggiorna l'installazione: installazione sudo bin / magento: aggiornamento 3. compila l'installazione: sudo bin / installazione magento: di: compila hai fatto tutto questo?
— Ashish Jagnani,

Questo codice funziona perfettamente con il modulo di indirizzo di checkout predefinito in magento 2.
— Ashish Jagnani,

Risposte:


14

Aggiungi questi file in uno qualsiasi dei tuoi moduli personalizzati:

app / code / Venditore / ModuleName / etc / Module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Vendor_ModuleName" setup_version="2.0.0" />
</config>

app / code / Venditore / ModuleName / registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
  \Magento\Framework\Component\ComponentRegistrar::MODULE,
  'Vendor_ModuleName',
  __DIR__
);

app / code / Venditore / ModuleName / etc / di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <type name="Magento\Checkout\Block\Checkout\AttributeMerger">
    <plugin name="shippingAddress" type="Vendor\ModuleName\Plugin\Checkout\Block\Checkout\AttributeMerger\Plugin"/>
  </type>
</config>

Venditore \ ModuleName \ plugin \ Checkout \ Blocco \ Checkout \ AttributeMerger \ Plugin.php

<?php
namespace Vendor\ModuleName\Plugin\Checkout\Block\Checkout\AttributeMerger;

class Plugin
{
  public function afterMerge(\Magento\Checkout\Block\Checkout\AttributeMerger $subject, $result)
  {
    if (array_key_exists('street', $result)) {
      $result['street']['children'][0]['placeholder'] = __('Flat No/House No/Building No');
      $result['street']['children'][1]['placeholder'] = __('Street Address');
      $result['street']['children'][2]['placeholder'] = __('Landmark');
    }

    return $result;
  }
}

Dove devo aggiungere il di.xmlfile? Non ho moduli personalizzati
— Luis Garcia,

Per favore controlla la mia risposta aggiornata.
— Ashish Jagnani,

Grazie, ho creato il modulo seguendo le tue istruzioni, ma i segnaposto non vengono ancora visualizzati. Il modulo è abilitato, pulisco la cache ed eseguo l'installazione: upgrade. Hai idea di cosa potrebbe esserci di sbagliato?
— Luis Garcia,

Scrivi il tuo codice esatto di tutti i file del modulo nella tua domanda cosa hai provato.
— Ashish Jagnani,

Ho appena aggiornato la mia domanda con il codice che ho provato
— Luis Garcia,
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.