I am using the following code to get and process the custom options
$orderIncrementId = $order->getIncrementId();
$orderReal = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$orderItems = $orderReal->getAllItems();
$sk = "";
foreach ($orderItems as $item)
{
$options = $item->getProductOptions();
foreach ($options as $option) {
if (is_array($option)) {
$firstElement = array_shift($option);
foreach ($option as $firstElement){
if (isset($firstElement['label']) && isset($firstElement['option_value'])){
if ($firstElement['label'] == 'Delivery Date') {
$deliveryDate = $firstElement['option_value'];
}
}
}
}
}
}
and my usage is to write it to CSV like this
if (isset($deliveryDate)) {
$outputString.= '"'.$deliveryDate.'",';
} else {
$outputString.= '"deliverydate error",';
}
now, I have made sure that
Label Delivery Date is available in data base "Catalog Product Option Title Table"
custom option Delivery Date has values in "Catalog Product Option
Table"
it looks like Delivery date is not being accessed via the script so my custom message 'deliverydate error' saves in CSV
in fact I am trying to debug mag extension that is not working properly...
to me, code is okay, but not sure why its not working....
can anybody guide me?
While it's not the perfect solution, this will return your custom option value for the ordered item, assuming that your product custom option is a type "field".
foreach ($orderItems as $item) {
$optionsArray = $item->getProductOptions();
if(isset($optionsArray['options'])) {
foreach ($optionsArray['options'] as $option) {
if($option['label'] == 'Delivery Date') {
$deliveryDate = $option['value'];
}
}
}
}
Related
I would like to ask you for some help or even just a comparison.
On my site I have bundles that are dimanically priced with select products, some with radio and some with default.
Magento 2 however shows two different prices between listing and product page. I have checked at code level and on the product page there is a manipulation via js that changes the price. In fact when the page loads you see a price before the update. Have you ever implemented some solution to make the same prices appear? This is the piece of code I am trying to implement but without success as it returns the same price because the products are set to default even though they are not selected on the product page.
$product = $this->productRepository->get($product_sku);
$selectionCollection = $product->getTypeInstance(true)
->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product),
$product
);
$children_ids = $this->bundleType->getChildrenIds($product->getId(), true);
$bundle_items = [];
$optionsCollection = $product->getTypeInstance(true)
->getOptionsCollection($product);
foreach ($optionsCollection as $options) {
$optionArray[$options->getOptionId()]['option_title'] = $options->getDefaultTitle();
$optionArray[$options->getOptionId()]['option_type'] = $options->getType();
if ($options->getType() !== 'checkbox' && $options->getRequired() === "1") {
foreach ($selectionCollection as $selection) {
foreach ($children_ids as $bundle_id) {
if ((array_values($bundle_id)[0] === $selection->getEntityId())
&& $options->getId() === $selection->getOptionId()) {
$price = $selection->getPrice();
$qty = $selection->getSelectionQty();
$bundle_items[] = $price * $qty;
}
}
}
}
}
$finale_price = array_sum($bundle_items);
I finally solved it by creating a function that would return the tier price. Mine is a bit more customised because I have different prices depending also on the customer group. I'll leave the code.
public function getBundlePrice($product_sku, $product_sal)
{
$product = $this->productRepository->get($product_sku);
$selectionCollection = $product->getTypeInstance(true)
->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product),
$product
);
$children_ids = $this->bundleType->getChildrenIds($product->getId(), true);
$bundle_items = [];
$optionsCollection = $product->getTypeInstance(true)
->getOptionsCollection($product);
foreach ($optionsCollection as $options) {
$optionArray[$options->getOptionId()]['option_title'] = $options->getDefaultTitle();
$optionArray[$options->getOptionId()]['option_type'] = $options->getType();
if ($options->getType() !== 'checkbox' && $options->getRequired() === "1") {
foreach ($selectionCollection as $selection) {
foreach ($children_ids as $bundle_id) {
if ((array_values($bundle_id)[0] === $selection->getEntityId())
&& $options->getId() === $selection->getOptionId()) {
$price = $selection->getPrice();
$qty = $selection->getSelectionQty();
if ($qty > 1 || $selection->getTierPrice()) {
$price = $this->getCustomPrice($selection);
} else if ($this->customerSession->getCustomer()->getGroupId() === "2") {
$price = $selection->getPartnerPrice();
}
$bundle_items[] = $price * $qty;
}
}
}
}
}
return $this->calculator->getAmount(array_sum($bundle_items), $product_sal);
}
I need to get the SKU for a simple product that is part of a configurable in Magento.
Something along the lines of:
if($_product is simple) {
echo $_product->getSku();
}
elseif($_product is configurable) {
$simples = $_product-> //Get simple products;
foreach($simples as $simple) {
echo $simple->getSku();
}
}
Also how can I check for the product type?
Thanks.
Magento 1
Did not tested but should work:
$product = Mage::getModel('catalog/product')->load(1);
if ($product->isSimple()) {
echo $product->getSku();
} else {
$childProducts = Mage::getModel('catalog/product_type_configurable')
->getUsedProducts(null,$product);
foreach ($childProducts as $childProduct) {
echo $childProduct->getSku();
}
}
You can use:
if ($product->getTypeId() == "configurable")
{
//Your code here
}
or
if ($product->isConfigurable())
{
//Your code here
}
To check type of product.
Magento 2
/** #var Magento\Catalog\Model\Product $configurableProduct */
$simpleProducts = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
foreach ($simpleProducts as $product) {
echo "Product Sku: " . $product->getSKU();
}
Using Magento 1.8.1, on the checkout page, I'm trying to add a product to the cart in the code. Here is the code I'm using:
$totals = Mage::getSingleton('checkout/cart')->getQuote()->getTotals();
$subtotal = $totals["subtotal"]->getValue();
$free_samples_prod_id = 1285;
if ( $subtotal >= 50 ) {
$this->addProduct($free_samples_prod_id, 1);
}
else{
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getId() == $free_samples_prod_id) {
$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId)->save();
break;
}
}
}
The code is in: app\code\core\Mage\Checkout\Model\Cart.php under public function init()
The product does get added sucessfully, however, everytime somebody visits their cart page, the quantity increases by one. How can I modify that code so the quantity is always 1?
Thank you
Axel makes a very good point, but to answer your immediate question, why not test for the product's presence before you add it
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
$subtotal = $totals["subtotal"]->getValue();
$free_samples_prod_id = 1285;
if ( $subtotal >= 50 ) {
$alreadyAdded = false;
foreach ($items as $item) {
if($item->getId() == $free_samples_prod_id) { $alreadyAdded = true; break; }
}
if(!$alreadyAdded) { $this->addProduct($free_samples_prod_id, 1); }
}
I cannot seem to get the custom options field to display in my magento code below - can any of you good people help me out?
public function getProductName($product)
{
$value = '<b>'.$product->getname().'</b>';
if ($product->getproduct_type() == 'configurable')
{
//add sub products
$collection = mage::getModel('sales/order_item')
->getCollection()
->addFieldToFilter('parent_item_id', $product->getitem_id());
foreach ($collection as $subProduct)
{
$value .= '<br><i>'.$subProduct->getname().'</i>';
//add product configurable attributes values
$attributesDescription = mage::helper('ProductReturn/Configurable')->getDescription($subProduct->getproduct_id(), $product->getrp_product_id());
if ($attributesDescription != '')
$value .= '<br>'.$attributesDescription;
}
}
return $value;
}
There are issue in $product->getproduct_type() is not wrong it should try it $product->getTypeId()
I am trying to create a Asos style header on my Magento category page.
In this box I have pulled in the category title and the category description, I am also some how needing to pull in a specific attribute from the layered navigation into the category view.phtml page.
At the moment I have
<?php $prod = Mage::getModel('catalog/product')->load($productId);
$att = $prod->getResource()->getAttribute('product')->getFrontend()->getValue($prod);
echo $att;
?>
But it is just pulling in the word No instead of the attributes that it is showing in the layered navigation for this specific category.
Try this:
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
Thanks to #Daniel Kocherga for the original answer here.
From code/core/mage/catalog/block/product/view/attributes.php
public function getAdditionalData(array $excludeAttr = array())
{
$data = array();
$product = $this->getProduct();
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
// if ($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
$value = $attribute->getFrontend()->getValue($product);
if (!$product->hasData($attribute->getAttributeCode())) {
$value = Mage::helper('catalog')->__('N/A');
} elseif ((string)$value == '') {
$value = Mage::helper('catalog')->__('No');
} elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
$value = Mage::app()->getStore()->convertPrice($value, true);
}
if (is_string($value) && strlen($value)) {
$data[$attribute->getAttributeCode()] = array(
'label' => $attribute->getStoreLabel(),
'value' => $value,
'code' => $attribute->getAttributeCode()
);
}
}
}
return $data;
}
}
Pretty sure this is the responsible section for displaying a 'No' or 'N/A' with your attributes
I am not sure but see below URL. I think it is help full to you.
How to add attributes to product grid in category
http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/add-attributes-to-product-grid
Magento Attributes: Using Different Filterable Attributes Per Category
http://www.human-element.com/Blog/ArticleDetailsPage/tabid/91/ArticleID/68/Magento-Attributes-Using-Different-Filterable-Attributes-Per-Category.aspx
Get Data for Magento Category Attribute on Product Page
http://spenserbaldwin.com/magento/get-data-for-new-magento-category-attribute