Show Out Of Stock text in Magento - magento

I need to add on my dropdown list an style into thee out of stock text, im already using this:
if (count($productsIndex) == 1) {
$stockItem = Mage::getModel('cataloginventory/stock_item')
->loadByProduct($productsIndex[0]);
if ($stockItem->getQty() == 0) {
$value['text'] .= ' Out Of Stock';
}
}
$info['options'][] = array(
'id' => $value['value_index'],
'label' => $value['label'],
'price' => $configurablePrice,
'oldPrice' => $this->_prepareOldPrice($value['pricing_value'], $value['is_percent']),
'products' => $productsIndex,
);
But this will show
12 Out Of Stock
This will display next to the size, how can i give style to this ?

Related

Laravel backpack select2_from_ajax setting my value as null after the correct value has been saved

Im having a weird problem.
Im using laravel backpack for an admin panel. There i use select2_from_ajax to list a values according to another field in create operation. It is showing up correctly as expected & i can select one too.
But after selection when i click save & back it gives me an error
That means my column doesn't allow to update to null right.
So when i go back & check the column it has saved the correct value.
But when default value of my column was null this error will not showup & db value would be changed to null.
This is my select2_from_ajax part.
$this->crud->addField([ // Select
'label' => "Link Type",
'type' => 'select_from_array',
'name' => 'link_type', // the db column for the foreign key
'options' => [1 => 'Product',0 => 'Collection'],
'allows_null' => false,
]);
$this->crud->addField([ // Select
'label' => "Link To", // Table column heading
'type' => "select2_from_ajax",
'name' => "link_to",
'entity' => 'link',
'attribute' => "name",
'data_source' => url('admin/itemtype'),
'placeholder' => "Select a item",
'minimum_input_length' => 0,
'include_all_form_fields' => true,
'dependencies' => ['link_type'],
]);
So why is it trying to set null value after the correct value?
Any help would be appreciated. Thanks.
My admin/itemtype function:
$search_term = $request->input('q');
$form = collect($request->input('form'))->pluck('value', 'name');
if ($search_term) {
if ($form['link_type'] == 0) {
$items = Collection::where('name', 'LIKE', '%' . $search_term . '%')->paginate(10);
} else {
$items = Product::where('title', 'LIKE', '%' . $search_term . '%')->paginate(10);
}
} else {
if ($form['link_type'] == 0) {
$items = Collection::paginate(10);
} else {
$items = Product::paginate(10);
}
}
return $items;

Magento invoice grid filter by joined attribute value

