我正在使用Magento 1.9.2.4(在我的测试页面中为1.9.2.3),我有一些可配置的产品有超过1个选项,每个产品(可配置的产品)具有不同的交付时间。我创建了一个名为“delivery_time”的属性,我希望在客户选择选项时更新该属性。为了实现这一点,我找到了一些我使用的代码片段。但它没有正确更新。
所以这是我的app / design / frontend / rwd / fitgmbh / template / catalog / product / view / type / options / 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') ?>
我在app / design / frontend / rwd / fitgmbh / template / catalog / product / view.phtml中显示输出
<div id="delivery"></div>
我知道可能很难理解我的问题,所以我想我必须提供一个指向我Testpage的链接。在可配置块中,我列出了所有可用的选项组合,其中最后一个数字“代表”my(test)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') ?>
除了错误之外,这个模块是否应该主动更新我的自定义属性“delivery_time”以便我可以使用
<?php echo $_product->getdelivery_time()?>
在我的view.phtml中?我不得不使用
<div id="delivery"></div>
了吗?
提前致谢
答案 0 :(得分:1)
Magento 1.9仍然使用原型来处理选择的onchange
。您可以像这样扩展此功能:
在 app / design / frontend / YOUR_INTERFACE / YOUR_THEME / template / catalog / product / view / type / options / configurable.phtml 中 - 在onchange
中添加额外的功能:< / p>
<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">
然后在JS中,扩展原型:
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 = '/YOUR_ROUTER/delivery/update';
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;
}
}
}
});
}
};
}
注意:我也在这里使用jQuery,因为该库现在也可以在Magento 1.9中使用,我个人喜欢他们的AJAX功能。
注意我使用AJAX调用控制器方法(var url = '/YOUR_ROUTER/delivery/update';
) - 此方法将返回您的交付时间。
为了使此方法有效,您需要create a custom module并设置事件路由器
所以在app / code / local / YOUR / MODULE / controllers / DeliveryController:
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;
}
注意这将获得配置产品 - 获取所有使用过的产品并搜索其交付时间然后将其返回。准备在JS中使用
这应该得到正确的交货时间。您还可以使用此控制器返回有关您的相关产品的任何其他信息,以更新其他内容!请注意,这是未经测试的代码,所以请问你是否被卡住了!
真的希望这有帮助。