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


Magento 1.9.2 - Update custom attribute 'delivery_time' in the configurable product process

我使用的是Magento 1.9.2.4(在我的测试页面中为1.9.2.3),我有一些可配置的产品,有不止一个选项,每个产品(可配置产品的子产品)都有不同的交付时间。我创建了一个名为"delivery_time"的属性,希望在客户选择选项时更新该属性。为了实现这一点,我发现了一些我使用的代码片段。但它没有正确更新。

这是我的应用程序/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>

我知道这可能很难理解我的问题,所以我想我必须提供一个到我的测试页面的链接。在可配置块中,我列出了所有可用的选项组合,其中最后一个数字"代表"我的(测试)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;
}
?>   

和我修改的可配置.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>

还有吗?

提前感谢

Magento 1.9仍然使用原型来处理select的onchange。你可以这样扩展这个功能:

app/design/frontend/YOUR_INTERFACE/YOUR_THEME/template/cacatalog/product/view/type/options/configuration.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';),该方法将返回您的交付时间。

为了让这种方法发挥作用,你需要创建一个自定义模块并设置一个事件路由器

因此,在应用程序/代码/本地/你的/模块/控制器/交付控制器:

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

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

真的希望这能有所帮助。