Adding Different Product Names for Different Websites programmatically - php

I have two websites for specific Store Views - English & German. Normally, I could have maintained 1 Website with two different Store Views, but it was the specific requirement of my client, to have each website for each specific store view.
Problem is I am not able to update / create different product names / descriptions, each for a product website, programmatically. I'm using this code to do it, which I found to be the same for different price:-
$combinationWebsiteWithName = array('1' => 'product name 1', '2' => 'product name 2');
foreach ($combinationWebsiteWithName as $_eachWebsiteId => $_eachProductName) {
$objWebsite = Mage::getModel('core/website')->load($_eachWebsiteId);
$storeIds = $objWebsite->getStoreIds();
$objProduct = Mage::getModel('catalog/product')
->setStoreId(end($storeIds))
->load($productId);
$objProduct->setName($_eachProductName);
$objProduct->save();
}
Can anybody please help me & find any errors in the above code?
Thanks in advance.

Eventually, I found out what was wrong in there, and so here is the answer:-
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
foreach ($websiteWiseProductNameArray as $_eachWebsiteId => $_eachProductName) {
$objWebsite = Mage::getModel('core/website')->load($_eachWebsiteId);
$storeIds = $objWebsite->getStoreIds();
foreach ($storeIds as $_eachStoreId) {
$objProduct = Mage::getModel('catalog/product')
->setStoreId($_eachStoreId)
->load($productId);
$objProduct->setData($targetAttrCode, $_eachProductName);
$objProduct->save();
unset($objProduct);
}
unset($storeIds, $objWebsite);
}
Last unexpected area of modification for me was setting the store ID to be that of Admin area, by using the following code: "Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));"

Related

PHP function returning different data on each call on separate php pages

I am developing a website that has a cart. basically the customer will login and choose what they want and then add it to the cart. this works perfectly. the problem comes when displaying the cart. when i add a product to my cart it adds it successfully (can see it in phpmyadmin), but when view my cart it does not display the product i added. i wait a few minutes it then shows up. i am struggling to find out why it has such a long delay to display.
Here is my php function that i call:
function CartGetAll($cid)
{
$query = "SELECT `cart`.`cart_id`, `cart`.`cart_product`, `cart`.`cart_qty`, `product`.`product_name`, `product`.`product_price`, `product`.`product_image` FROM `product` INNER JOIN `cart` ON `product`.`product_id` = `cart`.`cart_product` WHERE `cart`.`cart_customer` = $cid ORDER BY `product`.`product_name`";
$result = mysqli_query($this->link, $query);
$items = array();
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$items[] = array('cart_id' => $row['cart_id'], 'cart_product' => $row['cart_product'], 'cart_qty' => $row['cart_qty'], 'product_name' => $row['product_name'], 'product_price' => $row['product_price'], 'product_image' => $row['product_image']);
}
return $items;
}
EDIT 1:
here is my code where i call the method:
require './connection.php';
$cart = $con->CartGetAll($cid);
the $con is the object in the connection class that i use to call all my database functions. $cid is the customer id i pass as a parameter.
EDIT 2:
I forgot to mention that this only happens on the hosted website. When i was developing and testing my website on a local pc with wamp server & netbeans, it never had this issue. only on my online site i get this issue.
Your help would be greatly appreciated.

Selecting and using JSON array values

