0

my problem is, that i want to get a productUrl from productid (seems simple) BUT i need that productUrl in a file which is called by an ajax post. Ill show u some files to get an overview.

My test.phtml file which contains the link u can click and the ajaxpost which is calling my php file.

<?php
     $product_id = Mage::registry('current_product')->getId();
     $product = Mage::getModel('catalog/product')->load($product_id); 
?>

<?php if(Mage::helper('core')->isModuleOutputEnabled('XXXXX_Productpdf')): ?>
<?php $displayFrontend = Mage::getStoreConfig('datasheet/general/dropdown'); ?>
<?php if($displayFrontend == 1): ?>
<div class="pdfdiv">
    <i id="datasheetLoading" class="fa fa-spinner fa-spin"></i><a href="" id="createDatasheet"> <?php echo $product->getName();?> Datasheet </a><label id="generatingDatasheet">Generating Datasheet</label>
    <input id="datasheetProductId" type="hidden" value="<?php echo $product_id?>">
</div>
<script>
    jQuery("#datasheetLoading").css("display", "none");
    jQuery("#generatingDatasheet").css("display", "none");
    jQuery('#createDatasheet').click( function() { 
        var productid = jQuery("#datasheetProductId" ).val();
        var myKeyVals = {"productid" : productid};
        test = "test";
        jQuery.ajax({
            type: "POST",
            data: myKeyVals,
            url: "/app/code/local/XXXXX/Productpdf/pdf/createDatasheet.php",
             beforeSend: function(xhr){          
                jQuery("#datasheetLoading").css("display", "");
                jQuery("#createDatasheet").css("display", "none");
                jQuery("#generatingDatasheet").css("display", "");
            },
            success: function(data) {
                window.open(data,'_blank' );
                jQuery("#datasheetLoading").css("display", "none");
                jQuery("#generatingDatasheet").css("display", "none");
                jQuery("#createDatasheet").css("display", "");  
            }
        });
        return false;
    });

</script>
<?php endif ?>
<?php endif ?>

So as u can see this phtml contians a link which call my createDatasheet.php. In this file i need to get the product url all i got is the product id. createDatasheet.php:

<?php
    require_once("../../../../../Mage.php");    
    umask(0);
    Mage::app();
    $productid = $_POST["productid"];
    $product = Mage::getModel('catalog/product')->load($productid);
    $productName = $product->getName();
?>

If i echo the $productName there comes the name of my product so this part works so what i need now is:

$productUrl = ?

what i have tried:

    Mage::getUrl().$product->getUrlPath(); 
    Mage::getUrl().$product->getUrlPath();
    $product->getUrlInStore();
    $product->getProductUrl();
    Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);

What it should return

http://magento2.dev/index.php/categoryName/productName.html

What it actually return

http://magento2.dev/createDatasheet.php/productName.html?SID=s8v4n82sotc3tv7dpot8dq6df6

If i call the $product->getProductUrl(); in my test.phtml file i get the url i need.

1

If you are just looking to get the product url to createDataSheet.php, why don't you just send it through the ajax?

<script>
jQuery("#datasheetLoading").css("display", "none");
jQuery("#generatingDatasheet").css("display", "none");
jQuery('#createDatasheet').click( function() { 
    var productid = jQuery("#datasheetProductId" ).val();
    var myKeyVals = {
        "productid" : productid,
        "productUrl": <?php echo $product->getProductUrl(); ?>
    };
    test = "test";
    jQuery.ajax({
        type: "POST",
        data: myKeyVals,
        url: "/app/code/local/XXXXX/Productpdf/pdf/createDatasheet.php",
         beforeSend: function(xhr){          
            jQuery("#datasheetLoading").css("display", "");
            jQuery("#createDatasheet").css("display", "none");
            jQuery("#generatingDatasheet").css("display", "");
        },
        success: function(data) {
            window.open(data,'_blank' );
            jQuery("#datasheetLoading").css("display", "none");
            jQuery("#generatingDatasheet").css("display", "none");
            jQuery("#createDatasheet").css("display", "");  
        }
    });
    return false;
});

</script>

Then just get it like you do with productid:

$productUrl = $_POST["productUrl"];
0

http://magento2.dev/createDatasheet.php/productName.html?SID=s8v4n82sotc3tv7dpot8dq6df6 is simply the url with session id parameter.

First I'd ensure that this is disabled in the magento back-end - see this answer

If that doesn't work, you can quickly strip the parameter from the URL using the following php:

$url = strtok($url, '?');

More information here.

EDIT To fix the URL as specified in your comment below, you can use a simple str_replace:

$url = strtok($url, '?');
$ur = str_replace('createDatasheet.php','index.php/CategoryName', $url);

Which will replace the 'createDatasheet.php' part of the url with 'index.php/CategoryName' - creating a proper URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.