【问题标题】:Magento 1.9.2 - Update custom attribute 'delivery_time' in the configurable product processMagento 1.9.2 - 在可配置产品流程中更新自定义属性“delivery_time”
【发布时间】: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


    【解决方案1】:

    Magento 1.9 仍然使用原型来处理选择的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中,扩展原型:

    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中使用

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

    真的希望这会有所帮助。

    【讨论】:

    • 感谢您的建议。我会尽快试试这个!无论它是否有效,或者我是否能够/无法正确应用您的建议,我都会给出回应。再次感谢!
    • 我尝试使用您的建议,但如上所述,我的模块无法正常工作。也许你可以再看看我的练习?
    • 嘿伙计,have at look at this 它说明了一些先决条件 - 调试模块可能很麻烦。这篇文章可以提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2011-06-25
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    相关资源
    最近更新 更多