Magento $collection->join - Multiple - php

I'm currently extending the Magento Invoice Grid to display some additional columns.
I've got the data that I need for most, but I'm struggling to get more than 1 Column from the same Database Table.
This is what I have in my _prepareCollection
$collection->join('invoice','main_table.entity_id=invoice.entity_id','subtotal');
$collection->join('invoice','main_table.entity_id=invoice.entity_id','discount_description');
I assume I need to drop the second line and put them into somekind of array, I'm just not sure on how to do that as I don't work with PHP too much.
Thanks.

Oh, this was much more simple than I was expecting, I managed to work it out with the following:
$collection->join('invoice', 'main_table.entity_id = invoice.entity_id', array(
'subtotal',
'discount_description'
));

Related

Trying to populate custom fields in Wordpress from WP database

I'm trying to populate a custom field, let's say 'field_123' (in this case from ACF plugin) with data from a Wordpress database table (which is coming from an API), let's say 'house' > 'square_metres'.
Posts (cpt) are already showing the main content 'out of the box' but I would want to add some more database tables to populate other custom fields. (which I then can use later on on different grids in visual composer)
I started with something but then realized soon enough I wasn't gonna solve the problem myself..
global $wpdb;
$new_value = $_POST['acf']['field_123'];
$wpdb->update(
"house",
array(
"square_metres" => $new_value,
),
array( '%s' )
);
}
add_action('acf/save_post', 'update_field', 1);```
Someone any suggestions? Or a push in the right direction? Perhaps there are smarter/easier ways to achieve this.
Thanks!
Is it all on the same DB? "house" is an existing table? what's it's name on the database? are you sure you didn't call it "wp_house"? in that case you are missing $wpdb->prefix."house" as the table name.
That last row of "add_action" is confusing me, what were you exactly trying to do? With that code, if the "house" table exist, you whould be able to populate its square_metres value with what you have in $new_value just fine (be careful, you didn't specify any WHERE clause, you are going to update ALL the rows in a single shot, dunno if this was intentional).
Is your post missing a piece of code?

How to add Category, Websites, and Low Stock Date columns to Low Stock Report in Magento?

I want to add 3 new columns to the Low Stock Report in Magento. I need to display the product's categories, websites, and low stock date. I've tried adding this line ->joinInventoryItem('low_stock_date') to add the low stock date but I am not getting any value.
For the websites, I've tried adding this line:
->joinField('websites',
'catalog/product_website',
'website_id',
'product_id=entity_id',
null,
'left'
)
But I am also not getting any values.
Hi there at first you need to prepare a collection by joining tables.
For that see the example of joining the tables in the answer provided in the below link.
joining the tables
Another possible way would be using renderer to render the value in the grid.For that please refer to the answer that i provided in the link below.
using renderer
Or you can check the tutorial provided by Inchoo here
Hope this will help.

Insert an object into MySQL database field

i'm developing a web platform in codeigniter (first time with CI) to calculate quotes for a growing number of different products.
The problem i'm facing is that each of my products have different sets of 10+ options but I want to save this data to just one table in my database. I have previously used a different table for each product allowing the table structure to represent the different sets of options however this isn't very scalable with our growing product range.
After some research it appears one solution would be using the 'serialize' function to store all of my post data (from the quote form) for each product in one column and then unserialize when I want to use this data...
Is serializing the data the best approach and would anyone be able to provide a simple example to show how to handle the insert from a form submission / retrieving the data?
Thanks very much in advance
EDIT: Searching will only ever on a product type or unique id. My thoughts were to have a table like 'id, product_type, product_options' with the product_options containing the serialized data?
EDIT 2: Taking an EAV approach seems like a good shout. I'm used to querying and returning a single result object to be passed into the view ($query->quote_ref, $query->quote_date for example). Could anyone point me in the right direction on how to use the single quote's data when the query would return multiple rows, one for each attribute?
As you mention you would not need to search on the serialized data, then yes this approach will be fine. I would opt for json_encode as the data is more human readable in this form.
Then code will depend on your DAL but a basic example would be:
$productOptions=array($_POST['option1'], $_POST['option2']);//etc will need to validate data
$databaseMapper->product_type='product type';
//your product_options column is suitable sized varchar
$databaseMapper->product_options=json_encode($productOptions);
$databaseMapper->save();
To retrieve:
$databaseMapper->loadById(20);
//$productOptions is a standard php array
$productOptions = json_decode($databaseMapper->product_options, true);
EDIT re displaying in your view.
This is codeignitor specific (whereas the above is not).
Based on code from here: http://ellislab.com/codeigniter/user-guide/general/views.html
In your controller:
//code similar to above to retrieve product options data, ideally contained within a model
$data['productOptions']=$productOptions;
$this->load->view('content', $data);
in your view:
<ul>
<?php foreach ($productOptions as $option):?>
<li><?php echo $option;?></li>
<?php endforeach;?>
</ul>

Magento Order Table: custom column

I recently made a module that calculates the real gross margin for every order and order_item based on shipping cost data I import. I did this by adding 2 columns to the sales_flat_order table and the sales_flat_order_item table. This seemed to work great, until I realized that when I saved the imported data, it also updated the updated_at value. Since this was the first import of all orders, they all now report having been updated today. This is throwing off reports and other shipping software that syncs with it.
This brings me to 2 questions:
Is adding a column to an existing table (in this case, the sales tables) a major NO-NO?
If not, is there a way to set the data that doesn't increase the updated_at value?
If it helps, the code that actually writes the data is inside my IndexController.php file. It loops through the collection of orders and the items within those orders and sets the necessary values using something like $order->setGrossMargin($orderGM)->save();. I imagine it is the call to save() that is doing it, but I'm not sure the right way around this problem.
In the mean time, I'm working on a solution in which I import the data to custom tables and only read from the sales tables when necessary. Either way, it's a good exercise :)
Brian
Instead of calling save(), did you try:
$order->getResource()->saveAttribute('gross_margin')

Combining multiple rows or results with the same title to form drop down menus with PHP and MySQL

So I am picking up a project that was quit halfway through by the last guy so that I could get some more practice with PHP and databases. I have run into a problem, and I am sure it is common enough that there is a standard solution, but I am unable to find one.
The db I am working with has 4,600, so reorganizing is out of the question. It is a db of liquers for a wholesaler. Here is what the results page looks like currently:
What I am trying to set it up so the results are returned in list form, with only one title and dropdown menus for the different sizes/prices of products that looks like this:
The problem is that there are multiple entries in the db for each product. In this example there are 3, while some have 1, and some have 2.
I am really not sure how to go about this, and any help would be greatly appreciated. Thank you.
I'm not sure about the PHP syntax, but pseudocode here's what you could do:
allProductsReturnedFromMySQL = QueryYourDatabaseForAllProducts()
Hashtable[productId, List[productSizes]] dropDownsByProduct;
Hashtable[productId, commonProductInformation] uniqueProducts;
foreach (product in allProductsReturnedFromMySQL) {
if product.productId not in uniqueProducts
then add it with the product information that does not vary
if product.productId not in dropDownsByProduct
then add it with an empty list
append the size of this product to the corresponding list in dropDownsByProduct
}
After that little bit of logic you'll have all your unique products with the common properties for each one, and a way to fetch the corresponding sizes drop down. If you wanted to do this purely in SQL to minimize the data that's transferred, you could do something like this:
-- this would get you your products
select distinct id, property1, property2 from product
-- this would get you your drop downs by product
select id, size from product order by id
You can then build the same drop down hashtable by iterating through the second result set.
I'm not sure if this is the best way, but I've always approached this by altering the query so that it is sorted by product name. Then as you iterate through the rows, check to see if the product name matches the one you just processed. If it's the same, then this row is a different size of the same project.

Categories