i want to add a product to cart in magento , product details is :
type = configurable
product id = 1300
product sku = FCC100
Attribute Code= color
Attribute Label= color
Attribute Id= 152
options value = 28,43
options label = blue,red
this configurable product have two color option red and blue, there is two simple product atached to this configurable product.
i try with this code :
$product = array(
"product_id" =>"1300",
"qty" => 2,
"super_attribute" => array(152 => 28));
$result = $proxy->shoppingCartProductAdd($sessionID, $cartID, array($product));
but this code return me this message :
please specify product option(s)
i use this code to add simple product and it`s work fine :
$result = $proxy->shoppingCartProductAdd($sessionID, $cartID, array(array(
'product_id' => $productID ,
'sku' => $sku ,
'qty' => $qty,
'options' =>array(0 =>array('key' => $option1id ,'value' => $option1value),1 =>array('key' => $option2id ,'value' => $option2value)),
'bundle_option' => null,
'bundle_option_qty' => null,
'links' => null
)));
my problem is with configurable product. i try to add with simple child products but problem of this way is often child products have not price and price is setted to parent product.
what is the problem in my code ? is there any way to add configurable product to cart with out using API ?
this is what i found in product page source:
Product.Config({"attributes":{"152":{"id":"152","code":"color","label":"\u0631\u0646\u06af","options":[{"id":"28","label":"\u0622\u0628\u06cc","price":"0","oldPrice":"0","products":["1301"]},{"id":"47","label":"\u0632\u0631\u0634\u06a9\u06cc","price":"0","oldPrice":"0","products":["1302"]}]}},"template":"#{price}\u00a0\u0631\u06cc\u0627\u0644","basePrice":"550000","oldPrice":"550000","productId":"1300","chooseText":"\u0627\u0646\u062a\u062e\u0627\u0628 \u06cc\u06a9 \u06af\u0632\u06cc\u0646\u0647...","taxConfig":{"includeTax":false,"showIncludeTax":false,"showBothPrices":false,"defaultTax":0,"currentTax":0,"inclTaxTitle":"\u0634\u0627\u0645\u0644 \u0645\u0627\u0644\u06cc\u0627\u062a"}});
Your array must be like below
$arrProducts = array(
array(
"product_id" =>"21",
"qty" => 2,
"super_attribute" => array(
92 => 162
)
));
Your $product array is missing the options key values.
You need to add options which should be An array in the form of option_id => content per the documentation.
$product = array(
"product_id" => "1300",
"qty" => 2,
"options" => array(
152 => 28
)
);
Documentation: http://www.magentocommerce.com/api/soap/checkout/cartProduct/cart_product.add.html
Related
Im trying to create product with custom option dropdown programatically but it doesnt work and throw error. Maybe somebody know how to do it, I we be glad to hear it. In my opinion this script work well with single field option but not for dropdown.
PHP Warning: Invalid argument supplied for foreach()
My script file at root directory
<?php
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setSku('watermelon'); // Set your sku here
$product->setName('Watermelon'); // Name of Product
$product->setAttributeSetId(4); // Attribute set id
$product->setStatus(1); // Status on product enabled/ disabled 1/0
$product->setWeight(10); // weight of product
$product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
$product->setTaxClassId(0); // Tax class id
$product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
$product->setPrice(19); // price of product
$product->setStockData(
array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'is_in_stock' => 1,
'qty' => 29
)
);
$product->save();
$options = array(
"title" => "Insurance",
"type" => "drop_down",
"is_require" => 0,
"sort_order" => 10,
"additional_fields" => array(
array(
"title" => "Not necessary",
"price" => 0,
"price_type" => "fixed",
"sku" => "not_necessary",
"sort_order" => 0
),
array(
"title" => "1 Year",
"price" => 0,
"price_type" => "fixed",
"sku" => "one_year",
"sort_order" => 1
),
array(
"title" => "2 Years",
"price" => 0,
"price_type" => "fixed",
"sku" => "two_years",
"sort_order" => 2
),
array(
"title" => "3 Years",
"price" => 0,
"price_type" => "fixed",
"sku" => "three_years",
"sort_order" => 3
),
)
);
foreach ($options as $arrayOption) {
$product->setHasOptions(1);
$product->getResource()->save($product);
$option = $objectManager->create('\Magento\Catalog\Model\Product\Option')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
}
I am doing a shopping cart using code-igniter. While I can do the add cart functions, I'm somewhat confused with how to update them with regards to database. All I want is when I change the item quantity in the cart and click update ,both the cart and database must be updated for that particular item.I tried to do it but couldn't get it.Can someone please enlighten me what to do?
controller code
$this->load->model('user_m');
$result['query'] = $this->user_m->get($id); // inserts coresponing item
foreach ($result['query'] as $row)
$id = $row->pid;
$qty = $a;
$quan=$row->quantity;
$price = $row->pprice;
$name = $row->pname;
$q=$quan-$a; // for remainig stock i.e total qty-user qty
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name,
'stock' =>$q
);
$this->cart->insert($data);
$this->load->model('user_m');
$result['qry'] = $this->user_m->up_cart($id,$q);
redirect($_SERVER['HTTP_REFERER']);
}
just tell me how to update pls!
when ever you add any product in cart, it will generate an uniq row ID and that will be unique for each and every product and hence you need to update quantity by that row ID
$data = array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
);
$this->cart->update($data);
// Or a multi-dimensional array
$data = array(
array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
),
array(
'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
'qty' => 4
),
array(
'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
'qty' => 2
)
);
$this->cart->update($data);
Now you want to know about how you will get row id.
$cart=$this->cart->contents();
it will return whole array of cart and when you listing it on webpage, please associate row id with quantity text box to update that particular product.
please check following url:
https://ellislab.com/codeigniter/user-guide/libraries/cart.html
I am creating a plugin that ties into the Runit System for showing onhand products in WooCommerce. This requires changing products via a Cron job that will update them, from Simple Products to Variable Products and vice versa depending on the quantity of stock and the variations of these products, which change. I have working code, that updates these products in the backend, showing the correct variations, however, it does not update on the front end. The new variations, even when editing Variable Products, do not show on the front end when viewing the actual Product itself. I still see the old variations in there. How to update the variations to use the new variations automatically, instead of having to hit the Update button in the backend Administration when editing the Product?
Is there some sort of function that needs to be called for updating the Variations in WooCommerce that I'm not aware of? I am using the array like this and serializing it before updating the meta_value for the meta_key _product_attributes in wp_postmeta table, like so:
function ProcessBasicProperties(Array $properties)
{
$return = array();
$position = 0;
if (!empty($properties))
{
if (!empty($properties['siz']))
{
++$position;
$size = !is_array($properties['siz']) ? array($properties['siz']) : array_unique($properties['siz']);
$return['size'] = array(
'name' => 'Size',
'value' => count($size) > 1 ? implode(' | ', $size) : $size[0],
'is_visible' => count($size) > 1 ? 0 : 1,
'is_variation' => count($size) > 1 ? 1 : 0,
'is_taxonomy' => 0,
'position' => $position
);
}
if (!empty($properties['ext']))
{
++$position;
$extension = !is_array($properties['ext']) ? array($properties['ext']) : array_unique($properties['ext']);
$return['extension'] = array(
'name' => 'Extension',
'value' => count($extension) > 1 ? implode(' | ', $extension) : $extension[0],
'is_visible' => count($extension) > 1 ? 0 : 1,
'is_variation' => count($extension) > 1 ? 1 : 0,
'is_taxonomy' => 0,
'position' => $position
);
}
// styles do not get added to attributes for variable products, instead, with variable products, the style goes into the overall sku of the product (General Properties)
// So, in short, variable products should not have this key set.
if (!empty($properties['style']))
{
++$position;
$return['style'] = array(
'name' => 'Style',
'value' => htmlspecialchars($properties['style'], ENT_QUOTES),
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
'position' => $position
);
}
if (!empty($properties['color']))
{
++$position;
$colors = !is_array($properties['color']) ? array($properties['color']) : array_unique($properties['color']);
$return['color'] = array(
'name' => 'Color',
'value' => count($colors) > 1 ? htmlspecialchars(implode(' | ', $colors), ENT_QUOTES) : htmlspecialchars($colors[0], ENT_QUOTES),
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
'position' => $position
);
}
if (!empty($properties['gender']))
{
++$position;
$return['gender'] = array(
'name' => 'Gender',
'value' => htmlspecialchars($properties['gender'], ENT_QUOTES),
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
'position' => $position
);
}
if (!empty($properties['season']))
{
++$position;
$return['season'] = array(
'name' => 'Season',
'value' => htmlspecialchars($properties['season'], ENT_QUOTES),
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
'position' => $position
);
}
}
return $return;
}
This is a function that returns the proper array that get serialized and inputted into the wp_postmeta table for the meta_value where meta_key = _product_attributes. Each variation also has a meta_key of attribute_size and meta_key of attribute_extension where the meta_value equals the value of that specific variation as required within the wp_postmeta table for that posts variation ID.
I'm at a loss, trying to update the variations of a Variable Product, or when Converting a Simple Product to a Variable Product, that needs to be updated. It shows up fine in the backend admin panel of the product, but when going to the actual product page, it still shows the OLD product variations. The only way to show the new one's is to go into the backend admin panel of the product and hit the Update button, which than shows all of the new variations. But how to do this programmatically? I thought this was possible? Must be something I'm overlooking here? Perhaps there are terms somewhere that need updating?
Functions that I have tried, that failed to update the product with the new variations, s for small = variation id 181342 and l for large = variation id 181343 from ID column of of wp_posts table or post_id column of the wp_postmeta table for that Variation:
wp_set_object_terms(181342, 's', 'size');
wp_set_object_terms(181343, 'l', 'size');
and
do_action( 'woocommerce_create_product_variation', 181342 );
do_action( 'woocommerce_create_product_variation', 181343 );
None of these update the product as a variation that shows on the front end. It still shows ONLY the old variations on the front end. How to get the size s and l showing on the front end?
Figured it out... looks like you have to update the wp_options table using set_transient... Here's what I did and it nipped it in the bud:
$transients = array(
'name' => array(
'wc_product_total_stock_' . $product_id,
'wc_product_children_ids_' . $product_id
),
'values' => array(
$total_stock,
$allVariationIDs
),
'filters' => array(
'woocommerce_stock_amount'
)
);
// Set it up so that the variations show in the front end also...
foreach($transients['name'] as $k => $transient_name)
{
set_transient($transient_name, $transients['values'][$k],YEAR_IN_SECONDS);
if (isset($transients['filters'][$k]))
apply_filters($transients['filters'][$k], $transients['values'][$k]);
}
$product_id = the actual products ID value.
$allVariationIDs = an array of all ID values of the variations within the wp_posts table. $total_stock is the new stock of the product, calculating in all variation stock levels.
Hope this helps someone else to update their variations on the front end, after setting up all wp_posts and wp_postmeta table rows for your products and variations.
Along with updating product attributes, you will also have to update the product variations from the database.
For example, while deleting the product variations, you will have to remove it from the wp_posts table
You can refer this link for more details
Woocommerce - Cannot delete a product variation
I'm trying to develop a code in php which will take the order increment Id from an old order and will create a new order with same products along with the product custom options(if any).
Fetching par is fine even it was possible to add product via querystring but by that I can't add custom options. The cart page shows : Product Custom Options Required.
I think(not sure) I even succeeded in adding products with custom options via soap api but when I redirect to cart it shows an empty cart.
I'm a beginner, so please ignore any stupidity if it exists. :D
The code with the querystring is as below:
$form_key = Mage::getSingleton('core/session')->getFormKey();
$arrProducts = array(
/*array
(
"product" => "3",
"qty" => 7
),*/
array
(
"product" => "1",
"qty" => 3,
"options" => array
(
2 => array(
"date" => "11/12/13",
"date_internal" => "2013-12-11 00:00:00"
),
1 => "1"
)
header('Location: '.Mage::getUrl('checkout/cart/add/form_key/'.$form_key.'/', $arrProduct));
So, this directs to a page with the product but asks me to chose the product options.
and when i use the other product which is commented above, its a success.
Even i'm now able to add a product without custom(product with id='3') option in the cart. code is as follows:
<?php
require('../../../app/Mage.php'); //include mage.php
Mage::app();
Mage::getSingleton('core/session', array('name' => 'frontend'));
$form_key = Mage::getSingleton('core/session')->getFormKey();
//echo $form_key;
$cart = Mage::getSingleton('checkout/cart');
$product = new Mage_Catalog_Model_Product();
$product->load(2);
//$product->setProductOptions();
$params = array(
array
(
"product" => "3",
"qty" => 1
),
array
(
"product" => "2",
"qty" => 2,
"options" =>array
(
2 => array
(
"date" => "11/12/2013",
"date_internal"=> "2013-12-11 00:00:00"
),
1 => "1"
)
)
);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
header('Location:http://111.111.1.1/magento/checkout/cart');
?>
but it doesn't work for any product with custom options(product with id='2').
Error Message :
Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Please specify the product required option(s).' in C:\xampp\htdocs\magento\app\Mage.php:594 Stack trace: #0 C:\xampp\htdocs\magento\app\code\core\Mage\Checkout\Model\Cart.php(284): Mage::throwException('Please specify ...') #1 C:\xampp\htdocs\magento\pratmagento\panel\orders\try2addtocart.php(53): Mage_Checkout_Model_Cart->addProduct(Object(Mage_Catalog_Model_Product), Array) #2 {main} thrown in C:\xampp\htdocs\magento\app\Mage.php on line 594
It is required to set website_ids for the product.
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
I have this code, which would add one bundle product with the ID of 171 to the cart, given the option of a product with the color(23) Magenta(63). Is there any way to set a quantity to the option, like say we want to set the quantity to 2 of those items with that color?
Thanks for the help.
$params = array(
'product' => 171,
'bundle_option' => array(
23 => array(
0 => 63
),
),
'qty' => 1,
);
$cart = Mage::getSingleton('checkout/cart');
$product = Mage::getModel('catalog/product');
$product->load(171);
$cart->addProduct($product, $params);
$cart->save();
Have you tried intercepting the request to addtocart and seeing what a normal post array for that bundle would look like? That should give you the structure that you need.