How to get product url by SKU? [duplicate] - php

The question is simple.
If I know the sku of my product and nothing else, how can I retrieve the url to that item. This is commonly useful for third party integration where the unique id at the remote service wont match a product id. Or maybe you want to make a search box to search by sku only.

You can just do this:
$sku = 'ecco'; // SKU you want to load. 'ecco' is a sku in the Magento demo data
$url = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku)->getProductUrl();
echo $url;

Also.... You could add this to a file named 'geturlbysku.php' in magento root dir. This code enables a small amount of json representing the product to be pulled easily, enabling small javascript integration. Product URL is part of the dataset
<?php
require_once '/app/Mage.php';
umask(0);
Mage::app('admin');
$actual_link = "http://$_SERVER[HTTP_HOST]";
$sku = $_GET['sku'];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$fullUrl = "/catalog/product/view/id/" . $product->getId();
header("Content-Type: application/json");
echo $_GET['callback'] . '('.json_encode($product->getData()).')';
?>
Use like this...
www.yoursite.com/geturlbysku.php?sku=theskuoftheproduct
Part of the data returned includes "url_path":"your-product-name.html" which corresponds to the real url.

You can use the load method. This is quicker and easier than all other solutions.
Mage::getModel('catalog/product')->load('sku', 'sku');
For more information on loading products Click Here

$product = Mage::helper('catalog/product')->getProduct($this->getData('sku'), Mage::app()->getStore()->getId(), 'sku');
$url = Mage::getUrl($product->getUrlPath());
You can access the full url of the product based on its sku by adding this to a method and then setting the sku attribute on the class to the sku that you wish to search by.

Related

Retrieve name of vendor - WooCommerce & The official product vendors plugin

I'm using the Product Vendors plugin for WooCommerce (https://woocommerce.com/products/product-vendors/).
I'm also creating a custom loop for a page builder module, which pulls products by ID.
Using the above, I'm able to pull the WC_Product data using:
$thisProduct = new WC_product($id);
From there, I can get the price/product image etc using
$image = $thisProduct->get_image();
$price = $thisProduct->get_price();
Does anyone know how I would be able to pull the Vendors name? I've one a var_dump on the full $thisProduct, and it doesn't contain the vendor name anywhere within.
Any help would be much appreciated!
Thanks guys.
If that product is having a vendor mostly it will be saved under 'post_author'; You can retrieve Vendor by that author id.
$author = get_user_by( 'id', $product->post->post_author );
This variable $author will have all the info like name.
I figure this out, In order to retrieve the vendor information, we can use the following ($id being the product ID)
$sold_by = WC_Product_Vendors_Utils::get_sold_by_link( $id );
$sold_by will then contain an array of the vendor info, such as the store name and the link to the vendor store, which we can retrieve such as:
$vendor_link = esc_url( $sold_by['link'] );
$vendor_name = $sold_by['name'];
Thanks guys, hopefully this can be of assistance with someone in the future.

Get access to $item->getNumberOfDownloadsUsed() function on Download.php

I need to get the number of downloads bought and used, that's two methods that are available on $item object. But I can't instance $item on the file that I'm working:
app/code/core/Mage/Downloadable/Helper/Download.php
On this file, I need to retrieve the number of boughts and used of the download purchased.
I'm also trying to get the order ID or at least the link hash to identify that number of boughts/used downloads with the unique id of the purchase.
For example, I try this:
Mage::getModel('downloadable/link_purchased_item')
->load($this->getOrderItem()->getOrder()->getId(), 'order_id');
But $this->getOrderItem() is not available on Download.php file. I was trying this:
Mage::getModel('downloadable/link_purchased_item')->getCollection()
->addFieldToFilter('order_item_id', $this->getOrderItem()->getId());
But obviously getOrderItem() is unavailable.
Fatal error: Call to undefined method Mage_Downloadable_Helper_Download::getOrderItem() in /[...]/app/code/core/Mage/Downloadable/Helper/Download.php on line 135
But I'm able to use the customer singleton to retrieve client data like this:
$cliente = Mage::getSingleton('customer/session')->getCustomer();
So on this file I'm able to access others methods, but I'm unable to get the following details:
Number of downloads bought
Number of downloads used
Order ID or Link hash ID.
So please, I'm requesting how to get the current order instance and / or the current link instance, on the Download.php related with the file downloading.
Thank you!
You are doing it wrong, you can not get any object like this $this->getOrderItem() without set/declaring them. As you are trying to get order_id and item_id in the helper, which you are using on a custom page redirected from customer downloadable products. Here is what you have to do
Step - 1
From customer My Downloadable products, in the redirect url you have to pass that products order_id and item_id as parameter. You can get them from below code
$orderId = $_item->getPurchased()->getOrderId();
$itemId = $_item->getId();
Step -2
Now in your template file while using your helper function pass this order_id and item_id to method parameter. Like below code
$orderId = $this->getRequest()->getParam('order_param');
$itemId = $this->getRequest()->getParam('item_param');
//Your helper function
Mage::helper('your_helper')->yourMethod($orderId, $itemId);
Step - 3
In your helper file you can use below code
public function yourMethod($orderId, $itemId)
{
$linkPurchasedItem = Mage::getModel('downloadable/link_purchased_item')
->load($orderItem, 'order_id');
$LinkPurchaseOrderItemId = Mage::getModel('downloadable/link_purchased_item')->getCollection()
->addFieldToFilter('order_item_id', $itemId);
}
Note: Do not make any changes in core files, instead override them

