1

I am working on a project where on the product page instead of the normal configurable options, there are some configurable options and then the database is queried to see if particular vendors carry the product. It then displays the list of vendors via javascript as shown below.

enter image description here

I want the add to cart block to show up next to EACH vendor. Because this is all dynamically created, I had to pass the vendor ID to an "add to cart" script that I created. I took the original app/design/frontend/base/default/template/catalog/product/view/addtocart.phtml and made my own as seen below. The following php file is called via ajax. The original addtocart.phtml had a bunch of $this variables. I need to simulate $this (whatever model, helper its referring to) so that this block works. I am without much success. Can someone see what I am doing wrong or what I could do differently? Thanks so much!

    <?php require_once('/var/www/Staging/public_html/app/Mage.php'); ?>
<?php umask(0); Mage::app();?>
<?php $value = $_POST['value'];?>

<?php $block = Mage::getModel('catalog/product');?>
<?php $_product =  Mage::registry('current_product'); ?>
<?php //if($_product->isSaleable()): ?>
    <div class="add-to-cart">
        <?php //if(!$_product->isGrouped()): ?>
        <label for="qty"><?php echo $block->__('Qty:') ?></label>
        <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $block->getProductDefaultQty() * 1 ?>" title="<?php echo $block->__('Qty') ?>" class="input-text qty" />
        <?php //endif; ?>
        <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" id='$value'><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php echo $block->getChildHtml('', true, true) ?>
        </div>
    <?php //endif; ?>
1

Let's get to the root of what you're asking:

I need to simulate $this (whatever model, helper its referring to) so that this block works. I am without much success.

The addtocart.phtml view is called via this xml declaration in app/design/frontend/base/default/layout/catalog.xml:

<block type="catalog/product_view" name="product.info.addtocart" as="addtocart" template="catalog/product/view/addtocart.phtml"/>

That means that the $this you're referring to is the model catalog/product_view. Assuming no rewrites, that should translate to the class file Mage_Catalog_Block_Product_View, found in app/code/core/Mage/Catalog/Block/Product/View.php.

So, with all of that aside, you want $this to behave as if it's being called from the block. Assuming you're still using your standalone script called via AJAX, you should replace $this with $block - loading that as a new instance of the block model referenced above. This may lead to undesired effects - for instance - $block->getProduct() won't work.

Edited standalone addtocart.phtml:

Based on your edit, I have updated this answer:

<?php

require_once('/var/www/Staging/public_html/app/Mage.php');
umask(0);
Mage::app();

//ensure that the value is legitimate
if($_POST && is_numeric($_POST['value'])){
    $value = $_POST['value'];
}

//pass this in your ajax call for the add button
if($_POST && is_numeric($_POST['product_id'])){
    $product_id = $_POST['product_id'];
}

$helper = Mage::helper('core'); //for translation
$block = new Mage_Catalog_Block_Product_View(); // not best practice, but neither are standalones
$product =  Mage::getModel('catalog/product')->load($product_id); // no need to use the _ here, it's not protected/private; additonally Mage::registry won't work because you're technically not on a product detail page

$buttonTitle = ''; //you are using this, but it isn't set

?>
<div class="add-to-cart">
    <label for="qty"><?php echo $helper->__('Qty:') ?></label>
    <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $block->getProductDefaultQty($product) * 1 ?>" title="<?php echo $helper->__('Qty') ?>" class="input-text qty" />
    <button onclick="window.location = '<?php echo Mage::helper('checkout/cart')->getAddUrl($product);?>'" type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" id='$value'><span><?php echo $buttonTitle ?></span></button>
</div>

Some other thoughts:

You're using $_GET['value'] - don't accept unfiltered input from users. You're placing this directly into the add to cart button. Anyone could munge this to inject scripts and other bad stuff onto a page via the URL. This is called XSS - read more here: http://phpmaster.com/php-security-cross-site-scripting-attacks-xss/

If $_GET['value'] is your vendor, consider using vendor - it's semantic.

How I would implement this:

So, your approach isn't wrong, per se; it'll work. But here's how I would do it:

  • Create a vendor plugin
  • Create a custom controller action that takes an argument of vendor, using $this->getRequest()->getParams() - which filters natively for nefarious stuff
  • Set the output type of the controller to text/html, set to a blank theme that has no script includes or layout elements (structural blocks, etc.)

Or, instead of rolling your own, use the Unirgy Marketplace plugin which does what you're trying to do - and does it very well!

http://www.unirgy.com//products/umarketplace/magento-multi-vendor-marketplace

  • Hey, I think your example uses the add to wishlist or similar block vs the add to cart. I'm still super confused. I have sent your suggestions to the big man, though I am only doing what they want right now. I think creating a whole plugin for this might be a lot for us right now. Those last couple bullets I'm confused about. – CaitlinHavener Apr 18 '13 at 0:12
  • Is there any way that I can get my most recent edit to the code above to work? The error it throws me now is [Wed Apr 17 17:10:30 2013] [error] [client 142.196.40.159] PHP Fatal error: Uncaught exception 'Varien_Exception' with message 'Invalid method Mage_Catalog_Model_Product::__(Array\n(\n [0] => Qty:\n)\n)' in /var/www/Staging/public_html/lib/Varien/Object.php:652 – CaitlinHavener Apr 18 '13 at 0:13
  • You're totally right. Update inbound. – philwinkle Apr 22 '13 at 3:55
  • 1
    Glad to know a duplicate question was posted with a bounty. :-/ – philwinkle Apr 23 '13 at 17:19
  • 1
    upvote for you philwinkle! ;) – B00MER Apr 25 '13 at 7:50

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.