文章列表


Magento 2:如何在结帐页面中将购物车总数移动到购物车项目下方

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2的结帐页面中将购物车总数移动到购物车项目下方的步骤:</p><p>步骤1: 您需要 根据需要覆盖模块或主题中的checkout_index_index.xml文件。为模块和主题创建文件的路径如下</p><p>在模块中:</p><p>app\code\Vendor\Extension\view\frontend\layout\checkout_index_index.xml</p><p>在主题中:</p><p>app\design\frontend\Themes\Yourtheme\Magento_Checkout\layout\checkout_index_index.xml</p><p>然后添加代码,如下所示:</p><pre class="brush:bash;toolbar:false">&lt;?xml&nbsp;version=&quot;1.0&quot;&nbsp;encoding=&quot;UTF-8&quot;?&gt; &nbsp; &lt;page&nbsp;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&nbsp;xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:View/Layout/etc/page_configuration.xsd&quot;&gt; &nbsp;&nbsp;&nbsp;&lt;body&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;referenceBlock&nbsp;name=&quot;checkout.root&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;arguments&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;argument&nbsp;name=&quot;jsLayout&quot;&nbsp;xsi:type=&quot;array&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;components&quot;&nbsp;xsi:type=&quot;array&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;checkout&quot;&nbsp;xsi:type=&quot;array&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;children&quot;&nbsp;xsi:type=&quot;array&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;sidebar&quot;&nbsp;xsi:type=&quot;array&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;children&quot;&nbsp;xsi:type=&quot;array&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;summary&quot;&nbsp;xsi:type=&quot;array&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;children&quot;&nbsp;xsi:type=&quot;array&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;totals&quot;&nbsp;xsi:type=&quot;array&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;sortOrder&quot;&nbsp;xsi:type=&quot;string&quot;&gt;99&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;cart_items&quot;&nbsp;xsi:type=&quot;array&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;sortOrder&quot;&nbsp;xsi:type=&quot;string&quot;&gt;0&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/argument&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/arguments&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/referenceBlock&gt; &nbsp;&nbsp;&nbsp;&lt;/body&gt; &lt;/page&gt;</pre><p>输出:</p><p><img src="/uploads/images/20230829/5c44222e6065864e9719842330430638.png" title="1.png" alt="" width="780" height="403"/></p><p>购物车项目下方的购物车总计</p><p>结论:</p><p>这样,您可以通过在结帐页面中的购物车项目下方移动购物车总数来自定义Magento 2中的结帐页面。</p><p><br/></p>

Magento 2:如何检查客户是否已登录?

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>检查客户是否在Magento 2中登录的步骤:</p><p>我们可以使用以下方法检查客户是否已在Magento 2中登录。</p><p>让我们开始吧,</p><p>方法 1: 使用帮助程序文件</p><p>在以下路径处创建帮助程序文件。</p><p>app/code/Vendor/Extension/Helper/Data.php</p><p>然后添加代码,如下所示</p><pre class="brush:bash;toolbar:false">&lt;?php &nbsp; namespace&nbsp;Vendor\Extension\Helper; &nbsp; class&nbsp;Data { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Framework\App\Http\Context&nbsp;$httpContext &nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;httpContext&nbsp;=&nbsp;$httpContext; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getLogin()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&gt;httpContext-&gt;getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); &nbsp;&nbsp;&nbsp;&nbsp;} ?&gt;</pre><p>注意 – 根据您的要求,您可以在 phtml、控制器等中使用 getLogin() 方法。</p><p>方法 2:使用对象管理器</p><p>在您的Magento根目录中创建customer.php。</p><p>{{magento_root}}/customer.php</p><p>现在,添加以下代码</p><pre class="brush:bash;toolbar:false">&lt;?php &nbsp; use&nbsp;Magento\Framework\App\Bootstrap; require&nbsp;__DIR__&nbsp;.&nbsp;&#39;/app/bootstrap.php&#39;; &nbsp; try { &nbsp;&nbsp;&nbsp;&nbsp;$bootstrap&nbsp;=&nbsp;\Magento\Framework\App\Bootstrap::create(BP,&nbsp;$_SERVER); &nbsp;&nbsp;&nbsp;&nbsp;$objectManager&nbsp;=&nbsp;$bootstrap-&gt;getObjectManager(); &nbsp;&nbsp;&nbsp;&nbsp;$appState&nbsp;=&nbsp;$objectManager-&gt;get(&#39;\Magento\Framework\App\State&#39;); &nbsp;&nbsp;&nbsp;&nbsp;$appState-&gt;setAreaCode(&#39;frontend&#39;); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;$objectManager&nbsp;=&nbsp;\Magento\Framework\App\ObjectManager::getInstance(); &nbsp;&nbsp;&nbsp;&nbsp;$userContext&nbsp;=&nbsp;$objectManager-&gt;get(&#39;Magento\Framework\App\Http\Context&#39;); &nbsp;&nbsp;&nbsp;&nbsp;$loggedin&nbsp;=&nbsp;$userContext-&gt;getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if($loggedin)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;Customer&nbsp;is&nbsp;Logged-In&quot;; &nbsp;&nbsp;&nbsp;&nbsp;} } catch(\Exception&nbsp;$e) { &nbsp;&nbsp;&nbsp;&nbsp;print_r($e-&gt;getMessage()); }</pre><p>结论:</p><p>因此,使用上述方法,您可以轻松确定登录到Magento 2商店的客户。</p><p><br/></p>

