Use Tierprices of other customergroup - magento

i'm trying to get my pricing extension working but i'm stucking at the tierprices.
If a visitor comes to my site i added a paramter to the cookie.
Example: http://www.foo.com/category/product.html?sidx=5
Now the visitor enters my site with the sidx parameter. Now the Observer: catalog_product_get_final_price sets the finalprice with the first tierprice of this Product of Customergroup 5.
The Customergroup of the visitor is still NOT LOGGED IN. Now i need the other tierprices of the Customergroup ID 5 without registering.
I have googled a lot to find a solution for that but i don't get the point.
Best Regards
boti

If you want to get the Tier prices for any product:
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID_HERE);
$tierPrices = $product->getData('tier_price');
if (!is_array($tierPrices)) {
$tierPrices = array();
}
$result = array();
foreach ($tierPrices as $tierPrice) {
$row = array();
$row['customer_group_id'] = (empty($tierPrice['all_groups']) ? $tierPrice['cust_group'] : 'all' );
$row['website'] = ($tierPrice['website_id'] ? Mage::app()->getWebsite($tierPrice['website_id'])->getCode() : 'all');
$row['qty'] = $tierPrice['price_qty'];
$row['price'] = $tierPrice['price'];
$result[] = $row;
}
// $result now has an array of all tier prices...
// looking for cust_group = 5 would be trivial:
foreach ($tierPrices as $tierPrice) {
if($tierPrice['cust_group'] == 5) {
return $tierPrice; // this is what you want..
}
}

Related

How to check the value on the array cart

I have an array cart on codeigniter and creates a function to separate if there is engrave or not on the array like this,
$cart_count = 0;
$cart_cek = $this->cart->contents();
foreach ($cart_cek as $value2) {
if($cart_count >= 1){
break;
}
else{
if($value2['engrave-text'] != ""){
$order_id = $this->generate_order_id_costume();
$cart_count++;
}
else{
$order_id = $this->generate_order_id();
$cart_count++;
}
}
}
but when I run the function it always goes into else with value
$order_id = $this->generate_order_id();
even though there is engrave in the cart array.
For example, I have 3 items in the cart that each have the engrave field. How do I know that there are at least 1 engrave field filled in all the carts?
If you want to check that if the cart contains at least one engrave-text field is filled, you could use array_column to get the engrave-text arrays, then array_filter to check if it's empty :
$cart_cek = $this->cart->contents();
if (!empty(array_filter(array_column($cart_cek, 'engrave-text')))) { // if at least one 'engrave-text' has a value, call generate_order_id_costume() method
$order_id = $this->generate_order_id_costume();
} else { // otherwise call generate_order_id() method
$order_id = $this->generate_order_id();
}
First of all, you don't need to check the count to stop the loop and try using isset().
Try this if you want to check if $this->cart->contents(); has at least 1 engrave-text object.
$cart = $this->cart->contents();
foreach ($cart as $cart_value) {
if(isset($cart_value['engrave-text'])){
$order_id = $this->generate_order_id_costume();
} else {
$order_id = $this->generate_order_id();
}
}

Magento Multiple Wishlist creation programmatically

