WooCommerce edit order privilledge - php

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.

Related

Yiiframework First time login

I'm currently busy with a project that needs users to go to a specific page to create a profile when they log in for the first time (and haven't created one yet). Honestly, I don't know where to start. I would like to do it in a good way.
So in short:
User signs up -> logs in -> needs to fill in form before anything else is allowed -> continue to rest of application
Question: What is a neat way to do this? A solution that isn't going to give me problems in the future development of the application.
I suggest you to use filters. In every controller where the completed profile is neeeded add this code:
public function filters() {
return array(
'completedProfile + method1, method2, method3', // Replace your actions here
);
}
In your base controller (if you don't use base controller, in any controllers) you need to create the filter named completedProfile with the simular code:
public function filterCompletedProfile($filterChain) {
$criteria = new CDBCriteria(array(
'condition' => 'id = :id AND firstname IS NOT NULL AND lastname IS NOT NULL',
'params' => array(':id' => Yii::app()->user->getId())
));
$count = User::model()->count($criteria);
if ($count == 1) {
$filterChain->run();
} else {
$this->redirect(array('user/profile'));
}
}
Possibly add a field to the user profile database table which denotes if they have filled out their profile information. Something like profile_complete. Then you can do a test on pages to see if profile_complete is true and display the page if so, and display the profile page if not.

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

How to mark an order failed programmatically - Magento

I would just like to know what is that one property which when set would mark the order as failed so that failureAction() of app/code/core/Mage/Checkout/controller/OnepageController.php gets called.
I have an Observer.php where in I'm creating invoice for successful payments and for failed payments saving order with pending_payment status and wanting to redirect them to the cart page with an error message at the top.
All this happens fine. Just that for unsuccessful / failed payments, along with saving the order with pending_payment status n redirecting them to cart page with error message, I would also like to retain/save the cart from getting empty.
But to no luck
Observer.php
public function implementOrderStatus($event)
{
$order = $event->getEvent()->getOrder();
if ($this->_getPaymentMethod($order) == 'mypaymentmodule')
{
$quote = Mage::getModel('sales/quote')->load($order->getQuoteId());
if($order->getPayment()->getCcTransId() == NULL)
{
$order->cancel();
$order->setStatus('canceled');
$order->save();
$quote->setIsActive(1)->save();
/*$state = 'pending_payment';
$status = 'pending_payment';
$comment = 'Payment transaction failed due to incorrect AVS/CVD details.';
$isNotified = false;
$order->setState($state,$status,$comment,$isNotified)->save();
$order->setCanSendNewEmailFlag(false);*/
Mage::getSingleton('checkout/session')->addError('Sorry, either of your card information (billing address or card validation digits) dint match. Please try again');
Mage::app()->getFrontController()->getResponse()->setRedirect('checkout/cart/')->sendResponse();
}
else
{
if ($order->canInvoice())
$this->_processOrderStatus($order);
}
}
return $this;
}
But $quote->setIsActive(true)->save() does not seem to be doing the trick. Any help as to how can I save my cart from getting empty after saving the order with 'canceled' status.
You maybe should have a look at ./app/code/core/Mage/Sales/Model/Order.php. There you will find several constants for an order, which may be used to set the state of an order like this:
<?php
// [...] all your code within your custom action or script
//load your order by order_id (or by increment_id, if you like to, here, it's your order id
$order = Mage::getModel('sales/order')->load($your_order_id);
$order->setState(Mage_Sales_Model_Order::STATE_CANCELED); //or whatever distinct order status you'd like
$order->save();
The failureAction in the controiller action does nothing of the sort, if you want to call it manually, you can build it's url using Mage::getUrl()

Remove publishing capability from user IDs

I'm trying to remove the capability of users other than the administrator/superadmin (ID number 1) to add a page, I know that there are plugins which you can edit the wordpress role but in my case I need it to be per user/username/userid (no plugin that I no of is available)..
Currently user number 2 needs to be 'Administrator' because a specific plugin I use only displays reports to 'Administrator' role but I need to remove add page capabilities. I have the following code:
function modify_capabilities()
{
global $userdata;
get_currentuserinfo();
$userdata->ID != 1 ->remove_cap('publish_pages');
}
add_action('admin_init','modify_capabilities');
But it doesn't work.. The error is in this line:
$userdata->ID != 1 ->remove_cap('publish_pages');
Your code seems off by a bit:
function modify_capabilities()
{
global $userdata;
get_currentuserinfo();
if ($userdata->ID != 1) {
$role = get_role('author');
$role->remove_cap('publish_pages');
$role->remove_cap('publish_posts');
}
}
add_action('admin_init','modify_capabilities');
Updated based on your comments and taking the original details from the blog you linked to. Not sure why you removed those parts though...

In ubercart how to get uid and order_id after the user is created and associated to order

I have seen similar questions such as drupal :: order complete hook and upgrade user permission/roles which would work for me except that my order never reaches completed, only payment_received. At this time the uid is 0. It still doesn't work if I add a conditional action under "Customer completes checkout" to mark the status as complete as the uid is still 0.
So my question is, how can I get the uid and the order object after the user has successfully completed checkout and been created?
When Ubercart creates the user it also logs them in so you would just have to do this to get the uid:
global $user;
$uid = $user->uid;
It would probably be best served in hook_order() as mentioned in the similar question you linked to.
UPDATE
If there's no uid associated with the order you should be able to something like this:
function MYMODULE_order($op, &$order, $arg2) {
if ($op == 'update' && $arg2 == 'payment_received') {
if ($order->uid) {
$uid = $order->uid;
}
else {
global $user;
$uid = $user->uid;
}
}
}

Categories