Drupal/Ubercart custom price php code for roles - php

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

Related

WooCommerce edit order privilledge

Hello i made a new role using Ultimate Member named stock_user
i want all users in the stock_user role only thing they can do
is to change the order status in wooCommerce admin page from processing to shipped only
i tried plugins but nothing worked im sorry i don't have any code but i don't know where to start
can anyone tell me where to start to make this work ?
i want to limit this dropdown menu options to processing & shipped only when a user with privildge stock_user is logged in
EDIT 2: Its now working as it should thank to IndexTwo Answer but the problem is that inventory role is a custom role made by Ultimate Member plugin how can i make the status work with it ? here is the code of Indextwo
function vnm_remove_woo_statuses($statuses) {
$currentUser = wp_get_current_user();
$removeStatusesArray = array('pending', 'completed', 'cancelled', 'refunded', 'failed', 'refund-issued', 'shipping-progress', 'on-hold');
// If it's inventory user role
if ( in_array('inventory', $currentUser->roles)) {
foreach ($removeStatusesArray as $status) {
if (isset($statuses['wc-' . $status])) {
unset($statuses['wc-' . $status]);
}
}
}
return $statuses;
}
add_filter('wc_order_statuses', 'vnm_remove_woo_statuses');
i mean it works when i change inventory to administrator just fine but how to get it working with the custom role (inventory) ? thank you
There are a couple of ways to approach the logic on this, but here's the function first:
function vnm_remove_woo_statuses($statuses) {
$currentUser = wp_get_current_user();
$removeStatusesArray = array('pending', 'completed', 'cancelled', 'refunded', 'failed', 'refund-issued', 'shipping-progress', 'on-hold');
// If it's any role other than administrator, remove the status
if (!in_array('administrator', $currentUser->roles)) {
foreach ($removeStatusesArray as $status) {
if (isset($statuses['wc-' . $status])) {
unset($statuses['wc-' . $status]);
}
}
}
// ALWAYS need to return the status array regardless
return $statuses;
}
add_filter('wc_order_statuses', 'vnm_remove_woo_statuses', 5, 1);
Note the last line of the function: you always need to return the value of a filter, regardless of the logic therein - otherwise things go wonky. I've also set a high priority of 5 just in case anything else was hooking in.
The conditional logic is currently set to remove statuses if ANY role other than administrator tried to edit an order:
if (!in_array('administrator', $currentUser->roles)) { /* Do stuff */ }
In your example, you were removing the statuses only for administrators. You could switch that around so that it only happens for stock_user users:
if (in_array('stock_user', $currentUser->roles)) { /* Do stuff */ }
...however, be aware that if you have the ability to add multiple roles to a user and add Stock User to an administrator, their access to ordser statuses would also be restricted here.

specific php session variables lost in Chrome only,

I lose some session variables in Chrome only
After reading every similar question and answer on this site, I have to note:
It is not a 404 / missing favicon issue
Also not a redirect issue. The session is started and closed properly.
I have a $_SESSION['customer'] and $_SESSION['order'] variable, for instance.
As expected, $_SESSION['customer'] holds an object of customer data, $_SESSION['order'] holds an array of products.
When I add a product to the order, I need to look up any customer discounts for this specific product. Then it turns out in Chrome, the $_SESSION['customer'] variable is missing, and the sql query returns no discounts, since there is no customer number.
Let's give you some code to work with:
I am on the update order page, where I still have my customer and order variable.
I call this function with an ajax request to return a list of products:
public function live_search_product(){
$this->load->model('orders', 'orders');
$qry = $_GET['qry'];
if (strlen($qry)>0) {
$products = $this->orders_model->search_products($qry, false);
if ($products){
foreach ($products as $product){
echo ''.$product['short_title'] . '<br>';
}
}
}
}
I select the product from the dropdown list, and end up in this function, where I suddenly have lost my $_SESSION['customer'] object and $_SESSION['order'] array. Please note that all my other session variables still exist.
public function add_product() {
$this->load->model('orders', 'orders');
$product = $this->orders_model->get_product_by_id($this->router->id);
$discount = $this->discount->get_discount($product->id, $product->product_group_id, $_SESSION['customer']->id, $_SESSION['customer']->group_id );
$_SESSION['order'][$this->router->id] = array('product' => $product ,
'quantity' => 1,
'rental' => 0,
'bruto' => $product->price_ex,
'discount' => $discount->discount_percentage,
'netto' => ((100 - $discount->discount_percentage)/100) * $product->price_ex,
'total' => ((100 - $discount->discount_percentage)/100) * $product->price_ex);
$this->calculate_order_totals();
}
In firefox and iexplorer and edge there is no problem, it works.
I really hope you have any other suggestions.
**** EDIT ****
Every controller has an index function that is called automatically. route orders/orders will call orders/orders/index, and for instance route orders/orders/add_product, will call the function add_product in the orders controller (residing in the orders folder).
It turns out that in Chrome, somehow I get routed to the index function of this orders controller. Which is an overview orders function where I reset all order variables, to make sure nothing is left when I open a new order.
I have changed that, so this specific situation is solved.
However, can not understand why I get redirected to this function, when calling the add_product function directly.
Any ideas?

