Category based newsletter emails? Magento CE 1/6/2 - php

does anyone have an idea on how I could send newsletter emails to customers based on the category that they ordered from? So for instance, i would like to send a monthly email to customers who purchased exam gloves to restock their supply.

Here is one way to go about it:
1) get all of the (recent) orders
$orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('created_at', '2012-04-16 15:56:33');
Note: replace '2012-04-16 15:56:33' with correct date and timestamp.
2) Get the product's ordered
foreach($orders as $order):
// let's get some the order info
$orderData = $order->getData();
// extract the order ID from the order objecj (so we can load the whole order)
$orderId = $orderData['entity_id'];
// extract the customer's ID (so that we can find out customer's email)
$customerId = $orderData['customer_id'];
// load the customer by ID
$customer = Mage::getModel('customer/address')->load($customerId)->getData();
// get customer's email address
$customerEmail = $customer['email'];
// load the full order (so that we can get a list of product's ordered
$fullOrder = Mage::getModel('sales/order')->load($orderId);
// extract the products from the order
$products = $fullOrder->getAllItems();
endforeach;
3) Find out what category the product comes from
foreach ($products as $product):
// let's get an object with the ordered product's data
$productInfo = $product->getData();
// extract the product ID (so that we can load the product)
$prodId = $productInfo['item_id'];
// load the product
$product = Mage::getModel('catalog/product')->load($prodId);
// get all (names of) categories that this product is associated with
$categories = $product->getCategoryCollection()->addAttributeToSelect('name');
endforeach;
4) Send out a specific template to those customers (see code in first answer of this question) Sending e-mail programmatically in Magento is failing
Hope this was helpful

Related

Duplicate the categories of product

I need to copy the categories pivot for the product where id=15 and apply it to another product. Is there any shorthand to do the copy paste instead of getting the array and attach loop through?
$product = App\Product::find(15);
$product->categories()->attach([1, 5]);
$product = App\Product::find(15);
$newProduct = $product->replicate();
$newProduct->save();
and for the relations
$newProduct->categories()->attach($product->categories);
If I understood well your nee you want to attach the same categories which are attached to product with id = 15 to another product.
$product = App\Product::find(15);
After getting the concern product I can get Array of ids of categories to which that product is belongs to by calling pluck on collection of categories attached to that product
$categories_id = $product->categories()->pluck('id'); // [1,2,3,4,5]
If the second product on which I want to attach that categories has id = 5 I can perform the replication like this
$otherProduct = App\Product::find(5);
$otherProduct->categories()->sync($categories_id);

WooCommerce - get product by title?

How can you get a product by it's name? I know you can do it by ID and SKU, but for my current situation I need to grab a product by its title. I've been looking it up and can't seem to find the function.
My function occurs on the single product page, but the product I need to get data from WILL NOT be the current product that the user is looking at. Instead it will have the same name as the current product, just ending with a different symbol. (the product the user will be looking at ends with "-r" and the one I need to search for ends with "-$$$")
So far in my Functions.php:
function fill_refurbished_addon_selectbox(){
//get id of current product
global $product;
$id= $product->get_id();
//get name of product
$currentName = get_the_title($id);
//remove -r, add -$$$ and store in var
$searchFor = substr($currentName, 0, -2) . "-$$$";
echo $searchFor;
//find $$$ product by title
$coreCharge = ***GET PRODUCT BY TITLE ($searchFOr)***;
//get price of $$$ product, store in var
//populate dropbox
}
add_action('woocommerce_before_add_to_cart_button', 'fill_refurbished_addon_selectbox', 20);
The reason is, I need to fill a select box with info from the -$$$ product.
please try this
$product = get_page_by_title( 'Product Title', OBJECT, 'product' );
get_page_by_title retrieves a post given its title. If more than one post uses the same title, the post with the smallest ID will be returned.
syntax:- get_page_by_title( $page_title, $output, $post_type );

Magento: Accessing catalog and cart rule data

