WooCommerce products shortcode order by sku list - sql

Is there a way to modify the WooCommerce Products shortcode to order by a list of product sku's?
A search engine send a Post to the page with a list of sku's. The page should display the Products in the same order the http post is.
Example php and mysql select (the code like this worked in my old shop system where I can use SQL syntax):
$_POST['skus'] = "51,57,34,12,111";
$skus = $_POST['skus'];
select * from products where sku in ('$skus') order by FIELD(sku,'$skus);
How can I do an "orderby" like the example with products shortcode from woocommerce or is there a better way to do that with woocommerce?
Thanks for every answer.

With the help from Gavin and the code from https://wordpress.stackexchange.com/a/67830/43362
I found this solution:
function wpse67823_orderby_post_in($orderby, $query){
//Remove it so it doesn't effect future queries
remove_filter(current_filter(), __FUNCTION__);
$post__in = $_POST['skus'];
$post__in = str_replace(',','\',\'',$post__in);
return "FIELD(CAST(mt2.meta_value AS CHAR), '$post__in' )";
}
//Add filter and perform query
add_filter('posts_orderby','wpse67823_orderby_post_in',10,2);
echo do_shortcode('[products skus="'.$skus.'" orderby="sku"]');
remove_filter('posts_orderby','wpse67823_orderby_post_in',10,2);

Related

Odoo- How load products with multiple parents

I am working on Odoo 10e . I have situation which i am unable to solve in here.
I have a relation like following
Customer 1-------* Shipments 1-------* Shipment Detail 1-----* Products
Now i have a separate form in which i want to show products which are associated against a specific Customer in dropdown . How can i do this in Odoo
Do you mean you want to sort all products that have related to the Customer who selected in form view?
You can do this way:
#api.depends('customer')
def get_related_product(self):
res = []
#compute to get your product id here
return res
customer = fields.Many2one(....)
related_product = fields.Many2many(......., compute='get_related_product')

Display Product Combinations in Prestashop Customer View

I am trying to get the product combinations displayed in Prestashop admin Customer detail view in addition to the products that are displayed for the customer.
This seems to be the relevant call from AdminCustomersController.php in public form renderForm():
$products = $customer->getBoughtProducts();
Then in the Customer class I found the method:
public function getBoughtProducts()
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT * FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON o.id_order = od.id_order
WHERE o.valid = 1 AND o.`id_customer` = '.(int)$this->id);
}
How can I modify this method to show the product combination alongside the product name?
I am using Prestashop version 1.6.0.9.
you can get it using 2 ways:
order_detail table already have field 'product_name' that contains value like 'Product Name - Combination', so you can use $products['product_name'] in that case.
or
if for some reason it is not good for you, same table contains also product_attribute_id field, it is combination id, so:
$combination = new Combination($product['product_attribute_id']);
$attributes = $combination->getAttributesName($id_lang);
var_dump($attributes);
will give you array of attributes that current combination contains.

Prestashop get the product default image from the product name?

In Prestashop I want to know by which pattern prestashop shows the product default image? I mean lets say I have a mysql query for prestashop like this
SELECT ps_product.id_product,ps_product.id_category_default,description_short,ps_product_lang.name FROM ps_product_lang,ps_product WHERE ps_product_lang.id_lang=1 AND ps_product.id_product=ps_product_lang.id_product
Now here I will get the product id, product category, description, and name.
Now according to prestashop default products and database I have for the product iPod Nano the database fields are like
id_product id_category_default
1 3
Now if I want to get the product image I will use
img/p/id_product/id_category_default/id_product.id_category_default
This will make the img src like
img/p/1/3/13.jpg
Now when I used that I got the image for Belkin Leather Folio for iPod
but with the same condition I got the exact image for ipod shuffle. So can someone kindly tell me how to get the exact default image for the product name? Any help and suggestions will be really appreciable.
You can use this query to get the image id of the cover image (image displayed in category view) for a product:
SELECT id_image FROM ps_image WHERE cover = 1 AND id_product = $idProduct
Then you can generate the image path with the following code:
$imgPath = 'img/p/';
for ($i = 0; $i < strlen($idImage); $i++) {
$imgPath .= $idImage[$i] . '/';
}
$imgPath .= $idImage . '.jpg';
If you want to get the image id of the default thumbnail displayed in the product view then you can use this query:
SELECT id_image FROM ps_image WHERE position = 1 AND id_product = $idProduct