I'm using the following code to include a custom attribute, titled "sales rep" within the Magento invoice grid:
protected function _prepareCollection() {
$sales_rep = Mage::getResourceSingleton('customer/customer')->getAttribute('sales_rep');
$collection = Mage::getResourceModel('sales/order_invoice_grid_collection');
$collection->join('invoice', 'main_table.entity_id = invoice.entity_id',array('order_id as order_id'));
$collection->join('order', 'invoice.order_id = order.entity_id',array('customer_id as customer_id'));
$collection->getSelect()->joinLeft(
array('customer_sales_rep_table' => Mage::getSingleton('core/resource')->getTableName($sales_rep->getBackend()->getTable())),
'customer_sales_rep_table.entity_id = order.customer_id
AND customer_sales_rep_table.attribute_id = '.(int) $sales_rep->getAttributeId() . '
',
array('sales_rep'=>'value')
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
...
$this->addColumn('sales_rep', array(
'header' => Mage::helper('sales')->__('Sales Rep'),
'index' => 'sales_rep',
'filter' => false
));
This is working perfectly, just as long as the "addColumn" property "filter" is set to "false".
How would I go about allowing users to filter by this joined attribute?
You should add your column like this:
$this->addColumn('sales_rep', array(
'header' => Mage::helper('sales')->__('Sales Rep'),
'index' => 'sales_rep',
'filter_index' => 'customer_sales_rep_table.value'
));

Creating configurable products programmatically - pricing_value not saved

I have a custom xml file and I make a massive import of the products. I have some simples products under configurables ones. All is working well, configurable products are created with the simple products the "associated products" tab but one last thing still doesn't work : the price of each product.
Actually, Each simple product has its own price and the correct value is well saved in its attribute but in the "super product attribute configuration" panel, the values are empty.
When I fill the price fields manually, it works but it obviously must be done by the script, programmatically.
Here is my function to create the configurable product :
protected function createConfigurableProductFromSimpleProduct($product, $flagshipID)
{
$configurableProduct = $product->duplicate();
$configurableProduct->getResource()->save($configurableProduct);
$configurableProduct= Mage::getModel('catalog/product')->load($configurableProduct->getId());
$configurableProduct->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setSku($flagshipID)
->setDefRef($product_id)
->getTypeInstance()->setUsedProductAttributeIds(array($this->getAttributeId('root_colors'))); //attribute ID of attribute 'root_colors' in my store
$configurableProduct->setName($configurableProduct->getName());
$configurableProduct->setStatus(1);
$configurableProduct->setStockData(array(
'is_in_stock' => 1
));
$configurableProduct->setUrlKey($configurableProduct->getName());
$configurableProduct->save();
return $configurableProduct;
}
And here is the code for linking simple products to this configurable product :
protected function linkSimpleProductsToConfigurableProduct($simpleProducts, $configurableProduct)
{
$configurableProductsData = array();
foreach ($simpleProducts as $_product) {
$_product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$_product->getResource()->save($_product);
$configurableProductsData[$_product->getId()] = array( //[id] = id of a simple product associated with this configurable
'0' => array(
'label' => $this->getAttributeRawValue($this->getAttributeId('root_colors'), $_product->getRoot()), //attribute label
'attribute_id' => $this->getAttributeId('root_colors'), //attribute ID of discriminator attribute in my store
'value_index' => $_product->getColor(),
'pricing_value' => $_product->getPrice(),
'is_percent' => '0'
)
);
}
$configurableAttributesData = $configurableProduct->getTypeInstance()->getConfigurableAttributesAsArray();
$configurableProduct->setCanSaveConfigurableAttributes(true);
$configurableProduct->setConfigurableAttributesData($configurableAttributesData);
$configurableProduct->setConfigurableProductsData($configurableProductsData);
var_dump('saving configurable product after having link some simple products');
$configurableProduct->save();
}
Any help is welcome, thank you !
Hy all! After reading thousand of answer i solved it!
$cProduct->setCanSaveConfigurableAttributes(true);
$cProduct->setCanSaveCustomOptions(true);
$cProductTypeInstance = $cProduct->getTypeInstance();
$cProductTypeInstance->setUsedProductAttributeIds(array($attribute_id));
$attributes_array = $cProductTypeInstance->getConfigurableAttributesAsArray();
foreach($attributes_array as $key => $attribute_array) {
$attributes_array[$key]['use_default'] = 0;
$attributes_array[$key]['position'] = 0;
if (isset($attribute_array['frontend_label'])) {
$attributes_array[$key]['label'] = $attribute_array['frontend_label'];
} else {
$attributes_array[$key]['label'] = $attribute_array['attribute_code'];
}
}
$dataArray = array();
foreach ($simpleProducts as $simpleArray) {
$dataArray[$simpleArray['id']] = array(
"label" => $simpleArray['label'],
"attribute_id" => $simpleArray['attr_id'],
"value_index" => $simpleArray['value'],
"is_percent" => '0',
"pricing_value" => $simpleArray['price']
);
}
// MAIN MOD! Here i prepare an array for attributesData.
$valuesArray = array();
foreach($dataArray as $data){
$valuesArray[] = $data;
}
// MAIN MOD!
// this is not the best, but in my case I've only one attribute.
$attributes_array[0]['values'] = $valuesArray;
$cProduct->setConfigurableProductsData($dataArray);
$cProduct->setConfigurableAttributesData($attributes_array);
I dont post all the codes, but i see that with these little modification it solve the problem!
I'm having the exact same issue and suspect this is due to something in Magento 1.9. I've tried a couple of code workarounds, but none of them work yet. Hmm, same bummer as you are suffering from.
Instead of providing further comments, I'll just provide an answer instead ;)
I digged into this a little bit deeper and found that the setConfigurableProductsData() call is designed to set product information, but the flag "pricing_value" simply does not work (at least it doesn't work in Magento 1.9). The call is only used to identify the Simple Products that are part of this Configurable Product. Instead the setConfigurableAttributesData() call needs to be used to define the relationship between Simple Products and pricing.
The following code worked for me: It moves the definition of specific option values (identified with value_index) with specific prices (pricing_value) as part of the attribute-data and not the product-data.
$configurableAttributeSizeValues = array();
foreach($simpleProducts as $simpleProduct) {
$configurableAttributeSizeValues[] = array(
'label' => $simpleProduct->getAttributeText('size'),
'value_index' => $simpleProduct->getSize(),
'is_percent' => false,
'pricing_value' => $simpleProduct->getPrice(),
);
}
$configurableAttributeSize = array(
'id' => null,
'label' => 'Size',
'frontend_label' => 'Size',
'attribute_id' => $attribute->getId(),
'attribute_code' => 'size',
'values' => $configurableAttributeSizeValues,
'position' => 0,
);
$configurableAttributesData = array($configurableAttributeSize);
$configurableProductsIds = array();
foreach($simpleProducts as $simpleProduct) {
$configurableProductsIds[$simpleProduct->getId()] = $simpleProduct->getId();
}
$product->setConfigurableProductsData($configurableProductsIds);
$product->setConfigurableAttributesData($configurableAttributesData);
$product->setCanSaveConfigurableAttributes(true);
Before this code is put to use, I've loaded a product-collection called $simpleProducts and a single attribute $attribute. If you want to load multiple attributes to the same product, you can by adding them to the $configurableAttributesData array.
The original $configurableProductsData that you created in your code can still be used, however because most of its purpose is moved to the attributes-array instead, you can also just create a simple array of Simple Product IDs instead. That's what I did with $configurableProductsIds.
Hope this helps you out as well.
Here is what worked for me (some code repeated for clarity). Both setConfigurableProductsData and setConfigurableAttributesData need to be used for pricing_value to be saved.
$configurableAttributesData = $product->getTypeInstance()->getConfigurableAttributesAsArray();
$product->getTypeInstance()->setUsedProductAttributeIds(array($attribute->getId()));
$product->setCanSaveConfigurableAttributes(true);
$configurableProductsData[$childId] = array(
$childProductId => array(
'label' => $childProduct->getAttributeText($attribute->getAttributeCode()),
'attribute_id' => $attribute->getId(),
'value_index' => $optionId,
'is_percent' => false,
'pricing_value' => $childProduct->getPrice()
)
);
$configurableAttributesData[0]['values'][] = array(
'label' => $childProduct->getAttributeText($attribute->getAttributeCode()),
'attribute_id' => $attribute->getId(),
'value_index' => $optionId,
'is_percent' => false,
'pricing_value' => $childProduct->getPrice()
);
$configurableAttributesData[0]['attribute_id'] = $attribute->getId();
$product->setConfigurableProductsData($configurableProductsData);
$product->setConfigurableAttributesData($configurableAttributesData);
$product->save();

Add total in magento custom module grid footer

i have added sales order grid in my custom module.and also get footer total row in the grid. but the problem is when i add 'adult' column in the total it display nothing.for adult,child,total i m using renderer to get row data from order.so how to get total for adult,child,total at the footer.
i have used below code to display footer totals in grid.php
protected $_countTotals = true;
public function getTotals()
{
$totals = new Varien_Object();
$fields = array(
'base_grand_total' => 0,
'adult'=>0,
//actual column index, see _prepareColumns()
);
foreach ($this->getCollection() as $item) {
foreach($fields as $field=>$value){
$fields[$field]+=$item->getData($field);
}
}
//First column in the grid
$fields['increment_id'] = 'Totals';
$totals->setData($fields);
return $totals;
}
Thanks
Add this function to the grid.php
public function getReport($from, $to) {
if ($from == '') {
$from = $this->getFilter('report_from');
}
if ($to == '') {
$to = $this->getFilter('report_to');
}
$totalObj = Mage::getModel('reports/totals');
$totals = $totalObj->countTotals($this, $from, $to);
$this->setTotals($totals);
$this->addGrandTotals($totals);
return $this->getCollection()->getReport($from, $to);
}
and also in adding column add 'total' => 'sum' as below (I think you made this but once verify it properly):
$this->addColumn('item_id', array(
'header' => Mage::helper('mymodule')->__('Item ID'),
'align' => 'right',
'index' => 'item_id',
'type' => 'number',
'total' => 'sum',
));

Programmatically add breadcrumbs paths in Magento?

In Magento, when the user directly accesses the product page, such as from Google, the breadcrumbs would only be "Home" -> "Product Name".
How can I add categories in there even when users access the page directly from Google?
For example, on this page, I want to add the categories "Wedding Apparel" and "Wedding Dresses" in the breadcrumbs. I came up with an idea other than hard editing breadcrumbs.phtml but is there any way I can programmatically add a breadcrumbs item in template/catalog/product/view.phtml ?
I can get the categories (title and link) of the current product and then use some function / method to add them in the breadcrumbs dynamically and programmatically. Is this possible?
Here's the code that forces Magento to display the complete breadcrumb, including categories by looping trough each category for the current product:
© Danny Vince
<?php
if ($product = Mage::registry('current_product')) {
$categories = $product->getCategoryCollection()->load();
if($categories) {
foreach ($categories as $category)
{
if($category) {
$category = Mage::getModel('catalog/category')->load($category->getId());
break;
}
}
}
$lastCrumbName = $product->getName();
$lastCategoryAdjust = 0;
}
else {
if($category = Mage::registry('current_category')) {
$lastCrumbName = $category->getName();
}
$lastCategoryAdjust = 1;
}
if($category) {
if($path = $category->getPath()) {
$path = explode('/', $path);
$crumbs = array('home' => array('label' => 'Home',
'title' => 'Home',
'link' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB),
'first' => true,
'last' => false
));
for($i = 2; $i < count($path) - $lastCategoryAdjust; $i++) {
$cur_category = Mage::getModel('catalog/category')->load($path[$i]);
if($cur_category && $cur_category->getIsActive()) {
$crumbs['category' . $path[$i]] = array('label' => $cur_category->getName(),
'title' => $cur_category->getName(),
'link' => $cur_category->getUrl(),
'first' => false,
'last' => false
);
}
}
$crumbs['current'] = array('label' => $lastCrumbName,
'title' => '',
'link' => '',
'first' => false,
'last' => true
);
}
}
?>

Resources