I am able to create orders programmatically using my custom module.
I wish to add a flag to differentiate between various types of orders that can be created from admin/websites and orders created from my custom module.
I understand that admin and website orders can be differentiated by
if(!empty($order->getRemoteIp()){
//place online
}
else{
// place by admin
}
However, I still want to differentiate between orders manually placed from admin and orders placed from my custom module.
The following are some solutions I thought of
1) Adding a prefix to the order or order-increment id.
2) Creating a new store during creation of module and add all orders from my custom module using that store. However, I am not sure of what implications this might cause.
3) I am able to change the store name during order creation using
$order->setStoreName('customName');
But, this is not visible in the admin grid or order detail page. I'm guessing they fetch the information for "Purchased From" from the store id.
I am looking for, what could be the best solution from the above, or a better solution if any.
Note: My module is currently compatible with magento v1.4 and above. So I will need a solution that covers most versions.
You could create an order attribute and set it when you create your order.
To create an order attribute you can do like this, in your module folder, in sql/mymodule_setup/mysql4-upgrade-VNUMBER.php:
$installer = $this;
$installer->startSetup ();
$setup = new Mage_Eav_Model_Entity_Setup ( 'core_setup' );
$installer->getConnection()
->addColumn (
$installer->getTable ( 'sales_flat_order' ),
'my_attribute_code', // this is where you set the attribute code
'varchar(255) DEFAULT NULL' );
$setup->startSetup ();
$setup->addAttribute (
'order',
'my_attribute_code',
array (
'type' => 'int', // or text or whatever
'label' => 'My attribute Label'
));
$installer->endSetup ();
Then when dealing with the order, simply use
$order->setMyAttributeCode($value);
Related
I am trying to find a way to customize the list_users capability in Wordpress,
I have created a new role called my_admin and i added some capabilities to it.
The problem is that the list_users capability displays all users while i want to display only a group of users according to a user meta field.
Another question about the edit_users capability can i limit this capability to edit the users without promoting them, this capability enables editing the users and also promoting them which means that it enables changes to user roles also. Is there any way i can remove this promoting feature from it, can i also select only one input field to be edited and remove other fields?
function my_new_service_capabilities() {
// Get the role object.
$editor = get_role( 'my_admin' );
// A list of capabilities to add.
$addcaps = array(
'read',
'edit_posts',
'list_users', //I need to list users according to a user meta value.
'edit_users', //I need to enable editing users in only one input field
'moderate_comments',
);
foreach ( $addcaps as $acap ) {
// Add the capability.
$editor->add_cap( $acap );
}
}
add_action( 'init', 'my_new_service_capabilities' );
I think this should work for you →
https://codex.wordpress.org/Class_Reference/WP_User_Query
steps →
Retrieve all users.
User For each loop.
Use meta_query parameter, because WP stores capabilities as json
string in user_meta.
I am giving you a clue how to do this →
$entire_user_list = get_users();
$somse_users= array();
foreach($entire_user_list as $user){
if($user->has_cap('specific_capability')){
$somse_users[] = $user;
}
}
I had solved it using the DOMDocument class.
I need to add a custom column in the sales order adminhtml grid.
Since I get the data for that grid from an external REST API by asking for data to a specific increment_id, I can't use the tutorials which are using a magento database to join the needed tables.
Is there another way like:
function rendering_sales_order_row_before($rowdata) {
$columnContent = $restapi->callByIncrementId($rowdata['increment_id']);
$this->addColumn("Custom Column", $columnContent);
}
(This code should just illustrate my goal and I know that the solution will look completely different)
Is it possible to achieve that in magento in an elegant way?
EDIT: I'm using magento 1.9.2.1
Good question. I had a similiar problem. I solved it by using a custom renderer for my column.
First add your Sales Order Grid block XX_ModuleName_Block_Adminhtml_Order_Grid. Rewrite Mage_Adminhtml_Block_Sales_Order_Grid, extend it and add your column by overriding _prepareColumns() method.
$this->addColumn('my_column', array(
'header' => Mage::helper('sales')->__('My Column'),
'width' => '80px',
'index' => 'my_column',
'type' => 'text',
'order' => 'x',
));
Then add a custom renderer (XX_ModuleName_Block_Adminhtml_Sales_Order_Grid_Widget_Renderer_MyColumn) and extend Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract.
Override the render(Varien_Object $row) method. Here you can do your specific operation (do your api request, manipulate in whatever way you want) and return the string. To build your request you want to use the data by param $row.
That should work :) If you need more info, just comment.
I am trying to find a way to limit Drupal Taxonomy Terms that appear on Content Add form based on their Role permissions but also on their user id. I have a taxonomy set called Departments (IT, Marketing, Accounting, etc).
On this specific content add form, there is a dropdown which asks you to assign the department that the content will belong to. I would like it to be the full dropdown list if the role is set as Admin however if the it is a regular user, I want it to instead be defaulted or locked to the department that they are assigned to in their user profile.
I looked around and have found multiple suggestions online such as setting up an entity reference field and using Taxonomy Access Control module but I can't seem to set it up correctly. Any suggestions?
Thanks!
I think the easiest way to achieve this would be a little code in a custom module.
First I would use hook_permisision to create a new permission called manually_set_department.
It is preferable to check for permissions not roles as roles can change and it is more flexible once a permission is set up as you can apply it to other roles if needed.
To do this use:
function MY_MODULE_permission() {
return array(
'manually_set_department' => array(
'title' => t('Manually set department'),
'description' => t('Allows users to manually set their department'),
),
);
}
Now we need to cut down the list of departments if the user does not have the above permission. To do this we can use hook_form_alter. In this example I'm assuming your content type is called page:
function MY_MODULE_form_alter(&$form, &$form_state, $form_id) {
drupal_set_message("Form ID is : " . $form_id);
switch($form_id) {
case 'page_node_form':
// dpm($form); // dpm (supplied by the devel module) is always useful for inspecting arrays
if(!user_access('manually_set_department')) { // check the permission we created earlier
global $user;
$account = user_load($user->uid); // load the users account
//dpm($account);
$deparment_tid = $account->field_department[LANGUAGE_NONE][0]['tid']; // get their department tid
foreach($form['field_department'][LANGUAGE_NONE]['#options'] as $k => $v) {
if($k != $deparment_tid) unset($form['field_department'][LANGUAGE_NONE]['#options'][$k]); // unset any but their own departments
}
}
break;
}
}
I have a few general questions about modifying Magento's admin section and would be grateful to have them answered. I'm new to Magento so please bear with me.
My goal is to add a new column with a product attribute (e.g. "Size") to the "Category Products" table within the Catalog -> Manage Cateories section (see screenshot below).
Having little Magento dev experience, I'm not quite sure where to start. I had a look in some of the Grid.php files under the adminhtml directory, and while I see a bunch of statements like addColumn(...), I'm not sure where I'd slot in my new attribute column.
Also, I assume that instead of modifying any core files directly, I'd copy them to the same path under the local folder and edit or somehow extend them there? Do I have to edit any config files or do anything else for the change to be reflected? Am I - by doing this - in effect creating my own module?
I also read that I should disable "Compilation" before I make any changes. Why is this? Is there anything else to consider?
Again I am very grateful for any help and appreciate that my questions must seem basic. Any supplementary resources you could point me towards would be appreciated. Thanks.
Indeed you should start by understanding what file to edit and how to edit it. In this case you want to modify app/code/core/Mage/Adminhtml/Block/Catalog/Category/Tab/Product.php but, like you said, you should not modify the file in its current location. There are two ways to modify the file in the "correct" way.
(harder but more extensible) Create a new Module in local and tell Magento in the etc/config.xml that you are overwriting that Block (which is just a php class) with a different block in this new Module and have the new class extend the core Block class. Then you just need to overwrite one function (_prepareColumns).
(easier) Copy the file from app/code/core/Mage/Adminhtml/Block/Catalog/Category/Tab/Product.php to app/code/local/Mage/Adminhtml/Block/Catalog/Category/Tab/Product.php and modify the function you want (_prepareColumns)
If you are new to Magento, I recommend going with the second option because its easier. Magento will always load the file from local before it loads from core so the file in core will no longer be used and your version in local will be used. To find out more, read this article from Alan Storm
Now in order to add the column you want, do something similar to the SKU field
$this->addColumn('size', array(
'header' => Mage::helper('catalog')->__('Size'),
'index' => 'size'
));
in the order you want it (between Product Name and SKU). I am assuming that your Products have a field called size that you can retreive with $product->getSize()
Max solution was pretty spot on but missing some important steps, I'll elaborate on his original method
Create a new local override of the Product Tab by copying app/code/core/Mage/Adminhtml/Block/Catalog/Category/Tab/Product.php to app/code/local/Mage/Adminhtml/Block/Catalog/Category/Tab/Product.php
There are 2 functions involved in modifying the grid view. _prepareCollection and _prepareColumns
_prepareColumns by adding a call to the addColumn function just like:
$this->addColumn('size', array(
'header' => Mage::helper('catalog')->__('Size'),
'width' => '80',
'index' => 'size'
));
_prepareCollection, by default the product collection loaded in the grid only has a few attributes(name,sku,price) what you need to do add our now attribute by ->addAttributeToSelect('size') now if you are only working with a textfield attribute then this is the extend of the modifications you have to do however if your attribute is for example a dropdown you will need to do further changes to the prepare collection:
(optional) dropdown attributes only store the value of the option that was select so we need to provide an options array to the addColumns call so Magento can display the values correctly, we can do that in the following maner:
on your local copy of Products, add the following to the _prepareColumns functions
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'colour');
$options = array();
foreach( $attribute->getSource()->getAllOptions(true, true) as $option ) {
$options[$option['value']] = $option['label'];
}
$this->addColumn('colour', array(
'header' => Mage::helper('catalog')->__('Colour'),
'width' => '80',
'index' => 'colour',
'type' => 'options',
'options' => $options
));
While those are some very thorough question and I'm sure you will learn a lot, there is a ready-made solution; Enhanced Admin Product Grid has the ability to add arbitrary attributes as columns.
I need to get all products belonging to a specific category in Magento using the web services API. I tried this method:
$product_filter = array(
'category_ids' => array('eq' => '41')
);
$product_templates = $magento_client -> call($magento_session, 'product.list');
But it returns an error. I can only assume it's because category_ids is an array, so it won't really ever equal one specific value.
I did some research and found another method called category.assignedProducts and tried:
$product_templates =
$magento_client ->
call($magento_session, 'catalog_category.assignedProducts', array('41'));
But this returned an 'Access Denied' error. I went and looked at my sandbox of Magneto and saw that 'Assigned Products' has 3 options: 'Remove', 'Update', 'Assign', and I know that the admin for the system I'm linking to has set my access to 'Read-Only'. So I'm guessing that we'd have to check off 'assign' in that list, which would give me more access than they want to give.
I could retrieve all of the data and do the filtering on my end, but I wanted to check if anyone knew of a better way.
Thanks.
assignedProducts sounds like what you need but you shouldn't need to be passing along an array but an integer value and the store ID or code.
See the arguments required: http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_category#catalog_category.assignedproducts
I think I found the answer on http://www.magentocommerce.com/boards/viewthread/207099/ which basically says it can't be done from the product any more. We must now look at the problem from the category point of view.
category_ids no longer works in 1.4, they changed the table
structures around so that the categories are not available on the
product. Use the code below to get the products from the category and
then do a catalog_product.list call. To make it faster you can also
create a custom api solution to combine these in Magento and perform
just one call instead of two.
$proxy = new SoapClient($soapUrl.’api/soap/?wsdl’); $sessionId =
$proxy->login($apiUser, $apiPass);
$productList = $proxy->call($sessionId,
‘catalog_category.assignedProducts’, array(’4’));
$proxy->endSession($sessionId); print_r($productList);