Show product on main page by id opencart - php

I need to show one certain product on front page. I didn't find a module for this. What do I need to do? I need to show product with countdown timer but I can do this, I only do not know how to get product from database by ID or SKU. I guess the code should be like this:
echo $product_option_data[] = array(
'product_option_id' => $product_option['product_option_id'],
'option_id' => $product_option['option_id'],
'name' => $product_option['name'],
'type' => $product_option['type'],
'option_value' => $product_option['option_value'],
'required' => $product_option['required']
);

In OpenCart controller you can call
$this->load->model('catalog/product'); // only if not yet 'loaded' within the controller
$product = $this->model_catalog_product->getProduct($productId);
print_r($product);
which will return you the product by it's ID. Do with it whatever you need afterwards.

Related

How to change layout autocomplete dropdown list in Opencart

When we used autocomplete in opencart admin then it shows a drop down list of product names (just Like a featured products modules). I want to show model number instead of product name. if anyone know how i can do it ?
Thanks in advance.
I think you need to show instead on product name show model name or some other thing.
go to admin-->controller-->module-->featured modules(any other)
goto to code
if ($product_info) {
$this->data['products'][] = array(
'product_id' => $product_info['product_id'],
'name' => $product_info['product_name']
);
}
replace it into your needs
if ($product_info) {
$this->data['products'][] = array(
'product_id' => $product_info['product_id'],
'name' => $product_info['model']
);
}
now you just need to refresh it. Product name will replace into model name in admin panel.
'name' => strip_tags(html_entity_decode(
$result['model'] . ' - ' . $result['name'],
ENT_QUOTES,
'UTF-8'
)),
gives model and name.

Magento API v1- List prices for all products in one call

I've got the following code:
$filters = array('sku' => array('like'=>'%'));
$items = $magConn->call($sessionID, 'product.list', array($filters));
This will return an array of all the products and their sku, description, and qty.
However, I also need to get the price? Is there a way to get that as well?
I've also got this working,
$properties = ($magConn->call($sessionID, 'product.info', $item['sku']));
which will return all the attributes for one product. I've got over 2,000 products, and this is definitely not feasible if I want it to end tonight. ;)
No way without magento source code modification. You should go to \app\code\core\Mage\Catalog\Model\Product\Api.php, find next lines inside items() method:
$result[] = array( // Basic product data
'product_id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'set' => $product->getAttributeSetId(),
'type' => $product->getTypeId(),
'category_ids' => $product->getCategoryIds()
);
and add price here.
Load the collection :
$product = Mage::getModel('catalog/product')->load($productId);
Get actual price :
$product->getPrice();
Get special price :
$product->getFinalPrice();

Magento 'Select Configurable Attributes' with PHP

I have created new configurable products, and attached their simple products with PHP.
Now when I edit any configurable product I see this screen:
So in absence of any Magento documentation, what do I call in PHP to perform the same function as the screen above programatically?
I have seen $configurable_product->setConfigurableProductsData() used in some examples, but don't think it is what I need.
You are right, you are creating the association/link between configurable and child products but what's happening is that when you are creating your configurable product you are not setting up the setConfigurableAttributesData that basically setups the superattribute information for that configurable product.
foreach($configAttrCodes as $attrCode){
$super_attribute= Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product',$attrCode->code);
$configurableAtt = Mage::getModel('catalog/product_type_configurable_attribute')->setProductAttribute($super_attribute);
$newAttributes[] = array(
'id' => $configurableAtt->getId(),
'label' => $configurableAtt->getLabel(),
'position' => $super_attribute->getPosition(),
'values' => $configurableAtt->getPrices() ? $configProduct->getPrices() : array(),
'attribute_id' => $super_attribute->getId(),
'attribute_code' => $super_attribute->getAttributeCode(),
'frontend_label' => $super_attribute->getFrontend()->getLabel(),
);
}
$existingAtt = $product->getTypeInstance()->getConfigurableAttributes();
if(empty($existingAtt) && !empty($newAttributes)){
$configProduct->setCanSaveConfigurableAttributes(true);
$configProduct->setConfigurableAttributesData($newAttributes);
$configProduct->save();
}
This is small snippet should get you there, let me know if you have any questions or need further help.