I'm currently working on a shopping application but i stumbled upon a problem. The problem is that i can't select the JSON values from my "shopping cart". See the following code and description.
So by using add to cart buttons and such i'm creating the shopping cart. The shopping cart is actually a JSON object. An example of the cart:
{"cartItems":{"2":{"id":"2","name":"Blouse","price":26.99,"size":"M","quantity":"3","total_product_price":80.97},"5":{"id":"5","name":"Bedrukte Zomerjurk","price":30.5,"size":"L","quantity":"4","total_product_price":122}},"totalPrice":202.97,"totalItems":2,"customerSelect":{"id":"1","firstname":"John","lastname":"TestStefans"}}
As you can see the design of my JSON cart is:
cart:{"cartItems":{"id":{ product information }}}
The problem now is trying to select the values like the "name" and "price". This due to the "id"{ segment. But i need that piece for removing one item by id from the cart.
So my question is:
How would i be able to select all the product information and create an foreach for placing the information in the database / email template. I've been trying this but this only gave me the first product:
$cart_encode = json_encode($_SESSION['cart']['cartItems']);
$cartDecode = json_decode($cart_encode);
// Adding the product items
foreach ($cartDecode as $key => $cartItem) {
$resource->associations->cart_rows->cart_row->id_product = $cartItem->{"id"};
$resource->associations->cart_rows->cart_row->id_product_attribute = 1;
$resource->associations->cart_rows->cart_row->id_address_delivery = 1;
$resource->associations->cart_rows->cart_row->quantity = $cartItem->{"quantity"};
}
Take note that i'm using XML for database input. For the email template i've tried:
$testName = $_SESSION['cart']['cartItems']['name'];
$testPrice = $_SESSION['cart']['cartItems']['price'];
$testQuantity = $_SESSION['cart']['cartItems']['quantity'];
$testTotal = $_SESSION['cart']['cartItems']['total_product_price'];
$testProduct = array(
"Name:" => $testName,
"Price:" => $testPrice,
"Quantity" => $testQuantity,
"Total" => $testTotal
);
Iknow that the id number is missing but i cant dynamicly avoid that layer.
I hope that my question is clear
As always. Thanks in advance!

How to add Rating to sort list in Magento 1.7

Looking for some help adding sort by Rating in Magento. I have added code snippets to toolbar.php which seem to add the sort by Rating but when trying to select it, it gets stuck until I reload the page. Any help would be greatly appreciated. Code can be found below: This is the Toolbar.php file.
// Begin new Code
$this->getCollection()->joinField('rating',
'review/review_aggregate',
'rating_summary',
'entity_pk_value=entity_id',
'{{table}}.store_id=1',
'left');
// End new Code
AND
// Add rating to "Sort by"
$_availableOrder = $this->_availableOrder;
$_availableOrder['rating'] = 'Rating';
return $_availableOrder;
$this->_availableOrder = array(
‘rating_summary’ => Mage::helper(’catalog’)->__(’Rating’),
‘price’ => Mage::helper(’catalog’)->__(’Price’),
‘newest’ => Mage::helper(’catalog’)->__(’Newest’),
‘name’ => Mage::helper(’catalog’)->__(’Name’)
);
Best is to make this in a module but here you go:
First we shall alter the way products are retrieved from the database, to include the overall rating (shown as the number of stars on the product) along with the rest of the product attributes. Copy the file app/code/core/Mage/Catalog/Block/Product/List.php to app/code/local/Mage/Catalog/Block/Product/List.php and open it for editing.
In the new List.php file find the following line (around line 86):
$this->_productCollection = $layer->getProductCollection();
After this add the following:
$this->_productCollection->joinField('rating_summary', 'review_entity_summary', 'rating_summary', 'entity_pk_value=entity_id', array('entity_type'=>1, 'store_id'=> Mage::app()->getStore()->getId()), 'left');
Now we need to add in an option so that the customer can select "Rating" as an attribute to sort by. Copy the file app/code/core/Mage/Catalog/Model/Config.php to app/code/local/Mage/Catalog/Model/Config.php and edit.
In the new Config.php file find the following code (which should start around line 298):
$options = array(
'position' => Mage::helper('catalog')->__('Position')
);
Replace with code with:
$options = array(
'position' => Mage::helper('catalog')->__('Position'),
'rating_summary' => Mage::helper('catalog')->__('Rating')
);
Now when viewing categories on your website you should have an option of "Rating" in addition to the others. Note that the sort order defaults to ascending so the lowest rated products will be displayed first. The sort order can be changed by the customer by clicking the arrow next to the drop-down box. Aside from this caveat the new sort is fairly easy to implement and extends the usefulness of the ratings.
Credits: https://www.fontis.com.au/blog/sort-products-rating

How do i show all product info in opencart module?

I'm new to opencart and i created a module like latest module in opencart but the difference is the only latest module show latest product and my module is for all product and showing product in random manner. this my module is working fine..but there is a problem occurs for show the actual rating for each product..Now i want to add rating system in my module.
So i want to know that whta should be the right query of mysql to get all product info like name,description,reviews,ratings,product_id etc..
you haven't shown your approach so its just general pointers
first you load product.php model in your controller file $this->load->model('catalog/product');
then you decide what would be your filters when calling getProducts if you don't give any filters it will return all products in database, lets say you decide no filters then just pass an empty array of data (or don't pass anything at all )
$data = array();
then you call the function
$results = $this->model_catalog_product->getProducts($data); //or without $data
now you can do something like
foreach ($results as $result) {
$this->data['products'][] = array(
'product_id' => $result['product_id'],
'name' => $result['name'],
'rating' => $result['rating'],
);
now you can use that in your tpl file like
<?php foreach ($products as $product) { ?>
some code here
<?php echo $product['rating']; ?>
<?php } ?>
this is the generic way you should use, and its easy too, if you know how that getProducts function takes data out from database and return result see getProducts function in catalog/model/catalog/product.php

Drupal/Ubercart custom price php code for roles

Im building an e-commerce site for wholesale foods and the pricing for products change depending on the user logged in. Ive looked at member pricing and basically every module i could find to do with altering the price but they are either for drupal 6 or not really what im after. Im using Drupal 7 with ubercart 3.
Ive found this module http://drupal.org/project/uc_custom_price. It adds a field within product creation that allows custom php code to be added to each individual product which is exactly what im after. however im not that good with php which is why ive been hunting modules instead of changing code.
What ive got at the moment is:
if ([roles] == 'test company') {
$item->price = $item->price*0.8;
}
Except the [roles] part is the wrong thing to use there and it just throws errors. Ive tried using things like $users->uid =='1' to try to hook onto a user like that but that didnt work either.
what would be the correct variable to put there?
thanks
try this Drupal 7 global $user object
global $user; // access the global user object
if(in_array("administrator",$user->roles)){ // if its administrator
$item->price = $item->price*0.8;
}elseif(in_array("vip",$user->roles)){ // if its a vip
//..
}elseif(in_array("UserCompanyX",$user->roles)){ // if its a user from company X
//..
}
or
if($user->roles[OFFSET] == "ROLE"){
// price calculation
}
$user->roles is an array of the roles assigned to the user.
hope it helped
Make your own module with UC Price API:
http://www.ubercart.org/docs/developer/11375/price_api
function example_uc_price_handler() {
return array(
'alter' => array(
'title' => t('Reseller price handler'),
'description' => t('Handles price markups by customer roles.'),
'callback' => 'example_price_alterer',
),
);
}
function example_price_alterer(&$price_info, $context, $options = array()){
global $user;
if (in_array("reseller", $user->roles)) { //Apply 30% reseller discount
$price_info["price"] = $context["subject"]["node"]->sell_price - (
$context["subject"]["node"]->sell_price * 0.30) ;
}
return;
}
See also: http://www.ubercart.org/forum/development/14381/price_alteration_hook

Categories