1

Code for the analysis of the payment page but does not write the code in javascript codes..

For example (HTML Print):


<script>
woopra.track('Odeme Sayfasi', {
    urunSayisi: '',
    amount: '',
    currency: '$'
});
</script>
    21234.55

2: 2 pcs products

1234.55: amount

PHP & Javascript codes:

<?php 
$mageFilename = '/home4/emre2010/public_html/app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('checkout/session');
$output = "";
foreach ($session->getQuote()->getAllItems() as $item) { ?>

<script>
woopra.track('Payment Page', {
    urunSayisi: '<?php $output .= $item->getQty(); ?>',
    amount: '<?php $output .= $item->getBaseCalculationPrice(); ?>',
    currency: '$'
});
</script>

<?php } print $output;  ?>

Why not write into the javascript code? - Where I make mistake?

P.S: E-commerce script: Magento1

3 Answers 3

3

You're mixing 2 approaches. When you close the PHP tags and start your JavaScript code, that's getting output directly into the page. Then you're storing information in a variable instead of outputting it directly into the content.

You need to do something like this:

<script>
woopra.track('Payment Page', {
  urunSayisi: '<?php echo $item->GetQty(); ?>',
  amount: '<?php echo $item->getBaseCalculationPrice(); ?>',
  currency: '$'
});
</script>

If you want to build your JavaScript code and spit it out all in one go, then you'd need to put your JS code into a PHP variable and then print the output, like so:

<?php
$output = '';
$output .= "<script>";
$output .= "  woopra.track('PaymentPage', {";
$output .= "    urunSayisi: '" . $item->GetQty() . "',";

etc.

But the top approach of printing directly into your code is simpler and cleaner for what you seem to be doing.

Sign up to request clarification or add additional context in comments.

Comments

2

You're not printing the values where you think you are, just concatenating them into $output. I'd do it like this:

<?php 
$mageFilename = '/home4/emre2010/public_html/app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('checkout/session');

$output = "";
foreach ($session->getQuote()->getAllItems() as $item) { 
    $qty = $item->getQty();
    $price = $item->getBaseCalculationPrice();
    $output .= $qty . $price;
    ?> 
    <script>
    woopra.track('Payment Page', {
        urunSayisi: '<?php print $qty ?>',
        amount: '<?php print $price ?>',
        currency: '$'
    });
    </script>

<?php 
}
print $output;
?>

Comments

1

You should echo or print

<script>
woopra.track('Payment Page', {
    urunSayisi: '<?=$item->getQty()?>',
    amount: '<?=$item->getBaseCalculationPrice()?>',
    currency: '$'
});
</script>

2 Comments

you will get an error if you do not have short_open_tag set to On in php.ini . it is the best practice not to write short hand. instead you should use <?php echo $blah; ?>
Since PHP 5.4.0, <?= is always available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.