Magento SQL Query to show product manufacturer

I am trying to query the Product Manufacturer in Magento 1.7.0.2 . I browse again and again all the table to where I can get the manufacturer table and connect it with product SKU.
I try this query to hope that I can get the manufacturers of the product:
SELECT
eav_attribute_option_value.value FROM
eav_attribute,
eav_attribute_option,
eav_attribute_option_value
WHERE
eav_attribute.attribute_code = 'manufacturer' AND
eav_attribute_option.attribute_id = eav_attribute.attribute_id AND
eav_attribute_option_value.option_id = eav_attribute_option.option_id
but it is not equal to the product manufacturer when I compare the result to my magento admin product manufacturer.
My question is that what should I do to query so that I can get the list of manufacturers of the product so that I can sql join in with catalog_product_enity's SKU.
Does anyone has an idea about my case? I am new with magento so please be gentle with me.
Any help will be appreciated, Thanks in advance
I hope I understood correctly.
If you want to get the manufacturer for a product, you don't need any query.
This should work. Let's assume that you already have the product in var $_product;
$_product->getAttributeText('manufacturer');//this gets you the label
$_product->getManufacturer(); //gets the manufacturer id
[EDIT]
To get this for all the products do this:
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('manufacturer');
$collection->addAttributeToFilter('status', 1);//optional for only enabled products
$collection->addAttributeToFilter('visibility', 4);//optional for products only visible in catalog and search
foreach ($collection as $product) {
$sku = $product->getSku();
$manufacturerId = $product->getManufacturer();
$manufacturerLabel = $product->getAttributeText('manufacturer');
//do something with the values above - like write them in a csv or excel
}
Little late, but this is what I came up with. Works like a charm.
SELECT cpe.sku, cpev.value, cpf1.manufacturer_value
FROM catalog_product_entity cpe
JOIN catalog_product_entity_varchar cpev ON cpev.entity_id = cpe.entity_id
JOIN catalog_product_flat_1 cpf1 ON cpf1.entity_id = cpe.entity_id
WHERE cpev.attribute_id = 191

Magento: Get Collection of Order Items for a product collection filtered by an attribute

I'm working on developing a category roll-up report for a Magento (1.6) store.
To that end, I want to get an Order Item collection for a subset of products - those product whose unique category id (that's a Magento product attribute that I created) match a particular value.
I can get the relevant result set by basing the collection on catalog/product.
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('unique_category_id', '75')
->joinTable('sales/order_item', 'product_id=entity_id', array('price'=>'price','qty_ordered' => 'qty_ordered'));
Magento doesn't like it, since there are duplicate entries for the same product id.
How do I craft the code to get this result set based on Order Items? Joining in the product collection filtered by an attribute is eluding me. This code isn't doing the trick, since it assumes that attribute is on the Order Item, and not the Product.
$collection = Mage::getModel('sales/order_item')
->getCollection()
->join('catalog/product', 'entity_id=product_id')
->addAttributeToFilter('unique_category_id', '75');
Any help is appreciated.
The only way to make cross entity selects work cleanly and efficiently is by building the SQL with the collections select object.
$attributeCode = 'unique_category_id';
$alias = $attributeCode.'_table';
$attribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
$collection = Mage::getResourceModel('sales/order_item_collection');
$select = $collection->getSelect()->join(
array($alias => $attribute->getBackendTable()),
"main_table.product_id = $alias.entity_id AND $alias.attribute_id={$attribute->getId()}",
array($attributeCode => 'value')
)
->where("$alias.value=?", 75);
This works quite well for me. I tend to skip going the full way of joining the eav_entity_type table, then eav_attribute, then the value table etc for performance reasons. Since the attribute_id is entity specific, that is all that is needed.
Depending on the scope of your attribute you might need to add in the store id, too.