此代碼工做色板屬性。php
使用下面的代碼,第同樣本被自動詳情頁頁面加載後選定的,html
你必須重寫文件到你的主題,前端
應用程序/設計/前端/ PACKAGENAME / THEMENAME / Magento_Swatches /模板/產品/視圖/ renderer.phtmljquery
<?php /** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ ?> <?php /** @var $block \Magento\Swatches\Block\Product\Renderer\Configurable */ ?> <div class="swatch-opt"></div> <script> require(["jquery", "jquery/ui", "swatchRenderer"], function ($) { $('.swatch-opt').SwatchRenderer({ jsonConfig: <?php /* @escapeNotVerified */ echo $swatchOptions = $block->getJsonConfig(); ?>, jsonSwatchConfig: <?php /* @escapeNotVerified */ echo $swatchOptions = $block->getJsonSwatchConfig(); ?>, mediaCallback: '<?php /* @escapeNotVerified */ echo $block->getMediaCallback() ?>' }); //for selected option if only one value is exist.... jQuery(document).ready(function(){ //for swatches1 var swatchLength = $('.swatch-attribute').length; if(swatchLength >= 1){ if($('.swatch-attribute').hasClass("color")){ $('.swatch-option').first().trigger('click'); } } }); }); </script>
只需重寫你的樣本,renderer.js文件到您的themee和更改_init()
功能下同,json
應用程序/設計/前端/供應商/ THEMENAME / Magento_Swatches / JS /樣 - renderer.jsui
_init: function () { if (this.options.jsonConfig !== '' && this.options.jsonSwatchConfig !== '') { this._sortAttributes(); this._RenderControls(); //this is additional code for select first attribute value if(this.options.jsonConfig.attributes.length == 1){ $('.swatch-attribute .swatch-attribute-options div.swatch-option').first().click(); } } else { console.log('SwatchRenderer: No input data received'); } },