Adding custom option to order line

I'm currently trying to add a custom option to a specific orderline on add to cart via the following:
public function addToPackageQuote()
{
$cart = Mage::getSingleton("checkout/cart");
$quote = Mage::getSingleton("checkout/session")->getQuote();
$packageId = Mage::getModel('MyTuxedo_OPP/Package')->checkPackageId();
$products = $this->sortArray();
foreach ($products as $productInfo) {
try {
$split = explode(",", $productInfo);
$_product = Mage::getModel('catalog/product')->load($split[0]);
if($_product->isConfigurable()) {
$simpleId = $this->getConfigurableSimple($split[1],$split[3],$split[0]);
} else {
$simpleId = $split[0];
}
$product = Mage::getModel('catalog/product')->load($simpleId);
$options = new Varien_Object(array(
"qty" => 1,
"custom_options" => array(
"package" => $packageId,
"packageName" => Mage::helper('MyTuxedo_OPP')->getPackageName()
)
));
$quote->addProduct($product, $options);
$this->_getSession()->setCartWasUpdated(true);
$quote->save();
} catch (Exception $e) {
echo $e->getMessage();
}
$this->addFreeItems();
}
$cart->save();
unset($_SESSION['products']);
unset($_SESSION['productId']);
$cart->save();
// Let's unset all the package sessions (apart from a few that are needed!).
$this->kill();
}
This method is completely seperate from the generic add to cart handler, and is used purely in a packages system so that it adds simple products exclusively (also breaks down configurables super attribute to find the simple product too).
These simple products have no custom options attached to them in the Magento backend, nor is it a goal to add custom options to the product itself. What I would like to do is attach custom options to the order-line that is then transferred over to the order if a purchase is made. So effectively data that is added at the add to cart method and no where else!
The add to cart method works as expected it's just not including the custom options I am trying to attach. I have also tried defining the options object as simply:
$options = new Varien_Object(array(
"qty" => 1,
"package" => $packageId,
"packageName" => Mage::helper('MyTuxedo_OPP')->getPackageName()
)
The above info, not including qty is not in the orderline object at all, and I can't seem to work out where to move on from here.
Endlessly googling at the moment so some help would be most appreciated!!
I do appreciate I’m instantiating the product model object twice in this, however the plan is to just get it working then optimise! :)
You have to set the custom options for the product before adding it to cart.
$product->setCustomOptions($options);
The in Mage_Sales_Model_Quote::_addCatalogProduct() the custom options will be added to the cart item.
See also here: http://www.magentocommerce.com/boards/viewthread/49659/
By the way: Your code may be pretty slow because you are loading products twice in a foreach loop. You should consider some refactoring by using the product collection instead. Also it looks kind of hackish to directly access the $_SESSION variable here. You could rather use the Checkout Session for that (Mage::getSingleton('checkout/session')).
I have now resolved this, after much headache. You can add a custom option to the cart and not have to instantiate the product object and save a custom option to do this, it can be done via tacking onto an observer, and pulling the quote items.
After tacking onto: sales_quote_add_item
I then used:
public function addCustomData($observer) {
$event = $observer->getEvent();
$quote_item = $event->getQuoteItem();
$quote = $session->getQuote();
$quote_item->addOption(array("product_id" => $quote_item->getProduct()->getId(),
"product" => $quote_item->getProduct(),
"code" => 'PackageId',
"value" => Mage::getModel('MyTuxedo_OPP/Package')->checkPackageId()
));
$quote->save();
}
It is most important to include the product object and id, as the function doesn't use the loaded object for some reason.
You can then get at the object via:
$_item->getOptionByCode('PackageId')->getValue();
Quick piece of handy info, if it dumps a stack trace in front of you it can't find the defined option, lose the getValue() (if using var_dump) function to see if you are getting a null value, otherwise xdebug will give you a ton of hints to get around it.

