Performing an SQL query in oxid - php

I am fairly new to OXID.
I am trying to perform an SQL query by using OXID functions only.
The point is to check if $discount exists in oxVoucherSerie's table oxdiscount, and if it exists get the oxid from oxVoucherSerie that corresponds to that discount.
Sadly I cannot use plain MySQL and I am only allowed to use oxid functions. I have no idea how to do it.
public function save_voucher($discount){
$o_voucherid = oxNew("oxVoucherSerie");
$aWhere_discount['oxdiscount'] = $discount;
$sSql = $o_voucherid->buildSelectString($aWhere_discount);
$b_exists = $o_voucher->assignRecord($sSql);
}
This tells me if the discount exists. However, I have no idea to retrieve oxid from said discount.

we might need to clarify some wording and variables first, because i'm not really sure what you are trying to achieve.
voucher - (also called "coupon") shop visitor can enter voucher/coupon code in basket to achieve a price reduction or a free product.
Vouchers are generated in Shop Settings -> coupon series
discount - general price reduction for some categories or articles, e.g. 10% for pet accessories.
Discounts can be configured in Shop Settings -> Discounts
okay, i updated my post.
your code works well, i just changed it a little bit. First the code and then the explanation:
$o_voucher = oxNew("oxVoucherSerie");
$aWhere_discount['oxdiscount'] = 25;
$sSql = $o_voucher->buildSelectString($aWhere_discount);
$o_voucher->assignRecord($sSql);
var_dump($o_voucher);
I replaced $o_voucherid with $o_voucher (without id)
$o_voucher = oxNew("oxVoucherSerie"); gives you an oxVoucherSeries Object
$o_voucher->assignRecord($sSql); does not only tell you if voucher series with such discount exists, it also will load this voucher serie into $o_voucher , so you can access all the (not protedted) parameters of this voucher serie by using $o_voucher, e.g.:
$o_voucher->getId() or $o_voucher->oxvoucherseries__oxid->value() for getting its oxID or just var_dump($o_voucher); to see all the objects properties
By the way, if you are developing with OXID 4.9 you could use this module: https://marat.ws/direct-access-to-oxid-framework-functions/ for quick evaluating and debugging you code.
just copy-paste the code from above into the textarea and hit the button

Related

How do you set the special_price of product in magento via direct SQL query?