Magento: how to load product along its all data as it is used in admin

I'm trying to get bundle options data. using this : $product->getBundleOptionsData I need to use this, as I'm trying to change data programmatically and I would like to do it in a way that's as close as used in admin .
However, when I var_dump the result of the above function I get NULL while in admin side in bundle model product type I get correctly the data.
When I var_dump $product in my own file I get much shorter data than when I var_dump in bundle model product type save function.
what do I need to do to load all data of the product, so I can use getBundleOptionsData. I looked in several files and googled, but can't find an answer.
Finally I made it work to get bundle options data so I can manipulate it. I found the main code in magento's model bundle observer class duplicateProduct function: I needed however to add option_id (careful not to forget that)
here is the code in it's final stage.
$product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product);
$optionCollection = $product->getTypeInstance(true)->getOptionsCollection($product);
$selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product),
$product
);
$optionCollection->appendSelections($selectionCollection);
$optionRawData = array();
$selectionRawData = array();
$i = 0;
foreach ($optionCollection as $option) {
$optionRawData[$i] = array(
'option_id' => $option->getOptionId(), //my addition. important otherwise, options going to be duplicated
'required' => $option->getData('required'),
'position' => $option->getData('position'),
'type' => $option->getData('type'),
'title' => $option->getData('title')?$option->getData('title'):$option->getData('default_title'),
'delete' => ''
);
foreach ($option->getSelections() as $selection) {
$selectionRawData[$i][] = array(
'product_id' => $selection->getProductId(),
'position' => $selection->getPosition(),
'is_default' => $selection->getIsDefault(),
'selection_price_type' => $selection->getSelectionPriceType(),
'selection_price_value' => $selection->getSelectionPriceValue(),
'selection_qty' => $selection->getSelectionQty(),
'selection_can_change_qty' => $selection->getSelectionCanChangeQty(),
'delete' => ''
);
}
$i++;
}
$product->setBundleOptionsData($optionRawData); //changed it to $product
$product->setBundleSelectionsData($selectionRawData); //changed it to $product
you can either now change on the raw data in optionsrawdata. or getBundleOptionsData. and same for the other one.

Magento custom options: success.. almost!

Alright, so I'm working on my own script that reads specific xml files, and imports products. Now, each of these products may have a set of attributes, (i.e. color for example).
So, what I'm doing is importing each custom attribute, and generating a specific string, "Yellow, black finish wood" for example, and that would be a radio button you could select from when you go to order the product. Now, when I come to a product that has the same "parentid" but a different unique id, I need to generate a new row for the custom option radio button, so if there is another product where the color is red, I would have the following to select from:
Red, black finish wood
Yellow, black finish wood
This is working great for the first product with the same parent id, in other words, if I'm creating a brand new product, and not just adding custom options to the same product.
My question is, how do I generate a new custom option value, for the specific option? In other words, how do I add a new row to a certain option that is already created?
Here is basically what I'm doing to generate the first option on a new product.
$options = array();
$options[$ParentID] = array(
'title' => 'Option Title',
'type' => 'radio',
'is_require' => 1,
'values' => array()
);
$options[$ParentID]['values'][] = array(
'title' => 'Option Value Title',
'price' => 0.00,
'price_type' => 'fixed',
'sku' => $UniqueID,
);
foreach($options as $ParentID => $option) {
$id = Mage::getModel('catalog/product')->getIdBySku($ParentID);
$product = Mage::getModel('catalog/product')->load($id);
if(!$product->getOptionsReadonly()) {
$product->setProductOptions(array($option));
$product->setCanSaveCustomOptions(true);
$product->save();
}
}
this works out great, because magento generates an sku when they order a certain product in the format of $ParentID_$UniqueID, and then I can send the $UniqueID off to the vendor for the order.
So again, I'm open to suggestions as to how to add a Option Value to an already created Custom Option
load all options in and create array map with unique and already existing options to get their id -s in place

Categories