gpt4 book ai didi

javascript - Magento 1.9.2 - 更新可配置产品流程中的自定义属性 'delivery_time'

转载 作者:行者123 更新时间:2023-12-03 07:21:35 26 4
gpt4 key购买 nike

我正在使用 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 的链接。在可配置 block 中,我列出了所有可用的选项组合,其中最后一个数字“代表”我的(测试)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>

应用程序/代码/本地/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>

还有吗?

提前致谢

最佳答案

Magento 1.9 仍然使用原型(prototype)来处理选择的 onchange。您可以像这样扩展此功能:

app/design/frontend/YOUR_INTERFACE/YOUR_THEME/template/catalog/product/view/type/options/configurable.phtml 中 - 在 onchange 中添加额外的函数>:

<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中,扩展原型(prototype):

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 调用 Controller 方法 (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中使用

这应该获得正确的交货时间。您还可以使用此 Controller 返回有关关联产品的任何其他信息以更新其他内容!请注意,这是未经测试的代码,因此如果您遇到困难,请询问!

真心希望这会有所帮助。

关于javascript - Magento 1.9.2 - 更新可配置产品流程中的自定义属性 'delivery_time',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36191300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com