Pull SKU, Manufacturer and custom attribute from Magento database - mysql

I'm trying to pull a full list of products from the database with the SKU, manufacturer and a custom attribute called 'GTIN'.
I'm really struggling with the custom attribute part.
This statement works to pull the manufacturer and SKU
SELECT cpe.sku, m1.manufacturer,
FROM catalog_product_entity AS cpe
INNER JOIN exportview_manufacturer AS m1 ON cpe.entity_id = m1.entity_id
My MySQL is very poor and I can't seem to get this custom attribute. I've found the following statement online which I KNOW has all the details I need to make this work but thus far I've been getting my head in a mess trying to implement it
SELECT e.entity_id AS product_id, var.value AS product_name
FROM catalog_product_entity e, eav_attribute eav, catalog_product_entity_varchar var
WHERE
e.entity_type_id = eav.entity_type_id
AND eav.attribute_code = 'gtin'
AND eav.attribute_id = var.attribute_id
AND var.entity_id = e.entity_id
I simply need the first statement to also include the 'gtin' column, but joining is where I'm falling short. Can someone please assist?

You need to create one PHP script to get all product and it's attribute. So first create all_product.php file on your Magento root and add below code.
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once '../app/Mage.php';
Mage::app();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*') // select all attributes
->setPageSize(5000) // limit number of results returned
->setCurPage(1); // set the offset (useful for pagination)
foreach ($collection as $product) {
echo $product->getName(); //get name
/*you can get any attribue you want like name above. Even you can get all custom attribule for product for that you need to use print_r($collection->getData()) above foreach*/
}
Hope it will work.

Related

Get all sku for an order through sql

Working on a script to pull some customer order data from various databases and combine them into one output. Most of the stuff works fine, but can't seem to be able to get the sku's for the products of an order.
Noticed there was a meta_key called "_sku" so tried with something like
$sqlMetaSku = "SELECT * FROM wp_postmeta WHERE meta_key='_sku' AND post_id='$post_id'";
$resultMetaSku = $conn->query($sqlMetaSku);
if ($resultMetaSku->num_rows > 0) {
while($rowMetaSku = $resultMetaSku->fetch_assoc()) {echo($sqlMetaSku);
$clientSku = $rowMetaSku["meta_value"];
echo (between ('s:8:', ';s:12', $clientSku));
}
}
but no luck here.
Noticed there was a key called "_order_key" so maybe that's the way to go? I am not sure how to proceed.

SQL code for search option in a php web page

