Magento - custom product option don't show in order - magento-1.5

I'm try to add custom option to product programmatically whyle add him to cart. I'm use:
$a_options = array(
'options' => array(
'label' => 'Glove Size',
'value' => $attr_value ,
)
);
$item->addOption(new Varien_Object(
array(
'product' => $item->getProduct(),
'code' => 'additional_options',
'value' => serialize($a_options)
)
));
$quote->addItem($item);
This is shows option for product in cart and during checkout process, but don't show option in order information.
I also tried:
$item->getProduct()->addCustomOption('additional_options', $attr_value );
Try to show them via attributes - didn't help.
$params = array('product' => '1919','qty' => 1,
'options' => array(
'glove_size' => $gloves_id,
),);
$cart->addProduct('1919', $params);
Magento version is 1.5

I haven't check that in 1.5 version but the below code will work in 1.7.2 version:
For viewing the custom options you need set options in order items.That can be done through by calling an event sales_convert_quote_item_to_order_item
<sales_convert_quote_item_to_order_item>
<observers>
<jrb_setcustomoption_observer>
<type>singleton</type>
<class>jrb_setcustomoption/observer</class>
<method>salesConvertQuoteItemToOrderItem</method>
</jrb_setcustomoption_observer>
</observers>
</sales_convert_quote_item_to_order_item>
Set the details options in your observer
public function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer)
{
$quoteItem = $observer->getItem();
if ($additionalOptions = $quoteItem->getOptionByCode('additional_options')) {
$orderItem = $observer->getOrderItem();
$options = $orderItem->getProductOptions();
$options['additional_options'] = unserialize($additionalOptions->getValue());
$orderItem->setProductOptions($options);
}
}
For More details you can find in this article:
Magento - custom product option don't show in order
Thanks to Vinai

Related

How to apply extension hook in expressionengine

I'm working on expressionengine v2 and i have followed the docs at their site to make extension hooks, i've created a file in third party folder and install that from cp(control panel). Now i don't know how to call that function using hook just to show some text.
$hooks = array(
'after_channel_entry_insert' => 'after_channel_entry_insert',
'before_channel_entry_update' => 'before_channel_entry_update'
);
foreach ($hooks as $hook_name => $method_name) {
$data[] = array(
'class' => __CLASS__,
'method' => $method_name,
'hook' => $hook_name,
'settings' => serialize($this->settings),
'priority' => 10,
'version' => $this->version,
'enabled' => 'y'
);
}
//insert data in extension table
ee()->db->insert_batch('extensions', $data);
//now i want to call this function on hook
function after_channel_entry_insert($data = '') {
die('after_channel_entry_insert');
}

How to use select query for creating form in drupal 8?

I have a table in my database. I want to use the data of that table and create form of that data. I created a module and made a form.php page. Wrote select query in function but how to make form from that data?
I believe you want to create listing page of the table data. Here is how you can create a tabular listing with pagination and column sorting.
Suppose you have a table named "students" with the below fields.
id, name, email
1. Create a controller in your module in the below path.
modules/your_module/Src/Controller/StudentsController.php
<?php
namespace Drupal\your_module\Controller;
use Drupal\Core\Controller\ControllerBase;
class StudentsController extends ControllerBase {
public function __construct() {
}
public function list() {
$header = array(
array('data' => t('ID'), 'field' => 'st.id'),
array('data' => t('Name'), 'field' => 'st.name'),
array('data' => t('Email'), 'field' => 'st.email'),
);
$query = db_select('students', 'st')
->fields('st', array('id', 'name', 'email'))
->extend('Drupal\Core\Database\Query\TableSortExtender')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->orderByHeader($header);
$data = $query->execute();
$rows = array();
foreach ($data as $row) {
$rows[] = array('data' => (array) $row);
}
$build['table_pager'][] = array(
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
);
$build['table_pager'][] = array(
'#type' => 'pager',
);
return $build;
}
}
So your controller action is ready, now you have to add routing in order to create path to this listing page.
2. Create routing.yml file inside your module folder as the file name mentioned below, and the code below.
modules/your_module/your_module.routing.yml
students.list:
path: 'admin/config/students/list'
defaults:
_controller: 'Drupal\your_module\Controller\StudentsController::list'
_title: 'Students List'
requirements:
_permission: 'access students list'
3. To create permissions, you can create the followin ing file in your module.
modules/your_module/your_module.permissions.yml
access students list:
title: 'Access students list page'
Clear CMS cache
Go to People => Permissions, Enable the permission for relevant user roles.
Then, browse your page "admin/config/students/list"

Adding WooCommerce Attribute via PHP code

I could see a lots of question on adding woocommerce product attribute. But doing so, I could see those added attributes in this screen
wp-admin/edit.php?post_type=product&page=product_attributes
My question is how can we add the attributes via PHP code to so that it appears in this above specified woocommerce attribute screen ? My intention is to make these attributes visible under 'Layered Navs' widget to filter out.
Following code will create the attribute programmatically which will be visible on the product_attributes page in the backend.
global $wpdb;
$insert = $wpdb->insert(
$wpdb->prefix . 'woocommerce_attribute_taxonomies',
array(
'attribute_label' => 'name',
'attribute_name' => 'slug',
'attribute_type' => 'type',
'attribute_orderby' => 'order_by',
'attribute_public' => 1
),
array( '%s', '%s', '%s', '%s', '%d' )
);
if ( is_wp_error( $insert ) ) {
throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_attribute', $insert->get_error_message(), 400 );
}
// Clear transients
delete_transient( 'wc_attribute_taxonomies' );
Change name, slug etc with the relevant values.

How to create a menu from sub pages of a menu item AND use attributes/class on ul?

I understood how to create a menu from the child elements of a specific page (e.g. id=5) like this:
$pages = \Ip\Menu\Helper::getChildItems($pageId = 5);
echo ipSlot('menu', $pages);
Works so far. Just the formatting is due to the lack of Bootstrap classes enhancable.
But how can I add custom classes to the ul of this menu?
Normally I do this like this:
$options = array(
'items' => 'menu2',
'attributes' => array('class' => 'nav nav-stacked')
);
echo ipSlot('menu', $options);
But how can I combine these two methods?
All in formation is in documentation - https://www.impresspages.org/docs/navigation ("ADD CUSTOM MENU ITEMS").
items variable supports menu objects, too.
In your case the final solution should look like this:
$pages = \Ip\Menu\Helper::getChildItems($pageId = 5);
$options = array(
'items' => $pages,
'attributes' => array('class' => 'nav nav-stacked')
);
echo ipSlot('menu', $options);

Restrict sonata_type_model_list to 1 context

I am trying to allow users to select from existing images in the system and have context set like the provided example and this works. How can I stop the users from selecting from any other context type?
$form->add('image', 'sonata_type_model_list', array(), array('link_parameters' => array('context' => 'news')))
Not tested but I just stumbled over the parameter: hide_context
$form->add('image', 'sonata_type_model_list', array(), array(
'link_parameters' => array(
'context' => 'news',
'hide_context' => true
)
))
Maybe you want to give it a try?

Resources