I need to mass update special price from a CSV File (which tells me the sku, the special price, to/from date).
At the moment I do it using magento models method like this (after parsing the CSV rows) in a foreach loop:
$p = Mage::getModel('catalog/product');
$product_id = $p->getIdBySku($product['SKU']);
if (!$product_id) {
throw new Exception('Product Does Not Exists');
}
$p->setStoreId($store_id)->load($product_id);
$p->setSpecialPrice(floatval($product['Price']));
$p->setSpecialFromDate($product['FromDate']);
$p->setSpecialFromDateIsFormated(true);
$p->setSpecialToDate($product['ToDate']);
$p->setSpecialToDateIsFormated(true);
$p->save();
$p = null;
This is okay when a handful of product needs updating. However when you update 100+ products, this becomes incredibly slow and it affects the site performance also.
Is there anyway I can mass import special price and also set the dates via a direct SQL Query?
Whilst researching this issue, I have found a possible solution (based on this article http://fishpig.co.uk/magento/tutorials/update-prices-sql) with setting the special_price in the database directly like this:
Note: bd_ is my table prefix
UPDATE bd_catalog_product_entity AS CPE
INNER JOIN bd_catalog_product_entity_decimal AS CPED ON CPED.entity_id = CPE.entity_id
SET CPED.`value` = 2.99 -- New Special Price
WHERE
CPED.attribute_id = (
SELECT
attribute_id
FROM
bd_eav_attribute eav
WHERE
eav.entity_type_id = 4
AND eav.attribute_code = 'special_price'
)
AND CPED.store_id = 1 -- Retail Store
AND CPE.sku = 'ABS049' -- Sku Being Updated
This appears to be working (i can see the value in magento UI updated). I am not yet sure if this is the correct way to go about setting the special price in the database directly.
Also, I've not yet worked out how to set the from/to dates.
Any help on this will be much appreciated.
I recommend you to use Dataflow Profiles. (I guess you already know its usage, I am writing down the procedure in case it can be helpful to someone who doesn't know the usage of Dataflow Profiles)
Go to System-> Import/Export -> Dataflow Profiles
Run Export All Products Profile.
From the csv exported to your var/export directory of your magento, extract the products whose special price you wish to insert/update.
Update values of special_from_date, special_to_date and special_price fields in a new csv you wish to import.
Import the new csv you just created by running Import All Products Profile.
This is a much reliable way. Hope it helps!!
I'd recommend usage of INSERT with ON DUPLICATE KEY whilst the value for the special_price attribute could be not yet present. Didnt test your SQL but it looks ok - key things to handle is
to retrieve the correct attribute_id from eav_attribute (not releted
tip: don't do it in a subquery cause the performance will suffer
from it)
find the entity_id of the product by SKU from catalog_product_entity
another thing to keep in mind is when you use configurables you probably should set the price for configurable too, and all the simples (but this depends on your business approach)
Both the options will work but you have to do re-indexing of data in 2nd option. I am using the same sql for one of my project to set sale price. I am using it in cron file.
Direct query will save your time, if you have large amount of products.
Mage::getModel('index/process')->load(2)->reindexAll();
OR
php indexer.php --reindex catalog_product_price

Flagging table entries not to show again after that row of entries has been selected/used

I am a beginner in programming and I don't know if my title suits my issue, but here is what I am dealing with:
I made a simple inventory program. I can insert, edit and delete entries and it all works perfect.
For my next challenge I need to make it that when an item gets sold out of the inventory that my database will flag that entry so that it will not show again in that list in the application. (Makes sense right? when u sell a product it can't still be there cuz its sold)
But since I am a beginner I have no idea whats o ever how to research this cuz I have never done this before! That's why its a challenge :)
Can you pls tell me what I should research to learn about how I should be doing this ?
Thank you in advanced for ur advice and attention!
And my apologies if this question is off-topic to this website but I don't know where else to ask since this website is so awesome!
Cheers
If products are coming INTO your inventory you would need to scan the products and the amount of how much has come in right? When you do this for a product you would just update the record with the ID of the product that has come in(By scanning the product you should get the ID I assume) and the update query could look something like this:
UPDATE myTable SET productStock=productStock+amount* WHERE productID = ID*
*Amount stands for the amount that came INTO your inventory.
*ID stands for the ID of the product.
The above was an intro, now your question which you can lead off of the above, when a customer wants your product, and clicks on it, orders it, you can do the UPDATE query again, but now MINUS 1 or whatever the amount the customer orders(again by doing multiple amounts you should always check if you have it in stock, but that's another part)
UPDATE myTable SET Stock=Stock-amount* WHERE productID = *ID
*Amount now stands for the amount that the customer orders OUT OFF your inventory.
*ID stands for the ID of the product.
When your "productStock" turns 0, you could simply modify or add a query which will only show values where the "productStock" IS NOT 0.
Example:
SELECT * FROM products WHERE productStock <>* 0;
*<> This symbol stands for NOT EQUAL and in some versions of SQL this is written as !=
Hopefully this would help you on your way.
EDIT:
Why you should not 'flag' or 'remove' the product out of your DB is because when it returns you don't have to fill in the entire product again...

How do I weed out invalid results from an amazon api query

I'm using this function to request books from amazon that are priced at 0 but I am getting three different responses.
Some books are listed at 0
http://www.amazon.com/dp/B009EFZQV0/?tag=centsme-20
Some have a price
http://www.amazon.com/dp/B00A3H57CG/?tag=centsme-20
And others have a price but are free for prime members
can't find an example now
Now I looked at the source of the page and noticed multiple values for price and that I need the id pricelarge and listprice to both be 0. The faq doesn't help me at all and I'm getting stumped on how to fix this.
This is the query function I'm using now.
$search = new Search();
$conf->setRequest('\ApaiIO\Request\Soap\Request');
$conf->setResponseTransformer('\ApaiIO\ResponseTransformer\ObjectToArray');
$search->setCategory('MobileApps');
$search->setMaximumPrice(0);
$search->setPage($page);
$search->setBrowseNode($bid);
$search->setSort('reviewrank');
$search->setResponseGroup(array('ItemAttributes','ItemIds', 'Images'));
$formattedResponse = $apaiIO->runOperation($search, $conf);
For the response, I would also grab a price variable. That way you can double check the price of the item before you present it to your user.

How to use a combination of PHP and MySQL to get Related Products

I've a large table of products, and want to get related products based on a similarity of some kind - whether it is product code or title yet I am yet to decide.
What I want effectively is an output of the 4 most likely matches for a product based on title or code.
Example:
Product Code Related Products
JC83022 JC83021, JC83020, JC83029, JC8300
So I would ideally like a field appended to the product table which does some kind of string matching and returns the codes for the 4 most likely matches.
My idea is that I can use a MySQL query to perform this, but looking around PHP may have some better functions - maybe I could have a function on the product page that returns the products with the nearer levenshein distance - it's just how to query that against a table of over 30,000 products.
Cheers in advance.

Magento Order creation sales_flat_quote_item not being made

I have a module that takes a feed from another site and then imports the orders into magento. The problem is that despite the orders being created properly and appering in Magento they don't show up in the Products Ordered report.
The reason seems to be that this report looks at the sales_flat_quote_item table to produce its results but there is no entry for my sale items. They do however appear correctly in sales_flat _order_item.
Below is a shortened version of the code.
Any suggestions as to why im not getting an entry in flat_quote_item?
Why does the Magento model used by the Ordered Products report use the quote table and not the order table?
$quote = Mage::getModel('sales/quote')->setStoreId((string) $dataArray->StoreviewId);
if (is_object($product)) {
$product->setPrice(((string) $orderitem->Price) / $reverseRate);
$item = Mage::getModel('sales/quote_item');
$item->setQuote($quote)->setProduct($product);
$item->setData('qty', (string) $orderitem->Quantity);
$item->setCustomPrice((string) $orderitem->Price);
$item->setOriginalCustomPrice((string) $orderitem->Price);
$quote->addItem($item);
}
This code doesn't show any calls to $item->save or $quote->save, so it could be that you aren't saving the quote object.
Why does the Magento model used by the Ordered Products report use the quote table and not the order table?
Because you could have an order, which wasn't paid or was canceled and thus the product wasn't delivered. You still have an order in the system, it just wasn't executed.
My guess is, that the specific report should only contain successful order, where the products were shipped or at least the quote was sent.
Any suggestions as to why im not getting an entry in flat_quote_item?
You need to generate the quote, which isn't done automatically on saving an order.
See the following forums thread to get hints on how to generate the quote: http://www.magentocommerce.com/boards/viewthread/28426/P30/

Categories