I know how to add products to a customer's wishlist programmatically however it only adds to one wishlist. I have the multiple wishlist option set to enabled however I do not know how to create a new wishlist instead of merging products into the existing wishlist.
public function submitQuote(Mage_Adminhtml_Model_Session_Quote $quote)
{
$currentQuote = $quote->getQuote();
$customer = $currentQuote->getCustomer();
$items = $currentQuote->getAllVisibleItems();
//$wishlist = Mage::helper('wishlist')->getWishlist();
//Mage::register('wishlist', $wishlist);
$wishlist = Mage::getModel('wishlist/wishlist');
$curretDate = date('m/d/Y', time());
$wishlist->setCustomerId($customer->getId());
$wishlist->setName('Quote ' . $curretDate)
->setVisibility(false)
->generateSharingCode()
->save();
foreach ($items as $item)
{
$productId = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
$buyRequest = $item->getBuyRequest();
$result = $wishlist->addNewItem($product, $buyRequest);
if(is_string($result))
{
Mage::throwException($result);
}
$wishlist->save();
}
//Mage::unregister('wishlist');
}
I've had this problem before. To add a product to a customer's wishlist you need to start a wishlist model and call the addNewItem method passing the product object. Here is how I do it.
$customer = Mage::getModel('customer/customer');
$wishlist = Mage::getModel('wishlist/wishlist');
$product = Mage::getModel('catalog/product');
$customer_id = 1;
$product_id = 1;
$customer->load($customer_id);
$wishlist->loadByCustomer($customer_id);
$wishlist->addNewItem($product->load($product_id));
Hope this helps!
EDITED:
$customerid= "YOUR CUSTOMER ID "; // Modify This
$customer=Mage::getModel("customer/customer")->load($customerid);
$wishlist=Mage::getModel("wishlist/wishlist")->loadByCustomer($customer,true);
Check for reference this: app/code/core/Mage/Wishlist/Model/Wishlist.php
LATEST EDIT:
I see that your problem is here:
$result = $wishlist->addNewItem($product, $buyRequest);
FINAL EDIT:
Then create another function and add the items and wishlist you want to add it to (as parameters). This should add items to the wishlist that you have passed as an argument when calling the function. Its the only solution I can come up with. Hope this helps!
I just stumbled across this question because I'm trying to do something similar - your code part helped me to create a new wishlist, and I've managed to do the rest so thought I'd share.
This code will;
Create a new wishlist with the name 'Quote dd/mm/YY h:m:s'
Load the wishlist collection for the current user, ordered by highest ID first
Load the newest wishlist
Add all basket items to it
Redirect the user to the new wishlist view.
public function addToQuoteAction() {
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$customerData = Mage::getSingleton('customer/session')->getCustomer();
$customerId = $customerData->getId();
} else {
Mage::getSingleton('core/session')->addError($this->__('Please login to use this feature'));
return $this->_redirectUrl($this->_getRefererUrl());
}
// Create the wishlist
$newWishlist = Mage::getModel('wishlist/wishlist');
$curretDate = date('d/m/Y h:m:s', time());
$newWishlist->setCustomerId($customerId);
$newWishlist->setName('Quote ' . $curretDate)
->setVisibility(false)
->generateSharingCode()
->save();
// Find the newly create list and load it
$wishlistCollection = Mage::getModel('wishlist/wishlist')->getCollection()
->addFieldToFilter('customer_id',$customerId)->setOrder('wishlist_id');;
$firstWish = $wishlistCollection->getFirstItem();
$wishlist = Mage::getModel('wishlist/wishlist')->load($firstWish->getWishlistId());
$cart = Mage::getModel('checkout/cart')->getQuote();
//getAllItems
foreach ($cart->getAllVisibleItems() as $item) {
$productId = $item->getProductId();
$buyRequest = $item->getBuyRequest();
$result = $wishlist->addNewItem($productId, $buyRequest);
$wishlist->save();
}
Mage::getSingleton('core/session')->addSuccess($this->__('All items have been moved to your quote'));
return $this->_redirectUrl(Mage::getBaseUrl().'wishlist/index/index/wishlist_id/'.$firstWish->getWishlistId().'/');
}

MAGENTO - Load last created Product to Cart

is it possible to load the last registered (created) Product in my Cart?
How?
I know it sounds crazy but i need this for one of my project.
I think this is the part where the Product gets loaded:
cartcontroller.php
/**
* Initialize product instance from request data
*
* #return Mage_Catalog_Model_Product || false
*/
protected function _initProduct()
{
$productId = (int) $this->getRequest()->getParam('product');
if ($productId) {
$product = Mage::getSingelton('checkout/session')->getQuote()->getAllItems()
->setStoreId(Mage::app()->getStore()->getId())
->load($productId);
if ($product->getId()) {
return $product;
}
}
return false;
}
This (if I´m right) i need to be replaced with the last in shop created Product - sound weired but i need this....
You're almost there - try this code:
$collection = Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection();
$collection->getSelect()->order('created_at DESC');
$latestItem = $collection->getLastItem();
Note that when you get the latest quote item, you're not actually obtaining the product. To get the actual product, you would need to add this line:
$product = $latestItem->getProduct();
You can get the items in the cart like this:
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
Then loop through the items and see which one has the biggest id.
$max = 0;
$lastItem = null;
foreach ($items as $item){
if ($item->getId() > $max) {
$max = $item->getId();
$lastItem = $item;
}
}
if ($lastItem){
//do something with $lastItem
}

