我正在使用Magento 1.9.2.4(1.9.2.3在我的測試頁),並且我有一些可配置的產品,其中有多個選項和每個產品(可配置的兒童產品)具有不同的交付時間。我創建了一個名爲「delivery_time」的屬性,我想在客戶選擇一個選項時進行更新。爲了達到這個目的,我找到了一些我使用的代碼片段。但它沒有正確更新。Magento 1.9.2 - 更新可配置產品過程中的自定義屬性'delivery_time'
所以這裏是我的應用程序/設計/前端/ RWD/fitgmbh /模板/目錄/產品/視圖/類型/選項/ configurable.phtml
<?php
$_product = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
?>
<?php if ($_product->isSaleable() && count($_attributes) && $_product->isConfigurable()):?>
<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;?>
<?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->getDeliveryTime();
}
if($_product->getTypeId() == "configurable"):
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
foreach($simple_collection as $simple_product){
echo $simple_product->getName() . " - " . $simple_product->getDeliveryTime() . "<br>";
}
endif;
?>
<script type="text/javascript">
document.observe("dom:loaded", function() {
$("delivery").update("Bitte Optionen wählen");
});
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]
$("delivery").update("Lieferzeit: " + productMap[productId]);
} else {
$("delivery").reset(); //just a test ;-)
}
}
</script>
<?php echo $this->getChildHtml('after') ?>
,我表現出的應用程序/設計輸出/前端/ RWD/fitgmbh /模板/目錄/產品/ view.phtml與
<div id="delivery"></div>
我知道,這可能是很難理解我的問題,所以我想我必須提供一個鏈接到我的Testpage。在可配置塊中,我列出了所有可用選項組合,其中最後一個數字代表我的(測試)delivery_time(1-12)。我真的不知道爲了使代碼正常工作我必須做些什麼。我認爲「最簡單」解決方案的一部分可能是重置所有輸入,如果客戶「退回」選項選擇過程。但我的JavaScript技能並不存在。除此之外,其他一些交付更新也不正確。但經過數小時的試驗和錯誤,我至少暫時放棄了。也許有人可以幫助我。我非常感謝每一個提示!我希望我已經用適當的方式描述了我的「問題」。
我按照Chris Rogers的建議創建了一個名爲「Arithon_DeliveryUpdate」的模塊,但我從來沒有使用觀察者或路由器創建模塊。所以我的模塊肯定有問題。至少其活性;-)
應用程序/代碼/本地/ Arithon/DeliveryUpdate的/ etc/config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Arithon_DeliveryUpdate>
<version>1.0</version>
</Arithon_DeliveryUpdate>
</modules>
<frontend>
<routers>
<catalog>
<args>
<module>Arithon_DeliveryUpdate</module>
<frontName>delivery_time</frontName>
</args>
</catalog>
</routers>
</config>
應用程序/代碼/本地/ Arithon/DeliveryUpdate /控制器/ DeliveryController.php
<?php
public function updateAction() {
$match = 0;
if ($this->getRequest()->isPost()) {
extract($this->getRequest()->getPost());
$_storeId = Mage::app()->getStore()->getId();
$_attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attrId);
$_product = Mage::getModel('catalog/product')->load($productId);
if ($_product && $_attribute && is_object($_product) && $_product->type_id == 'configurable') {
$_attrCode = $_attribute->getData('attribute_code');
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $_product);
foreach($childProducts as $child) {
$cId = $child->getId();
$v = Mage::getResourceModel('catalog/product')->getAttributeRawValue($cId, $_attrCode, $storeId);
if ($v == $selectValue) {
$configAttr = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'delivery_time');
$configAttrId = $configAttr->getId();
$configAttrValue = Mage::getResourceModel('catalog/product')->getAttributeRawValue($cId, 'delivery_time', $storeId);
$match = array("attrId" => $configAttrId, "attrValue" => $configAttrValue);
break;
}
}
}
}
return $match;
}
?>
而且我修改configurable.phtml
<?php
$_product = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
?>
<?php if ($_product->isSaleable() && count($_attributes) && $_product->isConfigurable()):?>
<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="productAddToCartForm.updateDelivery(this, <?php echo $_product->getId() ?>);" name="super_attribute[<?php echo $attrId ?>]" id="attribute<?php echo $attrId ?>" 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;?>
<?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->getDeliveryTime();
}
//echo $simpleProduct->getDeliveryTime();
if($_product->getTypeId() == "configurable"):
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
foreach($simple_collection as $simple_product){
echo $simple_product->getName() . " - " . $simple_product->getDeliveryTime() . "<br>";
}
endif;
?>
<script type="text/javascript">
if (typeof productAddToCartForm != "undefined") {
productAddToCartForm.updateDelivery= function(select, product_id) {
if (select != null && product_id != null && typeof select.selectedIndex != "undefined") {
var keyword = 'attribute';
var url = '/deliveryupdate/delivery/update'; // don´t know what to put here
var val = select.options[select.selectedIndex].value;
var attrId = select.getAttribute("id").replace(keyword, "");
var formData = {
selectValue: val,
productId: product_id,
attrId: attrId
};
// Make request to controller which will determine which value the configurable_attr_placeholder
jQuery.ajax({
url: url,
type: 'POST',
data: formData,
success: function(data) {
// PHP returns string readily convertable to JSON
var response = JSON.parse(data);
if (typeof response == 'object') {
// JSON key values are attrId and attrValue
var delivEl = document.getElementById(keyword + response.attrId);
if (typeof delivEl != "undefined" && configSel) {
delivEl = response.attrValue;
break;
}
}
}
});
}
};
}
</script>
<?php echo $this->getChildHtml('after') ?>
除了失誤,是這個模塊應該交流tively更新我的自定義屬性「delivery_time」這樣我就可以使用
<?php echo $_product->getdelivery_time()?>
我view.phtml
?我不必使用
<div id="delivery"></div>
了?
在此先感謝
感謝您的建議。我會盡快嘗試!如果我能夠/不能夠正確應用您的建議,我會給予迴應。再次感謝! – CrynosArioch
不用擔心男人! –
我試圖使用你的建議,但我的模塊,如上所述,不能正常工作。也許你可以再看看我的練習嗎? – CrynosArioch