5

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');
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_Blockproduct_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>
CaitlinHavener
  • 1,408
  • 3
  • 24
  • 53
  • hey :) what are you trying to do here? post your addtocart.phtml also here – Meabed Apr 21 '13 at 21:33
  • That is my addtocart.phtml! What I don't have is the script attached. I have a jquery $j.ajax call to post to the php. – CaitlinHavener Apr 21 '13 at 22:22
  • You want to show the options for each product with qty select and add to cart button , is this right ? i cant understand clearly for your question whats the purpose ? – Meabed Apr 23 '13 at 02:05

2 Answers2

2

Mage_Catalog_Blockproduct_View is the error you're getting currently. Remember Magento class names are reflective of the directory structure.

I believe you are wanting Mage_Catalog_Block_Product_View

However, technically you would not want to initiate a block here, but I realize it is to get around the $this-> references. Also I would not reinitialize the full stack again. A better approach is to create a new module and use a custom controller for the add to cart action.

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Checkout') . DS . 'CartController.php';

class NS_AjaxCart_CartController extends Mage_Checkout_CartController
{
    public function addAction()
    {
        $params = $this->getRequest()->getParams();
...

You would then create a new Block in your module with a simple constructor to set the template you are going to be using in your block. Similar to:

public function __construct()
{
        parent::__construct();
        $this->setTemplate('catalog/vendoraddtocart.phtml');
}

To dynamically create a block, as $this would be accessible:

$this->getLayout()->createBlock('cms/block')->setBlockId('vendor_add_to_cart')->toHtml()

Consider reading and better understand the toHtml method, and how blocks and layouts all work:

Hope this helps.

Community
  • 1
  • 1
B00MER
  • 5,471
  • 1
  • 24
  • 41
1
$this->getProduct() return a product loaded from Mage_Catalog_Model_Product
So it not a registry So cant be used as Mage::registry('current_product')

Use something as

$_product = Mage::getModel('catalog/product')=>load(prodid)

Your approach seems wrong :(

Sandeep
  • 943
  • 1
  • 7
  • 11
  • I've already tried that one before. See where its commented out? //$product = Mage::getModel('catalog/product')->load($id); – CaitlinHavener Apr 22 '13 at 00:50
  • See working on it here too http://magento.stackexchange.com/questions/3060/dynamically-create-add-to-cart-block-for-each-final-configurable-option-helper/3066?noredirect=1#comment4245_3066 – CaitlinHavener Apr 22 '13 at 20:05
  • See updated code above! I am getting following error now: PHP Fatal error: Class 'Mage_Catalog_Blockproduct_View' not found in /var/www/Staging/public_html/ajax_calls/addToCartBlock.php on line 17 – CaitlinHavener Apr 22 '13 at 20:15
  • Mage_Catalog_Blockproduct_View you are targeting to some wrong class It should be Mage_Catalog_Block_Product_View – Sandeep Apr 23 '13 at 13:59