I am trying to update some (but not all ) prices from one store to another. For example one line of shirts should be 1.2 times the price in the second store
I would like to update some groups of items only, based on entity label but am struggling to pull all the data from magento (asside what i can get out in the code below). The bits I am missing are price and entity label. I know what tables they live in but not sure the correct magento syntax to access them eg Mage::getModel('catalog/product') and I am trying to achieve this using Magento friendly code and not queries
Using enterprise 1.11, not looking at buying a plugin at this stage, any advice greatly appreciated
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); #important
$product_id = '8782';
$collection = Mage::getModel('catalog/product')
->load($product_id);
echo "Collection: " . $collection->getName();
echo "<br>";
echo "Attribute Set: " . $collection->getAttributeSetId() . " SKU: " . $collection->getSku();
echo "<br>";
echo "Entity ID: " . $collection->getEntity_id() . " Product Type (Attribute Label): " . $collection->getProducttype();
Just clarifying:
In the sample you've shown, the $collection object isn't really a "collection".
It's an instance of one 'catalog/product' object.
To modify the price of one catalog/product object, you'd be looking at doing something like this:
$product = Mage::getModel('catalog/product')->load($product_id);
$product->setPrice($product->getPrice() * 1.2)
->save();
If you want to do this over a bunch of products, you'll probably want to use a collection of
'catalog/product' objects, and apply some attribute filters (which boils down to adding
WHERE clauses to the eventually generated SQL). (Here's one summary of the magento collection query
syntax.)
I'm not sure what you mean by "entity label". Is that a custom attribute you've attached to your products?
A general example, applying this price change to all products with a certain SKU:
$product_collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('sku', array('like' => 'FOO01-%'));
foreach($product_collection as $product) {
$new_price = calcNewPrice($product->getPrice());
$product->setPrice($new_price)
->save();
}
Where, if you're going across stores for price calcs, "calcNewPrice" might look something like this:
function calcNewPrice($product) {
$other_store_product = Mage::getModel('catalog/product')->setStoreId($other_store_id)
->load($product->getId());
if ($other_store_product) {
return $other_store_product->getPrice() * 1.2;
}
// else ???
}
Hope this helps.
Related
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.
I am developing a module to receive commissions from companies that are selling in my Magento store. It is already working, but need to add a new feature that I'm not getting.
I need to add an extra value in the commission, if the application of any company is greater than "X"
I did in a way that did not work:
foreach ($order->getAllVisibleItems() as $key => $item)
{
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$price = $item->getPrice();
$valor_comissao = (int)$product->getAttributeText('comissao_vendedor')/100;
$comissao_valor2= $price-($price*$valor_comissao);
$qty = $item->getQtyOrdered();
$valor_pedido= ($price*$qty);
if($valor_pedido > 150) {
$comissao_valor= ($comissao_valor2*$qty-17);
} else {
$comissao_valor=($comissao_valor2*$qty-2);
}
$comissoes[] = array (
'Razao' => 'Produto '.$item->getName().' id '.$item->getProductId().' x '.$item->getQtyToInvoice().' sku: '. $item->getSku(),
'Login' => $product->getAttributeText('login'),
'ValorFixo' => $comissao_valor
);
}
then only if the product go from $ 150.00 the value is added and if you have two selections from costing $ 100.00, the value is not added. I need the value to be added if the entire order of the brand exceeds $ 150.00.
The logic that I'm trying to follow is as follows:
Taking the total value of the order
Filter by tags, ie total value of the order by brand
If the product is a brand that exceeded "x" and she has not yet received the additional value, add shipping
I wonder if someone has gone through something similar and could help me.
Grateful.
Taking the total value of the order
You can retrieve the total of the order via:
$order->getGrandTotal()
Filter by tags, ie total value of the order by brand
I would recommend using Magento's resource model object and building a query on it.
This post shows how this can be done: https://stackoverflow.com/a/5139391/3784145
note
Mage::getModel('catalog/product')->getCollection()
can be used instead of
Mage::getResourceModel('sales/order_collection')
Explanation on the difference between Model and Resource:
https://magento.stackexchange.com/a/4579
If the product is a brand that exceeded "x" and she has not yet received the additional value, add shipping
I don't really know what you mean with: If the product is a brand that exceeded "x"
But I think this would require another collection query.
I am am trying to configure a magento store . I have a couple of ghost products that appear on the Front end. These Ghost products have no name, no image , and aren't found under my back-end product list. However their add to button is active and works and also has a wishlist URL in the form of.
example.com/wishlist/index/add/product/4/form_key/p3jZL1nym3j4XeNl/
I think I got myself into this mess after trying to duplicate a few products. How do I trace and get rid of these ghost products. Im using absolute template.
Do you using the flat tables? If yes then try to reindex it.
P.S. check the product ID: 4 (in the backend).
If you wanna delete products with blank(null) product name & images
You can try
$products=Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('*')->load();
foreach($products as $key => $pId)
{
$product=Mage::getModel('catalog/product')->load($pId);
if($product->getName()=='' && $product->getMediaGalleryImages()=='')
{
$product = Mage::getModel('catalog/product')->load($pId)->delete();
echo "successfully deleted product with ID: ". $pId ."<br />";
}
else{ echo "Could not delete product with ID: ". $pId ."<br />"; }
}
You can also try deleting products which dont have imgs using
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()
->joinLeft(
array('_gallery_table' => $collection->getTable('catalog/product_attribute_media_gallery')),
'e.entity_id = _gallery_table.entity_id',
array()
)
->where('_gallery_table.value IS NULL');
$collection->delete();
Magento's default sitemap generation produces an XML file that displays all products, including simple products that are associated with configurable products. I only want the configurable product not the associated simple products, can anyone help me?
In Mage/Sitemap/Model/Sitemap.php the function that generates the XML sitemap is generateXml() and the code block that generates the product urls is:
$changefreq = (string)Mage::getStoreConfig('sitemap/product/changefreq', $storeId);
$priority = (string)Mage::getStoreConfig('sitemap/product/priority', $storeId);
$collection = Mage::getResourceModel('sitemap/catalog_product')->getCollection($storeId);
foreach ($collection as $item) {
$xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
htmlspecialchars($baseUrl . $item->getUrl()),
$date,
$changefreq,
$priority
);
$io->streamWrite($xml);
}
unset($collection);
I tried using
Mage::getModel('catalog/product')->getCollection();
and changed this line:
htmlspecialchars($baseUrl . $item->getUrl()),
to this line to get it work
htmlspecialchars($item->getProductUrl()),
I get the correct products (without the associated ones), but the urls are like this:
http://www.domain.com/catalog/product/view/id/532/
I want the Url Rewrites, so i changed it to:
$collection = Mage::getModel('catalog/product')->getCollection($storeId)
->addUrlRewrite();
But i still get this:
http://www.domain.com/catalog/product/view/id/532/
Any ideas what's wrong?
you can try with below code.
$product_collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('entity_id', 16)->addUrlRewrite();
echo $product_collection->getFirstItem()->getProductUrl();
or you can load url separately as you already have product collection than try something like this
Mage::getResourceSingleton('catalog/product')->getAttributeRawValue($productId, 'attribute_code', Mage::app()->getStore());
I have the following code to grab a list of Products
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name')
->addAttributeToFilter("category_ids", array('finset'=>$this->category_id));
foreach($collection as $product) {
echo $product->getName();
}
My question is, how can I NOT echo products that are 'simple' but belong to a parent 'configurable' product. (for example don't show "Red Shirt Medium" as it belongs to "Red Shirt")
I have worked out that this association lives in 'catalog_product_super_link' but I have only just started with Magento and unfortuantely don't know how to do the filtering :)
Cheers guys,
Chris.
I don't know a direct way to add this condition to the collection, I'd be interested in such a solution too. But you can always check inside the loop for each product:
if (empty(Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId())))
{
echo $product->getName();
}
I've done something similar for our google feed.
This excerpt of code is what I use to check the products inheritance:
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('*');
$products->addAttributeToFilter('status', 1);//enabled
$products->addAttributeToFilter('price', array('gt' => 0) );//price not 0
//$products->addAttributeToFilter('visibility', 4); //catalog, search - comment out to show all items (configurable products simple product breakdowns)
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
$prodIds=$products->getAllIds();
try {
foreach($prodIds as $productId) {
$product = Mage::getModel('catalog/product');
$product->load($productId);
// SIMPLE PRODUCTS
if($product->getTypeId() == 'simple' ) {
$prodName = trim($product->getName());
$parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($productId);
if(!$parentIds)
$parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($productId);
if($parentIds) {
$parentProd = Mage::getModel('catalog/product')->load($parentIds[0]);
/*
* do something if this product has a parent or do some checks against $parentProd
*/
} // end parent check
}//if SIMPLE
} // foreach
} catch(Exception $e) {
die($e->getMessage());
}
There's a function called isConfigurable in the product class.
That may be of help to you.
$product->isConfigurable();
// if its the parent object it'll be true, if its the child it'll be false.
The quickest way might be to check if the product's visibility is set to "Not Visible Individually", since simple products associated with configurable products are typically set to this. Unfortunately I don't know the precise syntax but hopefully someone else who's willing to chime in does!
Since simple products that are part of configurable products usually have a visibility value of Not Visible Individually, it probably suffices to add a visibility filter to the collection that checks for visibility of the products in the catalog:
$collection->setVisibility(Mage::getModel('catalog/product_visibility')->getVisibleInCatalogIds());
In the unlikely circumstance that the resulting products are part of a configurable product, you can use the method Mage_Catalog_Model_Product_Type_Configurable::getParentIdsByChild to check if a product is used as part of a configurable product.