Magento hidden custom product attributes

I am currently making a module so that users see on the checkout page something like: "People who purchased product X also purchased Y, Z and T".
I made a cronjob to calculate which are the related products for each product, and I added an attribute to products in the install script.
I decided (for simplicity) - to store the most related 5 products, so I want to store something like: 123-3-5543-1290-9911. But I don't want the administrator to see this anywhere, and I tried the following:
$setup->addAttribute('catalog_product', $attrCode, array(
// more stuff
'type' => 'hidden',
'input' => 'text',
'visible' => 0,
// more stuff
));
I looked here: http://blog.chapagain.com.np/magento-adding-attribute-from-mysql-setup-file/ and I found some interesting stuff, but not how to completely hide this field.
The alternative would be to create my own table, but this seems to be a slightly more elegant solution.
What do you think? Is it better to create my own table, or to add an attribute and hide it?
Thank you
Setting the 'is_visible' property to 0 for catalog attributes will hide them from the admin forms in the backend, as can be seen from this code (Mage_Adminhtml_Block_Widget_Form::_setFieldset()):
protected function _setFieldset($attributes, $fieldset, $exclude=array())
{
$this->_addElementTypes($fieldset);
foreach ($attributes as $attribute) {
/* #var $attribute Mage_Eav_Model_Entity_Attribute */
if (!$attribute || ($attribute->hasIsVisible() && !$attribute->getIsVisible())) {
continue;
}
So execute
$setup->updateAttribute('catalog_product', $attrCode, 'is_visible', '0');
I solve it in this way.
Go to Catalog >> Attributes >> Manage Attributes >> Add new attribute
Then, save. Then, Go to database and Run this query:
SELECT * FROM eav_attribute WHERE attribute_code ='attribute_code';
Change the column frontend_input value to hidden, then save.
Hope it helps.

Code Igniter - carts library random works

I’m using a cart library to get orders in my web bookstore. But when I call a addCart function on one of my book it’s works, but not all the time. Please, help
There is my model function:
function get_books_by_ID($id)
{
$this->db->where('BOOK_ID', $id);
$query = $this->db->get('books');
return $query;
echo vardump($query);
}
Controller:
function addCards($id=1)
{
$query = $this->Kategorie_model->get_books_by_ID($id);
if($query->num_rows() > 0)
{
$item = $query->row();
$data = array(
'id' => $item->BOOK_ID,
'qty' => 1,
'price' => $item->BOOK_Price,
'name' => $item->BOOK_Title
);
$this->cart->insert($data);
}
}
View:
<tr>
<td class="color"><b>Cena: </b><?php echo $data->BOOK_Price;?>zł</td>
<td class="border" id="koszyk" ><?php echo anchor('ksiegarnia/addCards/'.$data->BOOK_ID, 'Koszyk'); ?></td>
</tr>
UPDATE:
vardump is nothing necessary. I want to use var_dump. But the problem is related with adding items to the session with carts library. I have a bookstore, and when I call a addCarts function, sometimes items is added to Carts, and cart function total() and total_items displaying it, but sometimes when I call function, nothing is happened. The items not add into carts. I don't now why this thing have a place. Why the carts library works randomly?
I just ran into this issue and it seems that in the codeigniter cart library the insert function checks the product(s) id and name against a regex only allow alpha-numeric, dashes, underscores and periods
Adjust the regex to what may come up:
$this->cart->product_id_rules = '.a-z0-9_';
$this->cart->product_name_rules = '.\:-_ a-z0-9';
In my case it would randomly add thing to carts too. If you turn on logging and check you'll be able to see that the name or id may contain invalid chars
i am doing as you did, and like you said, the cart is randomly works,
it seems can only contain 3 product in cart when i add another product, the product i added wont get into cart..
i am so hopeless, i get mind to change cart libraries with 3rd party
it is because i did not setting session to save to database, like in the tutorial (cart & session) the cart need session to write to database in order to work.
when i set session write to database, the problem solved
you can try...

Categories