Get product id in magento - php

In magento,i want to add quick look feature like this http://www.timberlandonline.co.uk/on/demandware.store/Sites-TBLGB-Site/default/Link-Category?cgid=men_footwear_boots.I have added a hidden input & a div in list.phtml.If i click the div of any product javascript returns product id of first product in that category page.But it should return product id of the selected div.

You need to look into this page (<path_to_your_template_folder>/template/catalog/product/list.phtml) carefully. You will find the following lines of code in different places of this page only:-
$_productCollection = $this->getLoadedProductCollection();
foreach ($_productCollection as $_product):
$reqProductId = $_product->getId();
endforeach;
If you carefully match the above code & the code in the above-mentioned page, you will know that you need to use the variable "$reqProductId" properly in your required "INPUT" element of type "hidden". So you will require it to do your part in the main "foreach" loop.
Hope it helps.

Try below code to get currently loaded product id:
$product_id = $this->getProduct()->getId();
When you don’t have access to $this, you can use Magento registry:
$product_id = Mage::registry('current_product')->getId();
Also for product type i think
$product = Mage::getModel('catalog/product')->load($product_id);
$productType = $product->getTypeID();

Related

Using wc_get_product() with a PHP variable for product ID

I'm building out custom landing pages for products in WooCommerce and I'd like to get the product price amongst other things in order to display them on the landing page.
Each landing page has some custom fields which allow the WP Admin to add in content, for the landing page as well as the product ID, which will then be used to generate the product price, checkout URL etc..
I can't get the wc_get_product(); to work with my custom field or a variable built off that. It only works when I use a straight ID. I think there's something I'm not understanding about how variables work within PHP. Here's my code.
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// This line is where the problem is...
$_product = wc_get_product('$courseID');
// If I replace the line above with this line
// $_product = wc_get_product('7217');
// everything works great, but that does not let
// each landing page function based on the custom fields where the user determines
// the product ID they are selling on that landing page.
// Get's the price of the product
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
Update
I get the following error using wc_get_product( $courseID ); or get_product( $courseID );:
Fatal error: Call to a member function get_regular_price() on a non-object in ...
Update related to your recent comment. The 2 ways to explore:
1) Instead of you should try to use to get the product object (avoiding the error):
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Get an instance of the product object
$_product = new WC_Product($courseID);
2) Alternatively if this doesn't work, you should try to use get_post_meta() function to get the product price (or any product meta data) this way:
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Get the product price (from this course ID):
$course_price = get_post_meta($courseID, '_regular_price', true);
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
This time you should get displayed the price with one or the other solutions.
Update: May be Also you need to convert $courseID to an integer variable.
Because you need to use your variable $courseID inside wc_get_product() (without the 2 ') function this way:
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Here
$_product = wc_get_product( $courseID );
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
This should work now.
You can try this out :
$courseID = the_field('course_id');
$product = get_product( $courseID );
I figured out the answer after running through the possible solution routes that #LoicTheAztec supplied in his response. None of these worked and so I assumed something else was up.
I use Advanced Custom Fields to add custom fields in the back end and I was using ACF's the_field() in order to create my variable. That is incorrect usage of that function as it's designed to display the field, (it's basically using php's echo). To work with these custom field's you need to use ACf's get_field() which is to use it to store a value, echo a value and interact with a value.
Once I switched to setting my $courseID to this..
$courseID = get_field('course_id');
Everything worked. My code worked, and all #LoicTheAztec's code approaches also worked.

Magento - Get the associated product attributes of an item in the wishlist

In app/code/local/Mage/Catalog/Product/Type/Configurable/Price.php, I am trying to get the attribute values of an associated product within the wishlist. I've attempted several approaches but I can only seem to produce data for the parent product.
Latest attempt
$customer = Mage::getSingleton('customer/session')->getCustomer();
if($customer->getId()) {
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $wlitem) {
$wishitem = Mage::getModel('catalog/product')->setStoreId($wlitem->getStoreId())->load($wlitem->getProductId());
//echo $wishitem->getId() . '<br>';
if($product->getId() == $wishitem->getId()) { //if the current product id equals the wishlist product id
echo $wishitem->getSku()."</br>";
}
}
}
That only gets me the parent product's sku. What I ultimately want to get is the attribute value for 2 attributes that I added for configurable products (not super attributes) but it seems that $product in Price.php only has the parent product collection.
Other Attempts:
$item_s = Mage::getModel('wishlist/item')->loadWithOptions($product->getId(), 'simple_product')->getOptionsByCode();
$simple_product = $item_s['simple_product']->getData();
$simple_product_id = $simple_product['product_id'];
$sim_product = Mage::getModel('catalog/product')->load($simple_product_id);
print_r($sim_product);
This only resulted in an error on the page.
Also:
$_item = Mage::getModel('catalog/product')->load($product->getId());
//echo $_item->getData('ppuom');
//print_r($_item);
$simpleProduct = $_item->getOptionsByCode()['simple_product']->getItem()->getProduct();
print_r($simpleProduct);
Seems as if you were most of the way there. I've tested this on my Magento site and it worked for me. It's pretty simple actually, you just have to grab the right model for that collection. Also, it seems like you're changing the pricing?!?! Be careful that your wishlist items contain the necessary attributes used in your logic.
$_item = Mage::getModel('catalog/product')->load($product->getId());
$attribute1 = $_item->getData('attribute1_code'); //see admin for attribute code
$attribute2 = $_item->getData('attribute2_code'); //see admin for attribute code
OR
Make changes to your template's wishlist files rather than the pricing logic in the code folder. You'll have access to all the data you need and it won't interfere with the price.php file which is relied on heavily in the cart and other critical areas of the website. The price in the wishlist is recalculated when it's moved to the cart anyway.