How to get bundle product price in Magento?

I have an bundle product as array like this: (take from params when add product to cart)
Array
(
[product] => 165
[bundle_option] => Array
(
[17] => 47
[22] => 60
[16] => 46
[15] => 42
[14] => Array
(
[0] => 39
)
)
)
How could I get price for this bundle product?
Something like this should also work:
public function getDisplayPrice($product) {
if($product->getFinalPrice()) {
return $product->getFormatedPrice();
} else if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
$optionCol= $product->getTypeInstance(true)
->getOptionsCollection($product);
$selectionCol= $product->getTypeInstance(true)
->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product),
$product
);
$optionCol->appendSelections($selectionCol);
$price = $product->getPrice();
foreach ($optionCol as $option) {
if($option->required) {
$selections = $option->getSelections();
$minPrice = min(array_map(function ($s) {
return $s->price;
}, $selections));
if($product->getSpecialPrice() > 0) {
$minPrice *= $product->getSpecialPrice()/100;
}
$price += round($minPrice,2);
}
}
return Mage::app()->getStore()->formatPrice($price);
} else {
return "";
}
}
You can use the built-in static method calculatePrice of Mage_Bundle_Model_Product_Price like so:
if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE){
$pricemodel = Mage::getModel('bundle/product_price');
$price = $pricemodel::calculatePrice(
$product->getData('price'),
$product->getSpecialPrice(),
$product->getSpecialPriceFrom(),
$product->getSpecialPriceTo(),
false,
Mage::app()->getStore()->getWebsite()->getId(),
Mage::getSingleton('customer/session')->getCustomer()->getGroupId(),
$product->getId()
);
$finalprice = $this->helper('core')->currencyByStore(Mage::helper('tax')->getPrice($product, $price));
}
You can use the getMinimalPrice() and getMaximalPrice() functions in the Mage_Bundle_Product_Price class, which are written specifically for bundle products.
// load product
$product = new Mage_Catalog_Model_Product();
$product->load(165);
$priceModel = $product->getPriceModel();
// get options
$block = Mage::getSingleton('core/layout')->createBlock('bundle/catalog_product_view_type_bundle');
$options = $block->setProduct($product)->getOptions();
$price = 0;
foreach ($options as $option) {
$selection = $option->getDefaultSelection();
if ($selection === null) {
continue;
}
$price += $priceModel->getSelectionPreFinalPrice($product, $selection, $selection->getSelectionQty());
}
or, you can use my magento module: https://github.com/head82/KH_ExtendedBundlePrice tested with magento 1.7
#Kevin, great work. I personally needed a slight modification. In Magento 1.7.0.2, the function getSelectionPreFinalPrice() actually calls upon getSelectionPrice() which calls upon getSelectionFinalTotalPrice() - but in that last part the price is calculated with the final price (so including tier-pricing and special prices) and not the original price.
I applied the following snippet to get that original price (without tier-pricing and without special prices):
$_normalPrice = 0;
$_options = $_priceModel->getOptions($_product);
foreach($_options as $_option) {
$_selection = $_option->getDefaultSelection();
if ($_selection === null) continue;
$_normalPrice = $_normalPrice + $_selection->getPrice();
}
Here is the magento way:
$_product = $this->getProduct();
$_priceModel = $_product->getPriceModel();
list($_minimalPriceTax, $_maximalPriceTax) = $_priceModel->getTotalPrices($_product, null, null, false);
list($_minimalPriceInclTax, $_maximalPriceInclTax) = $_priceModel->getTotalPrices($_product, null, true, false);
Assuming that $this->getProduct() returns Catalog/Product Model.
Works both with fixed and dynamic price types even compatible with Configurable Bundle extention. Taken from design/base/default/template/bundle/catalog/product/price.phtml
I solved it by own method, not sure its acceptable or not.send the post values and product id, the model will return the price.
$bundle_option = Mage::app ()->getRequest ()->getParam('bundle_option');
$bundle_option_array = call_user_func_array('array_merge', $bundle_option);
$price = Mage::Helper('airhotels/bundle')->getBundlePrice($productid,$bundle_option_array);
my helper file is
public function getBundlePrice($productId,$bundle_option_array) {
$product = new Mage_Catalog_Model_Product();
$product->load($productId);
$price=0;
$selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection($product->getTypeInstance(true)->getOptionsIds($product), $product);
foreach($selectionCollection as $option)
{
if (in_array($option->getSelectionId(), $bundle_option_array)){
$price += $option->price;
}
}
return $price;
}
Concept: I built a 1-D array from the 2-D array(the question). from the function in helper we can get all the selection id of a bundle product . By matching (using in_array) we can calculate the price for our custom selected products.
Although I'm sure you have figured out what you needed several year ago I'm not quite sure where in the accepted answer you would put your params.
What I found was that you can get the price for a bundle product with getFinalPrice() just like any other product, but you can set the selected options using the catalog/product helper.
$_product = Mage::getModel('catalog/product')->load($this->getRequest()->getPost()['product'];
$productHelper = $this->helper('catalog/product');
//getpost() contains the array you mentioned when you click add to cart
$_configuredProducts = $_product->getTypeInstance(true)->processConfiguration(new Varien_Object($this->getRequest()->getPost()), $_product,Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_FULL );
echo $_product->getFinalPrice();

Magento - How can I add rating information to a review

I'm manually creating reviews in Magento and I'm trying to find out how I add the rating information in? I can add the reviews no problem but I'm struggling with the rating values (star values).
I have an array that looks like this:
array("Price"=>80, "Value"=>60, "Quality"=>60);
How can I add that to the star system and the Summary Rating?
Thanks.
Ok, so this is what I have so far:
This adds a review:
$review->setEntityPkValue(23);//product id
$review->setStatusId(1);
$review->setTitle("title");
$review->setDetail("detail");
$review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE));
$review->setStoreId(Mage::app()->getStore()->getId());
$review->setStatusId(1); //approved
$review->setNickname("Me");
$review->setReviewId($review->getId());
$review->setStores(array(Mage::app()->getStore()->getId()));
$review->save();
$review->aggregate();
This adds a rating for a review <-I'm stuck here!
// this is some test code to add the rating review
$rating[0]['Price'] = 80;
$rating[0]['Value'] = 100;
$rating[0]['Quality'] = 80;
$product_id = 23;
$review_id = 631;
foreach ($rating as $ratingId => $optionId) {
// This is the bit where it all seems to go wrong!:
Mage::getModel('rating/rating')
->setRatingId(1)
->setReviewId($review_id)
->addOptionVote($val, $product_id);
}
Thanks!
This worked for me:
public function addReview($ratingarray)
{
$product_id = $ratingarray['product_id'];
$storeid = $ratingarray['store_id'];
$title = $ratingarray['title'];
$customerid = $ratingarray['customer_id'];
$nickname = $ratingarray['nickname'];
$detail = $ratingarray['detail'];
$review = Mage::getModel('review/review');
$review->setEntityPkValue($product_id);
$review->setStatusId(1);
$review->setTitle($title);
$review->setDetail($detail );
$review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE));
$review->setStoreId($storeid);
$review->setStatusId(1); //approved
$review->setCustomerId($customerid);
$review->setNickname($nickname);
$review->setReviewId($review->getId());
$review->setStores(array($storeid));
$review->save();
$review->aggregate();
//return "success";
$rating_options = $ratingarray['options'];
/*array(
array(1,2,3,4),
array(6,7,8),
array(11,12)
);*/
$row = count($rating_options);
$rating_id = 1;
foreach($rating_options as $key1=>$val1)
{
foreach($val1 as $key2=>$val2)
{
$_rating = Mage::getModel('rating/rating')
->setRatingId($key1)
->setReviewId($review->getId())
->addOptionVote($val2,$product_id );
}
}
return "Success";
}
I am calling this like =>
$options = array(1=>array(1,2,3,4),2=>array(6,7,8),3=>array(11,12));
$reviewarray = array('customer_id'=>'21','product_id'=>'176','store_id'=>'4','title'=>'Review','nickname'=>'XYZ','detail'=>'Nice Product with Life time warrenty', 'options'=>$options);

Resources