I have tried to write the search.php file for an online library web page. This is what I have done , I can search books by titles ,but I also want to search them by author or description
if (isset($_GET['str'])) {
$category = htmlspecialchars($_GET['str']);
$qlib = 'SELECT * FROM book_description JOIN book_class ON book_class.series = book_description.series WHERE book_description.title LIKE "%'.$category.'%"'
SELECT * FROM book_description
JOIN book_class ON book_class.series = book_description.series
WHERE book_description.title LIKE "%'.$category.'%"'
and (or) book_description.author like "%'.$author.'%"'
...
and so on
Hope it helps! You can use and or or in where clause

Get number of swatches of an attribute that has a product

I have configurable products with a swatch attribute, in this case is color.
I need to know how many colors (the number) has a product, or maybe how many single products compose this configurable product.
As a matter of fact, I need to know when there is more than one color in a product.
Finally I found it, maybe it will help somebody in the future:
$productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
//in this case the attribute color that I needed is in [0] position
$available_colors = sizeof($productAttributeOptions[0]["values"]);
if ($available_colors >1):
//custom code
endif;
Edit: This solution works for one product, or at least for a few products, but if you need it in a list of products it is really slow to run this code in each of the products. Sometimes it drives to a timeout and closes the db connections, so the web crashes with an error.
Finally I got a solution, maybe it is not the best one, but it is pretty fast compared to the one I was using before:
$_idsForTheQuery = $_productCollection->getAllIds();
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql_query = "SELECT parent_id, COUNT(parent_id) AS colors FROM
(SELECT cpr.parent_id FROM `eav_attribute` a
LEFT JOIN `catalog_product_entity_int` cpei ON cpei.attribute_id=a.attribute_id
LEFT JOIN `catalog_product_relation` cpr ON cpr.child_id=cpei.entity_id
WHERE attribute_code = 'color' AND cpr.parent_id IN (".implode (", ", $_idsForTheQuery).")
GROUP BY cpr.parent_id, cpei.value) colors
GROUP BY parent_id";
$results = $read->query($sql_query);
$number_of_colors_by_id_array = array();
foreach($results as $r)
{
$number_of_colors_by_id_array[$r["parent_id"]] = $r["colors"];
}
and then in the foreach loop of the products
<?php if ($number_of_colors_by_id_array[$_product->getId()]>1): ?>
<div class="aditional-colors-message">
<?php echo __('more colors available'); ?>
</div>
<?php endif; ?>

Magento, i need define first product image as thumbnail

I had imported my old oscommerce site with 15000 produts for magento!
But unfortably, the thumbnails are dont defined automatically
http://imgur.com/cNdXd
i can do maually, but 15000 produts is a larg number and i need sped a big amout of time for do it
if anywone can give-me a mysql command to set the first produts image as Base Image, Small Image and Thumbnail will be awesome
thanks
Try update them via api:
$proxy = new SoapClient('http://magentohost/api/soap/?wsdl');
$sessionId = $proxy->login('apiUser', 'apiKey');
$products = $proxy->call($sessionId, 'product.list');
foreach ($products as $product) {
$images = $proxy->call($sessionId, 'product_media.list', $product['sku'])
$imageFilename = $images[0]['file'];
$proxy->call($sessionId, 'product_media.update', array(
$product['sku'],
$imageFilename,
array('types' => array('image', 'thumbnail')
));
}
Related documentation links:
http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_product_attribute_media
http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_product#catalog_product.list
Im having a problem with this SQL code i got somewhere which is supposed to set your first image as image, small image and thumbnail:
UPDATE catalog_product_entity_media_gallery AS mg,
catalog_product_entity_media_gallery_value AS mgv,
catalog_product_entity_varchar AS ev
SET ev.value = mg.value
WHERE mg.value_id = mgv.value_id
AND mg.entity_id = ev.entity_id
AND ev.attribute_code IN ('image', 'small_image', 'thumbnail')
AND mgv.position = 1;
Doesn't change any rows...
Also, my attribute_id's for image, small_image, thumbnail and media_gallery in Magento CE v1.6.2 are 106,109,493,703 using EMSupermarket template.
The following code also does nothing:
UPDATE catalog_product_entity_media_gallery AS mg,
catalog_product_entity_media_gallery_value AS mgv,
catalog_product_entity_varchar AS ev
SET ev.value = mg.value
WHERE mg.value_id = mgv.value_id
AND mg.entity_id = ev.entity_id
AND ev.attribute_id IN (106,109,493)
AND mgv.position = 1;
Anyone how to do this correctly with my id's?
What happened with my oscommerce product import was that the imported product images only end up under the field name media_image, but not image, small_image or thumbnail so i want to copy the media_image values over to the other 3, any ideas how to do this?
I had the same problem. I found this page and ran that SQL command, and also did not get any results. But I modified it to work for me. Here is what I did.
First of all, I changed line 7 from:
AND ev.attribute_code IN ('image', 'small_image', 'thumbnail')
to
AND ev.attribute_ID = '74'
the 74 is my unique attribute id for 'image'. You will have a different number here. You find it by looking it up in mySQL...
75 was for 'small_image' and 76 for 'thumbnail'.
So I ran this code 3 times, 1 for each of those attribute number. Each time I change line 7 ev.attribute_ID = '75' :
UPDATE catalog_product_entity_media_gallery AS mg,
catalog_product_entity_media_gallery_value AS mgv,
catalog_product_entity_varchar AS ev
SET ev.value = mg.value
WHERE mg.value_id = mgv.value_id
AND mg.entity_id = ev.entity_id
AND ev.attribute_ID = '76'
AND mgv.position = 1;

MySQL question: Appending text to a wordpress post, but only if it's in a certain category

I need to amend (via CONCAT, presumably) something to every wordpress post if it belongs to a certain category (say, category ID 7), but I'm struggling to get it to work.
To test, I'm first trying to select all the relevant posts. So far, I have the following:
SELECT post_title
FROM cruise_wp_posts
LEFT JOIN cruise_wp_term_relationships
ON cruise_wp_term_relationships.object_id = cruise_wp_posts.ID
WHERE term_taxonomy_id = 87;
However, it only lists posts that are only in category 87 - I need all posts that are in category 87 (and possibly other categories too)
I'm a MySQL newbie, and this is really breaking my brain.
Any pointers would be passionately welcomed.
The best way to do it is to filter it in as needed. This way the addition is made everywhere the_content is used and not just in the templates you modify.
<?php
function my_content_concat($the_content) {
if (in_category(7)) {
$the_content .= '<br /><br />foo!';
}
return $the_content;
}
add_filter('the_content', 'my_content_concat', 9);
?>
in_category can take the id, name or slug of your target category.
I put the filter at 9 so that it runs before WordPress texturizes the content. If you don't need that run it at 11.
Why not just use get_the_category( $id ) and ammend the text when you output the post?
$cat = get_the_category( $postID );
if ($cat == 7) {
//Add text here
}