I'm building an extension that exports completed order information into a CSV file. I've been able to successfully access order and item information for the export but I'm having a problem accessing promotion data.
I'm looking to include the promotion data (catalog and cart rules) that are applied to that order in the export. Specifically I am trying to access the rule ids, names, descriptions, codes and applied dollar amount for a list of specific orders.
How would I access the promotion data in my extension?
If it helps, here is how I am currently accessing order/item data in my extension (qualifying by date frame):
$rows = array(); // array to hold data that will be exported to CSV
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$orders = $read->query("SELECT entity_id FROM sales_flat_order WHERE created_at >= '$from' AND created_at < '$today'");
while ($row = $orders->fetch()) {
$order = Mage::getModel('sales/order')->load($row['entity_id']);
$order_items = $read->query("SELECT item_id FROM sales_flat_order_item WHERE order_id={$order->getId()}");
while ($item_row = $order_items->fetch()) {
$item = Mage::getModel('sales/order_item')->load($item_row['item_id']);
$rows[] = array(
$order->getRealOrderId(),
$order->getCreatedAt(),
$item->getSky(),
$item->getQtyOrdered(),
..... );
); // end item loop
); // end order loop
Thanks in advance for any help you can provide,
-Mark
I believe this question has been answered on another question in the past. You can find the link to the original question and answer here Magento - get price rules from order
From that answer though..
//The order I want to check
$order_id = 859;
//Get the list of items for your order
$items = Mage::getModel('sales/order_item')
->getCollection()
->addFilter('order_id',array('eq'=>$order_id));
//loop through each item
foreach($items as $item){
//if the item has not had a rule applied to it skip it
if($item->getAppliedRuleIds() == '')continue;
/*
* I cant remember in the database they might be comma separated or space if multiple rules were applied
* the getAppliedRuleIds() function is the one you want
*/
foreach(explode(",",$item->getAppliedRuleIds()) as $ruleID){
//Load the rule object
$rule = Mage::getModel('catalogrule/rule')->load($ruleID);
// Throw out some information like the rule name what product it was applied to
echo "<p>".$item->getSku()." had rule ".$rule->getName()."(".$item->getAppliedRuleIds().") applied </p>";
}
}

How to get original (All store views) current category name in Magento?

How can I get "original" category name in Magento where I already translated category name for current store view. I would like to get category name as entered in All Store views because I would like to send original (English) name to GA for tracking - I need this when I'm on category page.
I can get localized category name in this way:
<?php
$currentCategory = Mage::registry('current_category');
$name = $currentCategory->getName();
?>
But couldn't find a way to get untranslated name without additional calls to database.
As mentioned above, this will need additional database requests. The following should work:
$defaultStoreCategory = Mage::getModel('catalog/category');
$defaultStoreCategory->setStoreId(Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
$defaultStoreCategory->load($currentCategory->getId());
$name = $defaultStoreCategory->getName();
Try with this code
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel('catalog/category')
->setStoreId(Mage::app()->getStore()->getId())
->load(YOUR_CATEGORY_ID);
Hope this helps.

How to retrieve bought items information inside an order

I want to send a weekly reminder email to customers who have bought something but not paid yet.
Using this script I can show an order:
<?php
//from http://www.exploremagento.com/magento/run-magento-code-outside-of-magento.php
require_once '../app/Mage.php';
umask(0);
Mage::app('default');
$orders = Mage::getModel('sales/order')->getCollection()
->setOrder('increment_id','DESC')
->setPageSize(1)
->setCurPage(1);
$LastOrderNr = $orders->getFirstItem()->getIncrementId() - 100000000;
$LastOrderNr=494;
$order = Mage::getModel('sales/order')->load($LastOrderNr); //put a valid order entity_id there;
print_r($order->debug());
echo "<br/>******************************<br/>";
?>
I can retrieve several useful info-blocks (i.e. customer's email address, total amount) but it won't show the items they have bought. How/where can I retrieve that kind of information?
In Magento ordered products called 'order items'. You can get order items data like this:
`
// 1. get order. you can load it by order entity id (f.e. 494)
$order = Mage::getModel('sales/order')->load($orderId);
// or by order increment id (100000494)
$order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);
// 2. get order items collection
foreach ($order->getItemsCollection() as $item) {
Zend_Debug::dump($item->getData());
}
`
it can be much easier to get the last Order , especialy needed for the success Site
$order = Mage::getModel('sales/order')->load(Mage::getModel('sales/order')->getCollection()->getLastItem()->getEntityId());
Example: Last Order Costs
$grand_total = Mage::getModel('sales/order')->load(Mage::getModel('sales/order')->getCollection()->getLastItem()->getEntityId())->getGrandTotal();

Categories