【发布时间】:2016-03-24 00:40:28
【问题描述】:
我正在使用 Magento 1.9.2.4(我的测试页面中的 1.9.2.3)并且我有一些具有多个选项的可配置产品,并且每个产品(可配置产品的子产品)都有不同的交货时间。我创建了一个名为“delivery_time”的属性,我想在客户选择一个选项时对其进行更新。为了实现这一点,我找到了一些我使用的代码 sn-ps。但它没有正确更新。
所以这是我的 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 的链接。在可配置块中,我列出了所有可用的选项组合,其中最后一个数字“代表”我的(测试)delivery_time (1-12)。我真的不知道我必须做什么才能让这段代码正常工作。我认为“最简单”的解决方案的一部分可能是在客户“返回”选项选择过程时重置所有输入。但我的 javascript 技能并不存在。除此之外,其他一些交付更新也不正确。但经过数小时的反复试验后,我放弃了,至少目前是这样。也许你们中的某个人可以帮助我。我真的很感谢每一个提示!我希望我以恰当的方式描述了我的“问题”。
按照 Chris Rogers 的建议,我创建了一个名为“Arithon_DeliveryUpdate”的模块,但我从未使用观察者或路由器创建模块。所以我的模块肯定有问题。至少它是活跃的;-)
app/code/local/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>
app/code/local/Arithon/DeliveryUpdate/controllers/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()?>
在我看来.phtml?我不必使用
<div id="delivery"></div>
还有吗?
提前致谢
【问题讨论】:
标签: javascript php magento