2012-11-18 8 views
3

이 스크립트를 사용하여 선택 옵션에서 동적 SKU를 표시 할 수 있지만 작동하지 않습니다.구성 가능한 제품보기에 동적 SKU 표시 Magento

올바른 SKU를로드하고 있지만 선택시 아무 것도 발생하지 않습니다.

이 코드는 Javascript에서 sku 목록을 가져오고 구성 가능한 제품보기에서 제품에 대한 선택 옵션에서 div를 업데이트합니다.

<?php 
$_product = $this->getProduct(); 
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes()); 
?> 
<?php if ($_product->isSaleable() && count($_attributes)):?> 
    <dl> 
    <?php foreach($_attributes as $_attribute): ?> 
     <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt> 
     <dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>> 
      <div class="input-box"> 
       <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select" onchange="return changeSku(this);"> 
        <option><?php echo $this->__('Choose an Option...') ?></option> 
        </select> 
       </div> 
     </dd> 
    <?php endforeach; ?> 
    </dl> 
    <script type="text/javascript"> 
     var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>); 
    </script> 

<?php endif;?> 


<?php 
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product); 
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions(); 

echo '<script type="text/javascript">'; 

echo ' 
document.observe("dom:loaded", function() { 
    $("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id"); 
}); 
'; 
echo ' function changeSku(sel){';  

$itemId = array();   
foreach($col as $simple_product){ 
$itemId[] = array($simple_product->getSelectLabel() => $simple_product->getSku()); 
} 

//echo "<pre>"; 
//print_r($itemId); 
//echo "</pre>"; 

foreach($itemId as $val){ 
foreach($val as $k => $v){ 
echo "\n".'if(sel.options[sel.selectedIndex].value == "'.$k.'"){'."\n"; 
echo '$("sku-container").update("<strong>Product Id: </strong>'.$v.'");'. "\n"; 
echo '}'; 
    } 
} 

echo "\n".'if(sel.options[sel.selectedIndex].value == ""){'."\n"; 
echo '$("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");'. "\n"; 
echo '}'; 

echo "}"; 
echo "\n</script>"; 
?> 

감사합니다.

감사

답변

8

스크립트는 $simple_product->getSelectLabel()에 대한 잘못된 키 것을 제외하고는 거의 정확합니다. 간단한 제품 모델에는 그러한 메소드/특성이 없습니다. 스크립트를 작동 시키려면이 키를 제품 ID (Product Id)로 바꿔야합니다. 제품 ID를 사용하여 선택한 제품의 SKU를 찾을 수 있습니다.

$productMap = array(); 
foreach($col as $simpleProduct){ 
    $productMap[$simpleProduct->getId()] = $simpleProduct->getSku(); 
} 


는 그런 다음에 "onchange를"함수 호출을 변경해야


그래서, 우선 당신은 그것이 "productId에 => productSku를"지도하기 위해 itemId 배열을 재구성해야 Configurable의 속성 ID를 changeSku() 함수에 전달하십시오. 따라서 기본 로직은 적절한 단순 제품의 속성을 검색 할 수 있습니다.

onchange="return changeSku(<?php echo $_attribute->getAttributeId() ?>, this);"> 


그리고 당신은 순서의 구성 설정을 사용할 필요가 후

선택한 제품 ID에 간단한 제품의 속성 ID를 선택 매핑하려면 : 참고로

function changeSku(confAttributeId, sel) { 
    var productMap = <?php echo Mage::helper('core')->jsonEncode($productMap);?>; 
    var selectedAttributeId = sel.options[sel.selectedIndex].value; 
    if (selectedAttributeId) { 
     var options = spConfig.config.attributes[confAttributeId].options; 
     var productId = options.find(function (option) {return option.id == selectedAttributeId}).products[0] 
     $("sku-container").update("<strong>Product Id: </strong>" + productMap[productId]); 
    } else { 
     $("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id"); 
    } 
} 


, 아래의 요약 전체 템플릿이 어떻게 보이는지 (조금 미화했습니다.) :

<?php 
$_product = $this->getProduct(); 
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes()); 
?> 
<?php if ($_product->isSaleable() && count($_attributes)):?> 
<dl> 
    <?php foreach($_attributes as $_attribute): ?> 
    <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt> 
    <dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>> 
     <div class="input-box"> 
      <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select" 
        onchange="return changeSku(<?php echo $_attribute->getAttributeId() ?>, this);"> 
       <option><?php echo $this->__('Choose an Option...') ?></option> 
      </select> 
     </div> 
    </dd> 
    <?php endforeach; ?> 
</dl> 
<script type="text/javascript"> 
    var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>); 
</script> 

    <?php endif;?> 

<div id="sku-container"></div> 

<?php 
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product); 
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions(); 

$productMap = array(); 
foreach($col as $simpleProduct){ 
    $productMap[$simpleProduct->getId()] = $simpleProduct->getSku(); 
} 
?> 

<script type="text/javascript"> 

document.observe("dom:loaded", function() { 
    $("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id"); 
}); 

function changeSku(confAttributeId, sel) { 
    var productMap = <?php echo Mage::helper('core')->jsonEncode($productMap);?>; 
    var selectedAttributeId = sel.options[sel.selectedIndex].value; 
    if (selectedAttributeId) { 
     var options = spConfig.config.attributes[confAttributeId].options; 
     var productId = options.find(function (option) {return option.id == selectedAttributeId}).products[0] 
     $("sku-container").update("<strong>Product Id: </strong>" + productMap[productId]); 
    } else { 
     $("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id"); 
    } 
} 
</script> 

이것은 원래 필요한 작업을 수행합니다.


은 또한 다음과 같은

  1. 귀하의 접근 방식은 두 개 이상의 구성 속성로 구성 제품에 대한 작동하지 않습니다. 해당 제품의 경우 사용자가 모든 선택 입력 값을 선택할 때까지 최종 단순 제품을 알 수 없습니다. 따라서 SKU를 출력하기 전에 모든 선택을 확인하기위한 접근 방식을 변경해야합니다.
  2. 신제품에 대한 구성을 지정하는 대신 사용자가 제품 구성을 편집 할 때 코드에서 대소 문자를 고려하지 않습니다. 쇼핑 카트에서 편집 링크를 클릭하여 편집 모드로 이동할 수 있습니다. 이 경우 모든 선택 입력은 이전에 선택한 값으로 미리 채워집니다. 그러나 텍스트는 "제품 ID를 표시하는 옵션을 선택하십시오"라고 말합니다. 스크립트는 편집 모드에서 다른 Javascript 오류를 생성 할 수도 있습니다. 수정 모드를 지원하려면 코드를 약간 수정해야합니다.
  3. 템플릿에 로직이 너무 많이 채워집니다. Magento 템플릿에는 간단한 인쇄물과 foreach 자 리가 있어야합니다.$conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions()과 같은 모든 메서드는 블록으로 이동하는 것이 좋습니다. 이렇게하면 코드 복잡성이 줄어 듭니다. 도움이 되길 바랍니다.
+0

감사합니다. Andrey, 당신은 나를 약혼시킵니다. – Dar

+0

@Andrey Tserkus - 이것은 우수합니다. 해결 한 3 가지 문제를 다루는 예가 있습니까? 나는 2 개 이상의 구성 가능한 속성을 처리하는 확장 프로그램, 플러그인 또는 예제를 찾기를 꺼려했습니다. 또한 사용자가 옵션 중 하나를 변경하면 sku를 업데이트하는 무언가를 찾고 있습니다. 모든 지침은 크게 감사하겠습니다. 고맙습니다! – NotJay

관련 문제