Product details in magento

I have this code in my magento app
<?php
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);
foreach ($order_details->getAllItems() as $item) {
//here i know we can get item name as $item->getName(), sku as $item->getSku()
//but how do we get the following details
//1.category name,2.store,3.tax,city,country,state
<?php } ?>
I know by simply print_r($order_details->getAllItems()) will get the array list.but im in no situation to do this
This items are not as same as products, they don't have all the properties. You can either do a Mage::getModel('catalog/product')->load($item->getProductId()); fast way to test something, but this adds an extra query to mysql for every item in the loop. Or I recommend to edit the sales xml -> Mage > Sales > etc > config.xml (in a local module). You should add to the nodes sales_convert_quote_item or sales_convert_order_item the attributes you want to be copied from product to item. In your case it would be the sales_convert_order_item node since you are dealing with an order.
You need to load the full product model to access that data.
The collection you are loading is not of products, it's a collection of Order Items, which has minimal data associated with it. You need to load the full product model, by getting the product if from the Order Item.
try this:
<?php
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);
?>
<?php foreach ($order_details->getAllItems() as $item): ?>
<?php $product = Mage::getModel('catalog/product')->load($item->getProductId()) ?>
<?php echo $product->getName() ?>
<?php // now you have the full product model/data loaded ?>
<?php endforeach ?>

Using magento product model in footer

I am trying to display the Swatch attributes of all the associated simple products to the configurable product that the user is viewing.
I need to do this in the footer which is proving more difficult than I thought as a lot of methods etc are not available in the footer.
I have this code which just shows the Swatch attribute for the configurable product, I need this modified to show the Swatch attribute for all the simple products associated to this configurable.
<?php
$SKU = "2726578";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$SKU);
echo $product->getSwatch();
?>
$sku = "2726578";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
if($product->getTypeId() == "configurable"){
$childs = $product->getTypeInstance()->getUsedProducts();
}
Hope this helps! Just iterate over the childs and fetch the values.
cheers!

get basket product id in observer

I've set an observer for sales_quote_add_item in order to clear the cart whenever a certain product is going to be added (it's only supposed to be ordered alone).
I'm just not sure on how to get the product ID of the product that is about to get added. With some trial & error I've come up with this:
$tmp = $observer->getEvent()->getQuoteItem()->getData();
echo $tmp['product_id'];
Which seems to be quite an ugly solution. I'm sure there is some shortcut or proper function to call for this, any ideas?
Your solution is good enough
$productId = $observer->getEvent()->getQuoteItem()->getProductId();
You may load product after if is needed
$product = Mage::getModel('catalog/product')->load($productId);
Use the following code to get product id of quote item data
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item)
{
$productId = $item->getProduct()->getId();
}
Hope this helps to you.

Categories