问题Magento =插入元素并调用函数javascript

问题Magento =插入元素并调用函数javascript

问题描述:

if u see javascript don't edit because its related with magento. thank you!

i have called a phtml script under price box in product view page with a validation function this is the phtml :

<script type="text/javascript">
//<![CDATA[

    $$('#product-price-<?php echo Mage::registry('current_product')->getId(); ?>').each (function(elem){
        $(elem).hide();
    });

    $('product-price-<?php echo Mage::registry('current_product')->getId(); ?>').show();
    var Cp = document.getElementById('product-price-<?php echo Mage::registry('current_product')->getId(); ?>');

    Cp.innerHTML = '$&nbsp;<input type="text" class="input-text price" name="custom_price" style="width:auto;" value="<?php echo Mage::registry('current_product')->getPrice(); ?>" onchange="onChangeCP(this);"/></span><input type="hidden" id="custom_price_total" name="custom_price_total" value="<?php echo Mage::registry('current_product')->getPrice(); ?>">';

    <?php 

        $product = Mage::getModel('catalog/product')->load(Mage::registry('current_product')->getId()); 
        $allowCustomPrice = $product->getAttributeText('allow_customprice');
        $minPrice = $product->getMinPrice(); //getAttributeText('min_price');
    ?>

    function changeCustomPrice(el){
        adviceBlock=$(el).up('span').down('.validation-advice');
        if(el.value.replace(',','.')>=<?php echo $minPrice; ?>){
            if(adviceBlock){
                adviceBlock.remove();
                el.removeClassName('validation-failed');
            }
        }else{
            if(!adviceBlock){
                el.insert({after:'<div class="validation-advice">The price should be equal or greater than <?php echo $minPrice; ?>.</div>'});
                el.addClassName('validation-failed');
            }
        }
        optionsPrice.productPrice=el.value;
        optionsPrice.reload();
    }
    optionsPrice.formatPrice=function(price){
        $('custom_price_total').value=price;
        return formatCurrency(price,optionsPrice.priceFormat);
    };
//]]>
</script>

this script hide the span that displays the price then it draw a input box with the price so that the client enter his price that should be greater than the mean price; if it s less then it draw an error.

when clicked in add to cart button the damn function doesn't execute and nothing is done !

if this method is wrong then how can i hide the span and add the input in product view using magento (php/...)

I'm not sure your issue is the function not running or addtocart fail. First, add to cart button is irrelevant to the price showing block. Second, you didn't bind the event on add to cart, so even you click button that will not execute. Last, maybe I miss something but I found the price that customer changed will not affect the price in your Magento. after these process the price might show different to user on product page, but it still show original price in cart.

you need to modified the price before product add to cart from php.