Product URL in a shared shopping cart in Magento multi-store websites

I have Magento multi-store websites that I want that the user will be able to add products to his shopping cart from all the website and pay once.
I successfully done it using this article.
But when the user click on the product in the shopping cart, he is not redirected to the right website. It's a limitation the described in the article at the end.
The link for editing items in the cart will redirect customer to
original cart website. It is possible to change it, you can override
getUrl method for cart items block or override controller.
I couldn't find any explaination to how to do this override.
Someone can help me do this?
Thanks
In my case i seen file of cart's item.phtml, it was using getproducturl().
So, I modified that method in file /app/code/core/Mage/Checkout/Block/Cart/Item/Renderer.php line 152.
I made condition that if its main website don't change otherwise it adds stores code in url like www.example/store1/Producturl.
I hope This will Help You.
As You Require The File method. Check belowed code.
public function getProductUrl()
{
if (!is_null($this->_productUrl)) {
return $this->_productUrl;
}
if ($this->getItem()->getRedirectUrl()) {
return $this->getItem()->getRedirectUrl();
}
$product = $this->getProduct();
$option = $this->getItem()->getOptionByCode('product_type');
if ($option) {
$product = $option->getProduct();
}
$webids = $product->getWebsiteIds();
$wid = array();
foreach($webids as $webid){
$wid[] = $webid;
}
if(!in_array(1,$wid)){
$website = Mage::app()->getWebsite($wid[0])->getCode();
$org_url = $product->getUrlModel()->getUrl($product);
$mod_url = str_replace("www.example.com/index.php/","www.example.com/".$website."/index.php/",$org_url);
return $mod_url;
}
return $product->getUrlModel()->getUrl($product);
}
As my first website id was "1", that's why i used in_array(1.$wid). You Can use your main website id over there. and in str_replace u can use baseurl() method of main website instead of static value i used.

product price for customer group

I hope someone can help me with this, I am trying to extract the product prices from magento based on customer group.
I am not using tier pricing, I simply have a product with a standard price and I have specified different prices for each customer group. For some reason I have been unable to extract this information.
I can see that the price mappings seems to be held in the table 'catalog_product_index_group_price' so I guess I could write direct SQL to extract these but I would much rather use the PHP Mage model to do this, or the V2 SOAP API.
I have tried many methods, currently im using something like below, but without success the price variable is always empty.
$rules = Mage::getResourceModel('catalogrule/rule');
$price = $rules->getRulePrice($now, $websiteId, $customer_group_id, $productID);
Please try the following
$product = Mage::getModel('catalog/product')->load($productId);
$groupPrices = $product->getData('group_price')
$groupPrices should now be an array with the data you are looking for.
the code didnt format well in the comment so here it is again!
<?php
include_once '../App/Mage.php';
Mage::app();
$productID = $_GET["id"];
$pd = Mage::getModel('catalog/product')->load($productID);
$groupPrices = $pd->getData('group_price');
echo json_encode($groupPrices);
?>

How to create simple product to attribute option value in magento

I created my own attribute set in magento, let say "MyAttribSet" with a large number of option values.
How can I retrieve these values and assign/create to each one a simple product programatically?
I found code sample here, but I'm missing the latter part with option values:
I'm using Magento version is 1.5.0.1
I solved the problem:
require_once 'app/Mage.php';
Mage::app('');
$attribute = Mage::getModel('eav/config')
->getAttribute('catalog_product', '955');
foreach ( $attribute->getSource()->getAllOptions(true) as $option){
//here comes the code sample (see link before)
.
.
.
$product->setMyCustomAttribute($option['value']);
}

Categories