如何在Magento 2中以编程方式按订单ID获取订单数据

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中以编程方式按订单ID获取订单数据的步骤:</p><p>第 1 步:在以下路径的 Magento 根目录中创建一个文件</p><p>magento_root_directory\getorderdata.php</p><p>然后添加以下代码片段</p><pre class="brush:bash;toolbar:false">&lt;?php &nbsp; use&nbsp;Magento\Framework\App\Bootstrap; ini_set(&#39;display_errors&#39;,&nbsp;TRUE);&nbsp; ini_set(&#39;display_startup_errors&#39;,&nbsp;TRUE); &nbsp; require&nbsp;__DIR__&nbsp;.&nbsp;&#39;/../app/bootstrap.php&#39;; &nbsp; $params&nbsp;=&nbsp;$_SERVER; &nbsp; $bootstrap&nbsp;=&nbsp;Bootstrap::create(BP,&nbsp;$params); &nbsp; $objectManager&nbsp;=&nbsp;$bootstrap-&gt;getObjectManager(); &nbsp; $state&nbsp;=&nbsp;$objectManager-&gt;get(&#39;Magento\Framework\App\State&#39;); $state-&gt;setAreaCode(&#39;frontend&#39;); $registry&nbsp;=&nbsp;$objectManager-&gt;get(&#39;Magento\Framework\Registry&#39;); &nbsp; $objectManager&nbsp;=&nbsp;\Magento\Framework\App\ObjectManager::getInstance(); &nbsp; $orderId=1;&nbsp;//Your&nbsp;Order&nbsp;Id &nbsp; $order&nbsp;=&nbsp;$objectManager-&gt;create(&#39;\Magento\Sales\Model\OrderRepository&#39;)-&gt;get($orderId); &nbsp; echo&nbsp;&quot;&lt;br&gt;-----Your&nbsp;Order&nbsp;Data-----&lt;br&gt;&quot;;&nbsp; echo&nbsp;&quot;&lt;br&gt;&nbsp;Entity&nbsp;Id&nbsp;:&nbsp;&quot;.$order-&gt;getEntityId();&nbsp; echo&nbsp;&quot;&lt;br&gt;&nbsp;Increment&nbsp;Id&nbsp;:&nbsp;&quot;.$order-&gt;getIncrementId();&nbsp; echo&nbsp;&quot;&lt;br&gt;&nbsp;State&nbsp;:&nbsp;&quot;.$order-&gt;getState();&nbsp; echo&nbsp;&quot;&lt;br&gt;&nbsp;Status&nbsp;:&nbsp;&quot;.$order-&gt;getStatus();&nbsp; echo&nbsp;&quot;&lt;br&gt;&nbsp;StoreId&nbsp;:&nbsp;&quot;.$order-&gt;getStoreId();&nbsp; echo&nbsp;&quot;&lt;br&gt;&nbsp;Subtotal&nbsp;:&nbsp;&quot;.$order-&gt;getSubtotal(); echo&nbsp;&quot;&lt;br&gt;&nbsp;GrandTotal&nbsp;:&nbsp;&quot;.$order-&gt;getGrandTotal(); echo&nbsp;&quot;&lt;br&gt;&nbsp;TotalQtyOrdered&nbsp;:&nbsp;&quot;.$order-&gt;getTotalQtyOrdered();&nbsp; echo&nbsp;&quot;&lt;br&gt;&nbsp;OrderCurrencyCode&nbsp;:&nbsp;&quot;.$order-&gt;getOrderCurrencyCode();&nbsp; echo&nbsp;&quot;&lt;br&gt;-----Your&nbsp;Customer&nbsp;Data-----&lt;br&gt;&quot;;&nbsp; echo&nbsp;&quot;&lt;br&gt;&nbsp;Customer&nbsp;First&nbsp;Name&nbsp;:&nbsp;&quot;.$order-&gt;getCustomerFirstname();&nbsp; echo&nbsp;&quot;&lt;br&gt;&nbsp;Customer&nbsp;Last&nbsp;Name&nbsp;:&nbsp;&quot;.$order-&gt;getCustomerLastname();&nbsp; echo&nbsp;&quot;&lt;br&gt;-----Your&nbsp;Billing&nbsp;Address&nbsp;Data-----&lt;br&gt;&quot;; echo&nbsp;&quot;&lt;pre&gt;&quot;; print_r($order-&gt;getBillingAddress()-&gt;getData());echo&nbsp;&#39;&lt;br&gt;&#39;; echo&nbsp;&quot;&lt;/pre&gt;&quot;; echo&nbsp;&quot;&lt;br&gt;-----Your&nbsp;Shipping&nbsp;Address&nbsp;Data-----&lt;br&gt;&quot;;&nbsp; echo&nbsp;&quot;&lt;pre&gt;&quot;; print_r($order-&gt;getShippingAddress()-&gt;getData()); echo&nbsp;&quot;&lt;/pre&gt;&quot;; &nbsp; ?&gt;</pre><p>结论:</p><p>因此,使用上述方法,您可以使用Magento 2中的订单ID检索订单信息。或者,当您需要Magento之外的订单信息时,您可以使用Magento 2中的SOAP API获取订单信息。</p><p><br/></p>

如何在Magento 2的结帐页面上预先选择默认付款方式?

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2的结帐页面上预先选择默认付款方式的步骤:</p><p>步骤1: 首先,我们必须在扩展名中的以下路径中创建“requirejs-config.js”文件。</p><p>app\code\Vendor\Extension\view\frontend</p><p>然后编写下面的代码</p><pre class="brush:bash;toolbar:false">var&nbsp;config&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;config:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mixins:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;Magento_Checkout/js/model/checkout-data-resolver&#39;:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;Vendor_Extension/js/model/checkout-data-resolver&#39;:&nbsp;true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} };</pre><p>第2步: 之后,我们必须在扩展名中以以下路径创建一个“<span style="margin: 0px; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-variant-alternates: inherit; font-weight: 600; font-stretch: inherit; font-size: 17px; line-height: inherit; font-family: Poppins, &quot;Nunito Sans&quot;, Montserrat, sans-serif; font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; vertical-align: baseline; text-size-adjust: 100%; -webkit-font-smoothing: antialiased; text-rendering: optimizelegibility; color: rgb(51, 51, 51); letter-spacing: 0.3px; text-wrap: wrap; background-color: rgb(255, 255, 255);">checkout-data-resolver.js</span>”文件。</p><p>app\code\Vendor\Extension\view\frontend\web\js\model</p><p>现在添加代码,如下所示</p><pre class="brush:bash;toolbar:false">define([ &nbsp;&nbsp;&nbsp;&nbsp;&#39;Magento_Checkout/js/model/payment-service&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&#39;Magento_Checkout/js/checkout-data&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&#39;Magento_Checkout/js/action/select-payment-method&#39; ],&nbsp;function( &nbsp;&nbsp;&nbsp;&nbsp;paymentService, &nbsp;&nbsp;&nbsp;&nbsp;checkoutData, &nbsp;&nbsp;&nbsp;&nbsp;selectPaymentMethodAction )&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&#39;use&nbsp;strict&#39;; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;function(checkoutDataResolver)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;checkoutDataResolver.resolvePaymentMethod&nbsp;=&nbsp;function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;availablePaymentMethods&nbsp;=&nbsp;paymentService.getAvailablePaymentMethods(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectedPaymentMethod&nbsp;=&nbsp;checkoutData.getSelectedPaymentMethod(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paymentMethod&nbsp;=&nbsp;selectedPaymentMethod&nbsp;?&nbsp;selectedPaymentMethod&nbsp;:&nbsp;&#39;cashondelivery&#39;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//set&nbsp;payment&nbsp;method&nbsp;as&nbsp;per&nbsp;your&nbsp;requirement(i.e&nbsp;cashondelivery&nbsp;replace&nbsp;with&nbsp;your&nbsp;payment&nbsp;method) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(availablePaymentMethods.length&nbsp;&gt;&nbsp;0)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;availablePaymentMethods.some(function&nbsp;(payment)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(payment.method&nbsp;==&nbsp;paymentMethod)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectPaymentMethodAction(payment); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;checkoutDataResolver; &nbsp;&nbsp;&nbsp;&nbsp;}; });</pre><p>在扩展中创建这两个文件后,运行Magento升级并在服务器中部署命令。</p><p>在店面中,您将看到在Magento 2的结帐页面上预先选择了默认付款方式。</p><p>预选默认付款方式</p><p><img src="/uploads/images/20230828/788074b262490a34b9e6b567c0d636a3.png" title="2.png" alt="" width="833" height="427"/></p><p>结论:</p><p>因此,使用上述步骤,您可以在Magento 2的结帐页面上轻松预先选择默认付款方式,并减少客户选择付款方式的工作量。</p><p>使用支付网关集成服务将您想要的付款方式集成到您的Magento 2商店中 ,并将其设置为默认付款方式,并使用上述方法在结帐页面上进行预选。</p><p><br/></p>

如何在Magento 2中获取特定类别的所有子类别ID?

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>要获取类别 ID,请执行以下步骤。</p><p>第 1 步:在以下路径的 Magento 根目录中创建一个文件</p><p>magento_root_directory\getcategory.php</p><p>然后添加代码,如下所示。</p><pre class="brush:bash;toolbar:false">&lt;?php &nbsp; use&nbsp;Magento\Framework\AppInterface; ini_set(&#39;display_errors&#39;,&nbsp;TRUE); try { &nbsp;&nbsp;&nbsp;&nbsp;require_once&nbsp;__DIR__&nbsp;.&nbsp;&#39;/app/bootstrap.php&#39;; } catch&nbsp;(\Exception&nbsp;$e) { &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&#39;Autoload&nbsp;error:&nbsp;&#39;&nbsp;.&nbsp;$e-&gt;getMessage(); &nbsp;&nbsp;&nbsp;&nbsp;exit(1); } &nbsp; try { &nbsp;&nbsp;&nbsp;&nbsp;$bootstrap&nbsp;=&nbsp;\Magento\Framework\App\Bootstrap::create(BP,&nbsp;$_SERVER); &nbsp;&nbsp;&nbsp;&nbsp;$objectManager&nbsp;=&nbsp;$bootstrap-&gt;getObjectManager(); &nbsp;&nbsp;&nbsp;&nbsp;$appState&nbsp;=&nbsp;$objectManager-&gt;get(&#39;\Magento\Framework\App\State&#39;); &nbsp;&nbsp;&nbsp;&nbsp;$appState-&gt;setAreaCode(&#39;frontend&#39;); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;$categoryFactory&nbsp;=&nbsp;$objectManager-&gt;get(&#39;\Magento\Catalog\Model\CategoryFactory&#39;);//&nbsp;Instance&nbsp;of&nbsp;Category&nbsp;Model &nbsp;&nbsp;&nbsp;&nbsp;$rootCategoryId&nbsp;=&nbsp;2;&nbsp;//&nbsp;your&nbsp;root&nbsp;category&nbsp;Id &nbsp;&nbsp;&nbsp;&nbsp;$category&nbsp;=&nbsp;$categoryFactory-&gt;create()-&gt;load($rootCategoryId); &nbsp;&nbsp;&nbsp;&nbsp;$categoryIds=&nbsp;$category-&gt;getAllChildren(false); &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$categoryIds;&nbsp;//will&nbsp;return&nbsp;comma&nbsp;separated&nbsp;list&nbsp;of&nbsp;ids } catch&nbsp;(Exception&nbsp;$e) { &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$e-&gt;getMessage(); } &nbsp; ?&gt;</pre><p>输出:</p><p>上面的代码将返回如下结果。</p><p>类别编号</p><p><img src="/uploads/images/20230828/57e2d6be00edce57b2cfe0f49c1a8353.png" title="1.png" alt=""/></p><p>结论:</p><p>希望您将能够在Magento 2中获取特定类别的所有子类别ID。</p><p><br/></p>

如何在Magento 2中以编程方式创建目录?

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中以编程方式创建目录的步骤:</p><p>第1步: 首先,在 etc文件夹中创建一个CreateDirectory.php文件。</p><p>app\code\Vendor\Extension\Helper</p><p>然后添加以下代码</p><p></p><pre class="brush:bash;toolbar:false">&lt;?php namespace&nbsp;Vendor\Extension\Helper; &nbsp; use&nbsp;Magento\Framework\App\Helper\AbstractHelper; use&nbsp;Magento\Framework\Filesystem; use&nbsp;Magento\Framework\App\Filesystem\DirectoryList; use&nbsp;Magento\Framework\Exception\FileSystemException; use&nbsp;Magento\Framework\Exception\LocalizedException; use&nbsp;Magento\Framework\Filesystem\Directory\WriteInterface; &nbsp; class&nbsp;CreateDirectory&nbsp;extends&nbsp;AbstractHelper { &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$file; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$newCreateDirectory; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Filesystem&nbsp;$file &nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;newCreateDirectory&nbsp;=&nbsp;$file-&gt;getDirectoryWrite(DirectoryList::VAR_DIR); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;createDirectory() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$directoryPath&nbsp;=&nbsp;&quot;magecomp&quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$newCreateDirectory&nbsp;=&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$newCreateDirectory&nbsp;=&nbsp;$this-&gt;newCreateDirectory-&gt;create($directoryPath); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(FileSystemException&nbsp;$e)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;LocalizedException( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__(&#39;you&nbsp;can&#39;t&nbsp;create&nbsp;directory&#39;,&nbsp;$directoryPath) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$newCreateDirectory; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p><span style="color: #ce9178;">结论:</span></p><p><span style="color: #ce9178;">因此,您可以在Magento 2中以编程方式轻松创建目录。您甚至可以以编程方式删除Magento 2中的目录</span></p><p><br/></p>

如何在Magento 2的CMS页面上添加图像字段?

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在本博客中,我将指导您完成在Magento 2中将图像字段添加到CMS页面的步骤。</p><p>在Magento 2中,CMS(内容管理系统)在管理静态页面(例如主页,关于我们页面和联系页面)方面起着至关重要的作用。默认情况下,Magento 2提供用于CMS页面上的内容编辑的文本字段。但是,在某些情况下,您可能希望包含图像以丰富内容并为客户提供视觉上吸引人的体验。</p><p>让我们看一下如何在Magento 2的CMS页面上添加图像字段的步骤。</p><p>在Magento 2的CMS页面上添加图像字段的步骤:</p><p>步骤1: 第一步,我们需要在扩展名内部的以下路径创建一个db_schema.xml文件。</p><p>{{magento_root}}/app/code/Vendor/Module/etc/db_schema.xml</p><p>现在添加代码,如下所示。</p><pre class="brush:bash;toolbar:false">&lt;?xml&nbsp;version=&quot;1.0&quot;?&gt; &lt;schema&nbsp;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&nbsp;xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;table&nbsp;name=&quot;cms_page&quot;&nbsp;resource=&quot;default&quot;&nbsp;engine=&quot;innodb&quot;&nbsp;comment=&quot;Cms&nbsp;Page&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;column&nbsp;length=&quot;255&quot;&nbsp;name=&quot;custom_image&quot;&nbsp;nullable=&quot;false&quot;&nbsp;xsi:type=&quot;varchar&quot;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/table&gt; &lt;/schema&gt;</pre><p>步骤2:之后,我们需要创建一个cms_page_form.xml来定义CMS页面表单,其中图像字段文件位于扩展名内的以下路径。</p><p>{{magento_root}}/app/code/Vendor/Module/view/adminhtml/ui_component/cms_page_form.xml</p><p>然后包括下面提到的代码</p><pre class="brush:bash;toolbar:false">&lt;?xml&nbsp;version=&quot;1.0&quot;&nbsp;encoding=&quot;UTF-8&quot;?&gt; &lt;form&nbsp;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&nbsp;xsi:noNamespaceSchemaLocation=&quot;urn:magento:module:Magento_Ui:etc/ui_configuration.xsd&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;fieldset&nbsp;name=&quot;content&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;field&nbsp;name=&quot;custom_image&quot;&nbsp;formElement=&quot;imageUploader&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;argument&nbsp;name=&quot;data&quot;&nbsp;xsi:type=&quot;array&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;config&quot;&nbsp;xsi:type=&quot;array&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;dataType&quot;&nbsp;xsi:type=&quot;string&quot;&gt;string&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;label&quot;&nbsp;xsi:type=&quot;string&quot;&nbsp;translate=&quot;true&quot;&gt;Page&nbsp;Image&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;formElement&quot;&nbsp;xsi:type=&quot;string&quot;&gt;imageUploader&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;source&quot;&nbsp;xsi:type=&quot;string&quot;&gt;page&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;sortOrder&quot;&nbsp;xsi:type=&quot;number&quot;&gt;30&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;previewTmpl&quot;&nbsp;xsi:type=&quot;string&quot;&gt;Magento_Catalog/image-preview&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&nbsp;name=&quot;required&quot;&nbsp;xsi:type=&quot;boolean&quot;&gt;false&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/item&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/argument&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/field&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/fieldset&gt; &lt;/form&gt;</pre><p>第三步:之后,我们需要在以下路径的扩展名中的 event.xml 文件中注册观察者文件。</p><p>{{magento_root}}/app/code/Vendor/Module/etc/events.xml</p><p>并添加以下代码段</p><pre class="brush:bash;toolbar:false">&lt;?xml&nbsp;version=&quot;1.0&quot;&nbsp;encoding=&quot;UTF-8&quot;?&gt; &lt;config&nbsp;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&nbsp;xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:Event/etc/events.xsd&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;event&nbsp;name=&quot;cms_page_prepare_save&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;observer&nbsp;name=&quot;custom_cms_image&quot;&nbsp;instance=&quot;Vendor\Module\Observer\SaveCustomImage&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/event&gt; &lt;/config&gt;</pre><p>第4步: 最后,我们需要将上传的图像保存在扩展名内的观察者文件中,位于以下路径。</p><p>{{magento_root}}/app/code/Vendor/Module/Observer/SaveCustomImage.php</p><p>现在添加代码,如下所述</p><pre class="brush:bash;toolbar:false">&lt;?php namespace&nbsp;Vendor\Module\Observer; &nbsp; use&nbsp;Magento\Framework\Event\ObserverInterface; use&nbsp;Magento\Framework\App\Request\DataPersistorInterface; use&nbsp;Magento\Framework\Filesystem; use&nbsp;Magento\MediaStorage\Model\File\UploaderFactory; &nbsp; class&nbsp;SaveCustomImage&nbsp;implements&nbsp;ObserverInterface { &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$dataPersistor; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$uploaderFactory; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$filesystem; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataPersistorInterface&nbsp;$dataPersistor, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UploaderFactory&nbsp;$uploaderFactory, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Filesystem&nbsp;$filesystem) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;dataPersistor&nbsp;=&nbsp;$dataPersistor; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;uploaderFactory&nbsp;=&nbsp;$uploaderFactory; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;filesystem&nbsp;=&nbsp;$filesystem; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;execute(\Magento\Framework\Event\Observer&nbsp;$observer) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data&nbsp;=&nbsp;$this-&gt;dataPersistor-&gt;get(&#39;cms_page&#39;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!$data)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$image&nbsp;=&nbsp;$data[&#39;custom_image&#39;]&nbsp;??&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($image)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$uploader&nbsp;=&nbsp;$this-&gt;uploaderFactory-&gt;create([&#39;fileId&#39;&nbsp;=&gt;&nbsp;&#39;custom_image&#39;]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$uploader-&gt;setAllowedExtensions([&#39;jpg&#39;,&nbsp;&#39;jpeg&#39;,&nbsp;&#39;gif&#39;,&nbsp;&#39;png&#39;]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$uploader-&gt;setAllowRenameFiles(true); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$uploader-&gt;setFilesDispersion(true); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$mediaDirectory&nbsp;=&nbsp;$this-&gt;filesystem-&gt;getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result&nbsp;=&nbsp;$uploader-&gt;save($mediaDirectory-&gt;getAbsolutePath(&#39;custom_cms_images&#39;)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data[&#39;custom_image&#39;]&nbsp;=&nbsp;&#39;custom_cms_images&#39;&nbsp;.&nbsp;$result[&#39;file&#39;]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$observer-&gt;getPage()-&gt;addData($data); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>输出:</p><p><img src="/uploads/images/20230826/c02aef4273ee6e11b15804058f12f1bb.png" title="3.png" alt="" width="757" height="359"/></p><p>结论:</p><p>您已成功将图像字段添加到Magento 2中的CMS页面。您还可以在Magento 2的后端CMS页面部分添加自定义按钮。</p><p><br/></p>

盘点100个Magento 2开源版本功能列表,你需要了解的magento2

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>Magento 2开源功能的完整列表</p><p><span style="color: #6a9955;">### 目录管理</span></p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>Magento 2提供了一个灵活且可定制的产品目录管理系统。这使您可以有效地组织库存并满足不同的客户需求。</p></li><li><p>创建和管理无限数量的产品</p></li><li><p>创建产品属性和属性集以区分产品</p></li><li><p>创建类别和子类别的层次结构</p></li><li><p>支持各种产品类型,包括简单产品、可配置产品、分组产品、虚拟产品、捆绑产品和可下载产品</p></li><li><p>跟踪产品库存水平</p></li><li><p>库存水平低时接收通知</p></li><li><p>为每个产品上传多张图片并创建图片库</p></li><li><p>使客户能够留下评论并对产品进行评分</p></li><li><p>高级搜索选项</p></li><li><p>批量导入/导出功能</p></li><li><p>高级定价选项,如特价、客户组定价和分层定价</p></li><li><p>设置添加到购物车的最小和最大产品数量</p></li><li><p>自动调整产品图片和水印大小</p></li><li><p>通过简单的库存管理支持延期交货</p></li></ul><p><span style="color: #6a9955;">### 目录浏览</span></p><p>Magento 2开源为客户提供了丰富的目录浏览体验,使他们能够轻松导航和探索您的产品。目录浏览功能可帮助客户发现产品、比较选项并做出明智的购买决策。</p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>分层类别结构</p></li><li><p>分层导航</p></li><li><p>产品筛选选项</p></li><li><p>带有自动建议的搜索栏</p></li><li><p>产品分类</p></li><li><p>分页和无限滚动</p></li><li><p>面包屑</p></li><li><p>列表或网格格式的产品视图</p></li></ul><p><span style="color: #6a9955;">### 产品浏览</span></p><p>Magento 2开源提供了一系列功能来增强客户的产品浏览体验,使他们更容易探索您的产品,查看详细信息并做出明智的购买决定。</p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>高质量的产品图像和缩略图视图</p></li><li><p>多个产品图片</p></li><li><p>快速查看选项</p></li><li><p>展示相关和交叉销售产品</p></li><li><p>最近查看的产品</p></li><li><p>添加到购物车按钮</p></li><li><p>愿望清单和比较按钮</p></li><li><p>社交分享选项</p></li></ul><p><span style="color: #6a9955;">### 现场管理</span></p><p>Magento 2开源提供站点管理功能,使电子商务企业能够有效地控制和维护其在线商店。</p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>自定义店面以匹配您的品牌标识并提供独特的用户体验</p></li><li><p>轻松更改商店的布局和设计</p></li><li><p>从单个管理面板管理多个在线商店</p></li><li><p>设置具有特定语言和货币配置的多个商店视图,以迎合全球受众</p></li><li><p>集成 CMS 以创建和管理内容页面、博客和其他静态页面</p></li><li><p>创建用户角色并分配特定权限</p></li><li><p>整页缓存、优化的数据库查询和代码缩小有助于缩短加载时间</p></li><li><p>备份商店的数据和配置</p></li><li><p>为产品、类别和内容页面创建 SEO 友好的 URL</p></li><li><p>设置URL重定向以保持SEO价值</p></li><li><p>在更新或维护任务期间将您的Magento 2商店置于维护模式</p></li><li><p>验证码功能可保护商店免受欺诈登录</p></li></ul><p><span style="color: #6a9955;">### 订单管理</span></p><p>Magento 2开源提供了一个强大的订单管理系统,允许电子商务企业有效地处理和管理客户订单。此功能对于确保顺利履行订单、跟踪和客户沟通至关重要。</p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>从管理面板创建新订单</p></li><li><p>编辑现有订单</p></li><li><p>清晰的订单状态和工作流程系统</p></li><li><p>电子邮件通知,让客户了解他们的订单状态</p></li><li><p>为订单生成发票和装箱单</p></li><li><p>从“我的帐户”跟踪订单</p></li><li><p>发放退款和处理退货</p></li><li><p>添加订单注释和注释</p></li><li><p>订单报告和分析</p></li><li><p>访问历史订单数据</p></li></ul><p><span style="color: #6a9955;">### 结帐、付款和运输</span></p><p>Magento 2开源提供广泛的结账,付款和运输功能,为客户提供无缝便捷的购物体验。这些功能对于顺利有效地完成交易至关重要。</p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>单页结帐</p></li><li><p>访客结账</p></li><li><p>地址自动完成</p></li><li><p>多种送货地址、付款方式、送货方式</p></li><li><p>安全的支付处理</p></li><li><p>商店付款信息</p></li><li><p>实时运费计算</p></li><li><p>设置特定的运输限制和规则</p></li><li><p>订单跟踪功能</p></li></ul><p><span style="color: #6a9955;">### 客户帐户</span></p><p>Magento 2开源提供强大的客户帐户管理功能,使电子商务企业能够提供个性化的购物体验并建立牢固的客户关系。这些功能可增强客户参与度并促进与您的在线商店的无缝交互。</p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>客户注册和登录</p></li><li><p>客户帐户仪表板</p></li><li><p>客户可以存储多个送货和账单地址</p></li><li><p>订单历史记录和跟踪</p></li><li><p>产品评论和评级</p></li><li><p>从“我的帐户”订阅时事通讯</p></li><li><p>创建多个愿望清单</p></li><li><p>重新排序以前下达的订单</p></li><li><p>个性化帐户首选项</p></li><li><p>管理客户帐户权限</p></li></ul><p><span style="color: #6a9955;">### 客户服务</span></p><p>Magento 2开源提供各种客户服务功能,以帮助电子商务企业提供出色的支持并确保客户满意度。这些功能旨在加强沟通、解决客户查询并有效解决问题。</p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>内置“联系我们”表单</p></li><li><p>直接从管理面板回复客户电子邮件</p></li><li><p>通过客户帐户仪表板直接与客户沟通</p></li><li><p>创建知识库或常见问题 (FAQ) 部分</p></li><li><p>向客户发送有关各种订单状态更新的自动电子邮件通知</p></li><li><p>客户可以从其帐户发起退货</p></li><li><p>客户服务代表提供个性化帮助和直接沟通</p></li><li><p>集成第三方实时聊天解决方案</p></li></ul><p><span style="color: #6a9955;">### 营销和转换工具</span></p><p>Magento 2开源提供了广泛的营销和转换工具,以帮助电子商务企业吸引客户,促进销售并提高整体转化率。这些功能旨在提高客户参与度并创造引人入胜的购物体验。</p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>创建各种促销优惠</p></li><li><p>百分比或固定金额折扣</p></li><li><p>买一送一 (BOGO) 优惠</p></li><li><p>免费送货</p></li><li><p>生成和分发优惠券代码</p></li><li><p>用于目标营销活动的客户细分功能</p></li><li><p>个性化产品推荐</p></li><li><p>自动电子邮件提醒以恢复废弃的购物车</p></li><li><p>创建和共享愿望清单</p></li><li><p>交叉销售和向上销售策略</p></li><li><p>客户忠诚度计划</p></li><li><p>设置电子邮件活动,让客户了解新产品、促销和更新</p></li></ul><p><span style="color: #6a9955;">### 搜索引擎优化功能</span></p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>Magento 2开源提供了一系列内置的SEO功能,以帮助提高您的在线商店在搜索引擎中的可见性并吸引自然流量。通过针对搜索引擎优化您的Magento 2商店,您可以增加在搜索结果中排名更高并覆盖更广泛受众的机会。</p></li><li><p>适用于您的产品、类别和内容页面的 SEO 友好网址</p></li><li><p>自定义元标记和元描述</p></li><li><p>自动生成 XML 站点地图</p></li><li><p>规范代码可防止出现重复内容问题</p></li><li><p>配置机器人.txt文件</p></li><li><p>支持丰富网页摘要/架构标记</p></li><li><p>创建网址重写</p></li><li><p>响应式设计</p></li></ul><p><span style="color: #6a9955;">### 国际支持</span></p><p>Magento 2开源包括各种国际支持功能,可帮助电子商务企业扩大其全球市场并满足各种客户需求。这些功能旨在促进多语言、多币种和跨境操作。</p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>多语言支持</p></li><li><p>多币种支持</p></li><li><p>支持复杂的税务规则和配置,以符合不同国家和地区的税务法规</p></li><li><p>使用 Geo-IP 技术检测客户的地理位置</p></li><li><p>翻译和本地化能力</p></li><li><p>支持跨境运输</p></li><li><p>集成国际支付网关</p></li><li><p>计算关税、税款和海关费用</p></li><li><p>多区域库存管理</p></li><li><p>设置国际网址结构</p></li></ul><p><span style="color: #6a9955;">### 分析与报告</span></p><p>Magento 2开源提供分析和报告功能,使电子商务企业能够深入了解其绩效,客户行为和销售趋势。这些功能对于做出明智的决策、优化战略和推动业务增长至关重要。</p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>销售报表</p></li><li><p>产品报告</p></li><li><p>客户报告</p></li><li><p>废弃购物车报告</p></li><li><p>搜索词报告</p></li><li><p>营销报告</p></li><li><p>库存报告</p></li><li><p>客户细分报告</p></li><li><p>地理报告</p></li><li><p>仪表板和自定义报告</p></li><li><p>第三方分析集成,如谷歌分析</p></li></ul><p><span style="color: #6a9955;">### 移动商务</span></p><p>Magento 2开源提供移动商务(m-commerce)功能,允许电子商务企业创建响应迅速且用户友好的移动购物体验。随着移动设备在网上购物中越来越受欢迎,这些功能可帮助您针对移动用户优化商店并占领很大一部分市场。</p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p>响应式设计功能</p></li><li><p>针对移动设备优化的结账流程</p></li><li><p>触摸友好型导航</p></li><li><p>适合移动设备的商品详情</p></li><li><p>AMP(加速移动页面)支持</p></li><li><p>移动搜索功能</p></li><li><p>特定于移动设备的主题和模板</p></li><li><p>集成第三方移动应用解决方案</p></li><li><p>使用移动分析工具</p></li></ul><p><span style="color: #6a9955;">### 总结</span></p><p>凭借其增强的功能,改进的性能和强大的功能,Magento 2提供了许多优势,这些优势对于您的在线企业的成功至关重要。选择可靠的Magento开发服务提供商,帮助您创建强大,高性能且用户友好的电子商务商店,以满足您的业务目标和客户期望。</p><p><br/></p>

如何在Magento 2的类别分层导航中添加库存过滤器

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>今天我们将学习如何在Magento 2中的类别分层导航中添加库存过滤器。</p><p>为客户提供无缝高效的购物体验至关重要。实现这一目标的一种方法是启用有用的过滤器,帮助客户缩小产品搜索范围。</p><p>在Magento 2中的类别分层导航中添加库存过滤器可以通过允许客户根据产品的可用性过滤产品来大大增强用户体验。提供此类过滤选项有助于在您的Magento 2电子商务平台上提供更简化和客户友好的购物体验。</p><p>在本教程中,我们将指导您完成将库存筛选器添加到Magento 2商店中的类别分层导航的过程。</p><p>在Magento 2中的类别分层导航中添加库存过滤器的步骤:</p><p>步骤1: 首先,我们需要在扩展名内部的以下路径创建一个“di.xml”文件。</p><p>app\code\Vendor\Extension\etc\frontend</p><p>然后添加代码,如下所示</p><pre class="brush:bash;toolbar:false">&lt;?xml&nbsp;version=&quot;1.0&quot;?&gt; &lt;config&nbsp;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&nbsp;xsi:noNamespaceSchemaLocation=&quot;../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;type&nbsp;name=&quot;Magento\Catalog\Model\Layer\FilterList&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;plugin&nbsp;name=&quot;stock-filter-category-navigation&quot;&nbsp;type=&quot;Vendor\Extension\Model\Plugin\FilterList&quot;&nbsp;sortOrder=&quot;100&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/type&gt; &lt;/config&gt;</pre><p>第2步: 之后,我们需要在扩展名中以以下路径创建一个“Stock.php”文件。</p><p>app\code\Vendor\Extension\Model\Layer\Filter</p><p>现在添加代码,如下所述</p><pre class="brush:bash;toolbar:false">&lt;?php namespace&nbsp;Vendor\Extension\Model\Layer\Filter; &nbsp; class&nbsp;Stock&nbsp;extends&nbsp;\Magento\Catalog\Model\Layer\Filter\AbstractFilter { &nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;IN_STOCK_COLLECTION_FLAG&nbsp;=&nbsp;&#39;stock_filter_applied&#39;; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$_activeFilter&nbsp;=&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$_requestVar&nbsp;=&nbsp;&#39;in-stock&#39;; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$_scopeConfig; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Framework\App\Config\ScopeConfigInterface&nbsp;$scopeConfig, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Catalog\Model\Layer\Filter\ItemFactory&nbsp;$filterItemFactory, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Store\Model\StoreManagerInterface&nbsp;$storeManager, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Catalog\Model\Layer&nbsp;$layer, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Catalog\Model\Layer\Filter\Item\DataBuilder&nbsp;$itemDataBuilder, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array&nbsp;$data&nbsp;=&nbsp;[] &nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_scopeConfig&nbsp;=&nbsp;$scopeConfig; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::__construct($filterItemFactory,&nbsp;$storeManager,&nbsp;$layer,&nbsp;$itemDataBuilder,&nbsp;$data); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;apply(\Magento\Framework\App\RequestInterface&nbsp;$request) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$filter&nbsp;=&nbsp;$request-&gt;getParam($this-&gt;getRequestVar(),&nbsp;null); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(is_null($filter))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_activeFilter&nbsp;=&nbsp;true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$filter&nbsp;=&nbsp;(int)(bool)$filter; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$collection&nbsp;=&nbsp;$this-&gt;getLayer()-&gt;getProductCollection(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$collection-&gt;setFlag(self::IN_STOCK_COLLECTION_FLAG,&nbsp;true); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$collection-&gt;getSelect()-&gt;where(&#39;stock_status_index.stock_status&nbsp;=&nbsp;?&#39;,&nbsp;$filter); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;getLayer()-&gt;getState()-&gt;addFilter( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_createItem($this-&gt;getLabel($filter),&nbsp;$filter) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getName() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;__(&quot;Stock&quot;); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;_getItemsData() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($this-&gt;getLayer()-&gt;getProductCollection()-&gt;getFlag(self::IN_STOCK_COLLECTION_FLAG))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data&nbsp;=&nbsp;[]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;($this-&gt;getStatuses()&nbsp;as&nbsp;$status)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data[]&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;label&#39;&nbsp;=&gt;&nbsp;$this-&gt;getLabel($status), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;value&#39;&nbsp;=&gt;&nbsp;$status, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;count&#39;&nbsp;=&gt;&nbsp;$this-&gt;getProductsCount($status) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$data; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getStatuses() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\CatalogInventory\Model\Stock::STOCK_IN_STOCK, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\CatalogInventory\Model\Stock::STOCK_OUT_OF_STOCK &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getLabels() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\CatalogInventory\Model\Stock::STOCK_IN_STOCK&nbsp;=&gt;&nbsp;__(&#39;In&nbsp;Stock&#39;), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\CatalogInventory\Model\Stock::STOCK_OUT_OF_STOCK&nbsp;=&gt;&nbsp;__(&#39;Out&nbsp;of&nbsp;stock&#39;), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getLabel($value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$labels&nbsp;=&nbsp;$this-&gt;getLabels(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset($labels[$value]))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$labels[$value]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&#39;&#39;; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getProductsCount($value) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$collection&nbsp;=&nbsp;$this-&gt;getLayer()-&gt;getProductCollection(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$select&nbsp;=&nbsp;clone&nbsp;$collection-&gt;getSelect(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;reset&nbsp;columns,&nbsp;order&nbsp;and&nbsp;limitation&nbsp;conditions &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$select-&gt;reset(\Zend_Db_Select::COLUMNS); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$select-&gt;reset(\Zend_Db_Select::ORDER); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$select-&gt;reset(\Zend_Db_Select::LIMIT_COUNT); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$select-&gt;reset(\Zend_Db_Select::LIMIT_OFFSET); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$select-&gt;where(&#39;stock_status_index.stock_status&nbsp;=&nbsp;?&#39;,&nbsp;$value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$select-&gt;columns( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;count&#39;&nbsp;=&gt;&nbsp;new&nbsp;\Zend_Db_Expr(&quot;COUNT(e.entity_id)&quot;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$collection-&gt;getConnection()-&gt;fetchOne($select); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>第三步: 之后,我们需要在扩展名中的以下路径中创建“FilterList.php”文件。</p><p>app\code\Vendor\Extension\Model\Plugin</p><p>最后,添加代码如下</p><pre class="brush:bash;toolbar:false">&lt;?php namespace&nbsp;Vendor\Extension\Model\Plugin; &nbsp; class&nbsp;FilterList { &nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;STOCK_FILTER_CLASS&nbsp;&nbsp;=&nbsp;&#39;Vendor\Extension\Model\Layer\Filter\Stock&#39;; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$_objectManager; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$_layer; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$_storeManager; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$_stockResource; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$_scopeConfig; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Store\Model\StoreManagerInterface&nbsp;$storeManager, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Framework\ObjectManagerInterface&nbsp;$objectManager, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\CatalogInventory\Model\ResourceModel\Stock\Status&nbsp;$stockResource, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Framework\App\Config\ScopeConfigInterface&nbsp;$scopeConfig &nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_storeManager&nbsp;=&nbsp;$storeManager; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_objectManager&nbsp;=&nbsp;$objectManager; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_stockResource&nbsp;=&nbsp;$stockResource; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_scopeConfig&nbsp;=&nbsp;$scopeConfig; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;isEnabled() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$outOfStockEnabled&nbsp;=&nbsp;$this-&gt;_scopeConfig-&gt;isSetFlag( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\CatalogInventory\Model\Configuration::XML_PATH_DISPLAY_PRODUCT_STOCK_STATUS, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Store\Model\ScopeInterface::SCOPE_STORE &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$outOfStockEnabled; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;beforeGetFilters( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Catalog\Model\Layer\FilterList\Interceptor&nbsp;$filterList, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Catalog\Model\Layer&nbsp;$layer &nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_layer&nbsp;=&nbsp;$layer; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($this-&gt;isEnabled())&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$collection&nbsp;=&nbsp;$layer-&gt;getProductCollection(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$websiteId&nbsp;=&nbsp;$this-&gt;_storeManager-&gt;getStore($collection-&gt;getStoreId())-&gt;getWebsiteId(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_addStockStatusToSelect($collection-&gt;getSelect(),&nbsp;$websiteId); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;array($layer); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;afterGetFilters( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\Catalog\Model\Layer\FilterList\Interceptor&nbsp;$filterList, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array&nbsp;$filters &nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($this-&gt;isEnabled())&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$filters[]&nbsp;=&nbsp;$this-&gt;getStockFilter(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$filters; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getStockFilter() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$filter&nbsp;=&nbsp;$this-&gt;_objectManager-&gt;create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;getStockFilterClass(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&#39;layer&#39;&nbsp;=&gt;&nbsp;$this-&gt;_layer] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$filter; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getStockFilterClass() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self::STOCK_FILTER_CLASS; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;_addStockStatusToSelect(\Zend_Db_Select&nbsp;$select,&nbsp;$websiteId) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$from&nbsp;=&nbsp;$select-&gt;getPart(\Zend_Db_Select::FROM); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!isset($from[&#39;stock_status_index&#39;]))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$joinCondition&nbsp;=&nbsp;$this-&gt;_stockResource-&gt;getConnection()-&gt;quoteInto( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;e.entity_id&nbsp;=&nbsp;stock_status_index.product_id&#39;&nbsp;.&nbsp;&#39;&nbsp;AND&nbsp;stock_status_index.website_id&nbsp;=&nbsp;?&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$websiteId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$joinCondition&nbsp;.=&nbsp;$this-&gt;_stockResource-&gt;getConnection()-&gt;quoteInto( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;&nbsp;AND&nbsp;stock_status_index.stock_id&nbsp;=&nbsp;?&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\Magento\CatalogInventory\Model\Stock::DEFAULT_STOCK_ID &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp; }</pre><p>步骤4:在Magento中创建所有文件后,您需要运行Magento升级,编译和部署命令,如下所示。</p><pre class="brush:bash;toolbar:false">php&nbsp;bin/magento&nbsp;setup:upgrade php&nbsp;bin/magento&nbsp;setup:static-content:deploy&nbsp;-f php&nbsp;bin/magento&nbsp;setup:di:compile php&nbsp;bin/magento&nbsp;cache:clean php&nbsp;bin/magento&nbsp;cache:flush</pre><p>输出:</p><p>访问您的Magento 2商店的类别页面,您现在应该在分层导航中看到库存过滤器。</p><p>库存过滤器</p><p>现在,如果客户在“库存”筛选器菜单中选择“有货”选项,它将显示可供购买的产品。</p><p>在库存过滤器中选择的库存期权</p><p>如果客户选择“缺货”选项,它将显示当前没有库存的产品。</p><p>在库存筛选器中选择缺货选项</p><p><br/></p><p><img src="/uploads/images/20230826/cbd1f64f4a47ba84e8fab41eea1cfdf6.png" title="2.png" alt="" width="740" height="318"/></p><p>结论:</p><p>通过执行以下步骤,您可以通过向类别分层导航添加库存筛选器来增强Magento 2商店的用户体验。这将使客户能够根据产品的可用性筛选产品,帮助他们查找当前有货或缺货的产品。</p><p><br/></p>

在Magento 2中从前端下订单后以编程方式向订单添加评论

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在本Magento 2教程指南中,我将解释如何在Magento 2中从前端下订单后以编程方式向订单添加评论。</p><p>在Magento 2中,从前端下订单后以编程方式向订单添加评论或评论的能力对于各种目的非常有用,例如提供其他说明,说明或内部通信。</p><p>让我们看看如何在Magento 2中从前端下订单后以编程方式向订单添加评论。</p><p>在Magento 2中从前端下订单后,以编程方式向订单添加评论的步骤:</p><p>第 1 步:在 app\code\Vendor\Extension\etc\events.xml 创建一个文件,并添加以下代码。</p><pre class="brush:bash;toolbar:false">&lt;?xml&nbsp;version=&quot;1.0&quot;?&gt; &nbsp; &lt;config&nbsp;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&nbsp;xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:Event/etc/events.xsd&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;event&nbsp;name=&quot;checkout_onepage_controller_success_action&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;observer&nbsp;name=&quot;order_observer&quot;&nbsp;instance=&quot;\Vendor\Extension\Observer\OrderObserver&quot;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/event&gt; &lt;/config&gt;</pre><p>第 2 步:在 app\code\Vendor\Extension\Observer\OrderObserver.php 创建一个文件,并添加以下代码。</p><pre class="brush:bash;toolbar:false">&lt;?php &nbsp; namespace&nbsp;Vendor\Extension\Observer; &nbsp; use&nbsp;Magento\Framework\Event\Observer; use&nbsp;Magento\Framework\Event\ObserverInterface; use&nbsp;Magento\Sales\Api\OrderRepositoryInterface; use&nbsp;Magento\Sales\Model\Order\Status\HistoryFactory; &nbsp; class&nbsp;OrderObserver&nbsp;implements&nbsp;ObserverInterface { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$orderRepository; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;$orderStatusHistoryFactory; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;__construct( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderRepositoryInterface&nbsp;$orderRepository, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HistoryFactory&nbsp;$orderStatusHistoryFactory &nbsp;&nbsp;&nbsp;&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;orderRepository&nbsp;=&nbsp;$orderRepository; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;orderStatusHistoryFactory&nbsp;=&nbsp;$orderStatusHistoryFactory; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;execute(Observer&nbsp;$observer) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$order&nbsp;=&nbsp;$observer-&gt;getEvent()-&gt;getOrder(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$comment&nbsp;=&nbsp;&quot;Your&nbsp;comment&nbsp;goes&nbsp;here.&quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;addCommentToOrder($order-&gt;getId(),&nbsp;$comment); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;addCommentToOrder($orderId,&nbsp;$comment) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$order&nbsp;=&nbsp;$this-&gt;orderRepository-&gt;get($orderId); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$statusHistory&nbsp;=&nbsp;$this-&gt;orderStatusHistoryFactory-&gt;create(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$statusHistory-&gt;setComment( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__(&#39;Comment:&nbsp;%1.&#39;,&nbsp;$comment) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$statusHistory-&gt;setEntityName(\Magento\Sales\Model\Order::ENTITY); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$statusHistory-&gt;setStatus($order-&gt;getStatus()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$statusHistory-&gt;setIsCustomerNotified(false)-&gt;setIsVisibleOnFront(false); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$order-&gt;addStatusHistory($statusHistory); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;orderRepository-&gt;save($order); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(\Exception&nbsp;$e)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;LocalizedException(__(&quot;Failed&nbsp;to&nbsp;add&nbsp;the&nbsp;comment&nbsp;to&nbsp;the&nbsp;order:&nbsp;%1&quot;,&nbsp;$e-&gt;getMessage())); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p>输出:</p><p>转到前端的订单视图页面,您现在应该在其中看到评论表单。添加评论,然后单击“提交评论”按钮。评论应添加到订单的状态历史记录中。</p><p><img src="/uploads/images/20230826/8566ef32e16669e2d954892ba729e710.png" title="1.png" alt="" width="944" height="347"/></p><p>结论:</p><p>通过执行这些步骤,您可以通过启用向订单添加评论或评论来增强商店的沟通和组织。此功能对于跟踪与每个订单相关的特殊说明、更新或任何其他相关信息特别有用。您还可以使用Magento 2订单评论扩展来允许客户在结帐页面上添加评论并从后端成功管理它。</p><p><br/></p>