Magento duplicating options - php

I'm generating custom options for products in magento with the following:
$options = array();
$options = array(
'title' => 'Select Options',
'type' => 'radio',
'is_require' => 1,
'sort_order' => 0,
'values' => array()
);
$options['values'][] = array(
'title' => $customAttributeString,
'price' => 0.00,
'price_type' => 'fixed',
'sku' => $uniqueId,
'sort_order' => '1'
);
$id = Mage::getModel('catalog/product')->getIdBySku($sku);
$product = Mage::getModel('catalog/product')->load($id);
if(!$product->getOptionsReadonly()) {
$product->setProductOptions(array($options));
$product->setCanSaveCustomOptions(true);
$product->save();
}
I have this running in a loop, with a different SKU everytime and when I run my loop once, it generates custom options for the first product just fine, the second product has its own custom options, and the first products custom options, and the third product has custom options for all three, etc.. could anyone give me some insight on why this is happening?

Sorry for the late response but as Magento manage the product_option as a Singleton, you need to reset it on each iteration :
Mage::getSingleton('catalog/product_option')->unsetOptions();
Hope this helps.
Guillaume

$product->setProductOptions(array($option));
Notice, that you're setting not $options but $option (without "s" at the end). Maybe it intersects with some of your variables not shown in code snippet.
Also
$options = array();
is useless here, just remove it
It would be nicer, if you include iteration cycle and initialization of $customAttributeString, $sku, $uniqueId to you code sample there.

Mage::getSingleton('catalog/product_option')->unsetOptions();
Work fine just before the loop iteration
Atif

Related

WPBakery Page Builder with taxonomy dropdown not working

I am trying to build a custom element for WPBakery Page Builder with a dropdown where custom taxonomy elements (categories) are listed.
Unfortunately I end up with empty dropdown elements. What am I doing wrong here?
The $options array gets filled perfectly when I var_dump it.
$terms = get_terms(array(
'taxonomy' => 'team',
'hide_empty' => false,
));
$options = array();
foreach($terms as $team) {
$options[$team->name] = $team->slug;
}
This is my params array inside the vc_map() function:
'params' => array(
array(
'type' => 'dropdown',
'holder' => 'div',
'class' => '',
'heading' => 'Team',
'param_name' => 'category',
'value' => $options
)
)
We had the same problem today.
After four hours fiddling around and trying everything, we finally found out that we were using a reserved word as our param_name, it was post_category, which by the way, is not on the Codex's Reserved Terms page, even though it definitely was one of them.
label also isn't on the list, but given that the error and behaviour was exactly the same, I'd advice trying to switch it for something else.

Woocommerce Rest API Product filters how it works?

I am trying to integrate the woocommerce rest APIs with my Applications. All the defaults operations like get all products, get products by category etc are working perfectly fine.
Can someone please let me know how are product filters implemented.?
bellow is my code.
$data = array(
'status' => 'publish',
'category' => '51',
'per_page' => 100,
'page' => 1,
'attribute' => "Color",
'attribute_term' => "Loft Gray"
);
$results = $woocommerce->get('products', $data)
Your question is really open ended. You don't have any code samples that show what you have working, such as when you say, "get products by category etc are working perfectly fine." What else do you need?
I can show you a couple examples, but who knows if it will help or not. I assume you already have a $woocommerce connection variable working...
Example 1:
$products = array();
$data = array(
'status' => 'publish',
'per_page' => 30,
'orderby' => 'date',
'order' => 'asc',
'featured' => 1
);
$products = $woocommerce->get('products', $data);//returns the first 30 featured products that are published, and sorts them by date
Example 2:
$results = array();
$data = array(
'status' => 'publish',
'category' => '51',
'per_page' => 100,
'page' => 1
//'filter[posts_per_page]' => '-1', //this was removed in v2 api
);
$results = $woocommerce->get('products', $data);//returns 100 published products of product category ID 51 (get this ID from your CMS)
//This can be used for pagination, since the filter functionality is removed
The API docs show you all the different properties you can access: http://woocommerce.github.io/woocommerce-rest-api-docs/?php#list-all-products
I hope this helps. If it doesn't, then please ask a specific question.

Show stock of configurable products on Magento 1.9

I'm new on Magento and I have problems to show stock of configurable product. I have tried a lot of things that I found on Google, Stackoverflow, Magento Forums, but I failed. Here is the only solution that I cannot try:
$_product is your configurable product.
To get all its simple use :
$_product->getTypeInstance(true)->getUsedProducts ( null, $_product);
So you might have something like :
foreach ($_product->getTypeInstance(true)->getUsedProducts ( null,
$_product) as $simple) {
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty();
echo $simple->getName()." with size ".$simple->getSize()." have a stock of $stock";
echo ''; } I let you adapt to your precise needs and ask question if needed
My problem is: I don't know where I can apply this solution!
()
You need to perform two step to get this working however I have checked this with version 1.8 but hope it will work in your case also
step 1. copy file app/code/core/mage/catalog/block/product/view/type/configurable.php
And create a folder under app/code/local with same directory structure mage/catalog/block/product/view/type/configurable.php
Now find function name getJsonConfig in configuration.php file.
Go to following code
$info['options'][] = array(
'id' => $value['value_index'],
'label' => $value['label'],
'price' => $configurablePrice,
'oldPrice' => $this->_prepareOldPrice($value['pricing_value'], $value['is_percent']),
'products' => $productsIndex,
);
Before this code place below two lines of code
$simpleproduct = Mage::getModel('catalog/product')->load($productsIndex);
$qty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty();
Here you can see that i'm getting product stock by product id, now in info option array code you have to add one parameter
$info['options'][] = array(
'id' => $value['value_index'],
'label' => $value['label'],
'price' => $configurablePrice,
'oldPrice' => $this->_prepareOldPrice($value['pricing_value'], $value['is_percent']),
'products' => $productsIndex,
'qty' => $qty, // we are sending stock parameter so we can use it latter in drop down field
);
step 2. Now go js/varian/configuration.js
find the following line
if(allowedProducts.size()>0){
options[i].allowedProducts = allowedProducts;
element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price), options[i].id);
Place following line under this
element.options[index].innerHTML += " Stock is :"+options[i].qty;
that's it all about cheer
Let me know if you have any query
with app/code/local/Mage/Catalog/Block/Product/View/Type/Configurable.php
$simpleproduct = Mage::getModel('catalog/product')->load($productsIndex);
$qty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty();
$info['options'][] = array(
'id' => $value['value_index'],
'label' => $value['label'],
'price' => $configurablePrice,
'oldPrice' => $this->_prepareOldPrice($value['pricing_value'], $value['is_percent']),
'products' => $productsIndex,
'qty' => $qty, // we are sending stock parameter so we can use it latter in drop down field
);
$optionPrices[] = $configurablePrice;
}
}
and at js/varien/configurable.js
if(allowedProducts.size()>0){
options[i].allowedProducts = allowedProducts;
element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price), options[i].id);
if (typeof options[i].price != 'undefined') {
element.options[index].setAttribute('price', options[i].price);
}
element.options[index].innerHTML += " Stock is :"+options[i].qty;
element.options[index].config = options[i];
index++;
}
isn't working at 1.9.4... is it something wrong?

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: 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.

Categories