Where can I find a complete and working example for creating BundleProducts in PHP?
I'm using Magento 1.7
Google gives me only fragments
EDIT:
until now I'm at this code, but the save throws
Fatal error: Call to a member function getStoreId() on a non-object in \app\code\core\Mage\Bundle\Model\Selection.php on line 73
-
---removed old code--
EDIT 2
Ok the exception has gone by registering the product and my bundle appears in the backend, but the bundle items do not show up in the admin panel
$items = array();
$selections = array ();
$items[] = array(
'title' => '',
'option_id' => '',
'delete' => '',
'type' => 'radio',
'required' => 'true',
'position' => 0,
);
$items[] = array(
'title' => '',
'option_id' => '',
'delete' => '',
'type' => 'radio',
'required' => 'true',
'position' => 0,
);
$selection = array();
$selection[] = array(
'selection_id' => '',
'option_id' => '',
'product_id' => '2',
'delete' => '',
'selection_price_value' => '',
'selection_price_type' => '0',
'selection_qty' => '1',
'selection_can_change_qty' => '0',
'position' => '0',
'is_default' => '1',
);
$selections[] = $selection;
$selection = array();
$selection[] = array(
'selection_id' => '',
'option_id' => '',
'product_id' => '3',
'delete' => '',
'selection_price_value' => '',
'selection_price_type' => '0',
'selection_qty' => '1',
'selection_can_change_qty' => '0',
'position' => '0',
'is_default' => '1',
);
$selections[] = $selection;
$storeID = 1;
$websiteIDs = array(1);
$product = Mage::getModel('catalog/product');
$p = array(
'sku_type' => 0,
'sku' => '123321',
'name' => "BarProduct",
'description' => 'Foo',
'short_description' => 'Bar',
'type_id' => 'bundle',
'attribute_set_id' => 4,
'weight_type' => 0,
'visibility' => 4,
'price_type' => 0,
'price_view' => 0,
'status' => 1,
'created_at' => strtotime('now'),
'category_ids' => $cats,
'store_id' => $storeID,
'website_ids' => $websiteIDs
);
$product->setData($p);
Mage::register('product', $product);
Mage::register('current_product', $product);
$product->setBundleOptionsData($items);
$product->setBundleSelectionsData($selections);
$product->setCanSaveBundleSelections(true);
$product->setCanSaveConfigurableAttributes(false);
$product->setAffectBundleProductSelections(true);
$product->setCanSaveCustomOptions(true);
$product->save();
Ok this code is working for me now:
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
$processes->walk('save');
$items = array();
$selections = array ();
$items[] = array(
'title' => 'o1',
'option_id' => '',
'delete' => '',
'type' => 'radio',
'required' => '1',
'position' => '0',
);
$items[] = array(
'title' => 'o2',
'option_id' => '',
'delete' => '',
'type' => 'radio',
'required' => '1',
'position' => '0',
);
$selection = array();
$selection[] = array(
'selection_id' => '',
'option_id' => '',
'product_id' => '2',
'delete' => '',
'selection_price_value' => '',
'selection_price_type' => '0',
'selection_qty' => '1',
'selection_can_change_qty' => '0',
'position' => '0',
'is_default' => '1',
);
$selections[] = $selection;
$selection = array();
$selection[] = array(
'selection_id' => '',
'option_id' => '',
'product_id' => '3',
'delete' => '',
'selection_price_value' => '',
'selection_price_type' => '0',
'selection_qty' => '1',
'selection_can_change_qty' => '0',
'position' => '0',
'is_default' => '1',
);
$selections[] = $selection;
$storeID = 1;
$websiteIDs = array(1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
$cats = array(3);
$p = array(
'sku_type' => 1,
'sku' => '123321',
'name' => "BarProduct",
'description' => 'Foo',
'short_description' => 'Bar',
'type_id' => 'bundle',
'attribute_set_id' => 4,
'weight_type' => 0,
'visibility' => 4,
'price_type' => 1,
'price_view' => 0,
'price' => 1.99,
'has_options' => 1,
'required_options' => 1,
'status' => 1,
'created_at' => strtotime('now'),
'category_ids' => $cats,
'store_id' => 0,
'website_ids' => $websiteIDs,
'weight' => 0,
'weight_type' => 1,
'delivery_time' => '',
'generate_meta' => 1,
'tax_class_id' => 1, //19%
);
$product->setData($p);
Mage::register('product', $product);
Mage::register('current_product', $product);
$product->setCanSaveConfigurableAttributes(false);
$product->setCanSaveCustomOptions(true);
// Set the Bundle Options & Selection Data
$product->setBundleOptionsData($items);
$product->setBundleSelectionsData($selections);
$product->setCanSaveBundleSelections(true);
$product->setAffectBundleProductSelections(true);
$product->save();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');
$processes->walk('reindexEverything');
Related
Page loading correctly and every things work fine while bringing datas. Now I want to filter them but FilterColumn() method doesnt even work. When I tried filter() method it's working but then I won't get $keywords variable. I am missing something while querying leads ?
if (request()->ajax()) {
$leads = Lead::with('country','agent','detail')->orderBy('id','DESC');
$leads->where(function($q) use ($user){
if ($user->hasRole('agent') && !$user->hasRole('admin') && !$user->hasRole('lead')){ //agent isem
$q->where('agent_id',$user->id)
->orWhere('agent_id',null);
}
});
return DataTables::of($leads)->
addColumn('edit_button', function ($lead) {
$link = route('leads.edit',$lead->id);
return ' Edit ';
})->
filterColumn('edit_button', function ($query, $keyword) {
dd($keyword);
return $query->whereHas('patient', function ($query) use ($keyword) {
$query->whereRaw("CONCAT( name, ' ', surname ) like ?", ["%$keyword%"]);
});
})->rawColumns(['edit_button','phone','detail.conversion_notes'])->
toJson();
}
$tableColumn = [
['data' => 'edit_button', 'name' => 'edit_button', 'title' => 'Edit', 'searchable' => false],
['data' => 'full_name', 'name' => 'full_name', 'title' => 'Full Name'],
['data' => 'phone', 'name' => 'phone', 'title' => 'Phone'],
['data' => 'email', 'name' => 'email', 'title' => 'Email'],
['data' => 'country.name', 'name' => 'country.name', 'title' => 'Country'],
['data' => 'agent.name', 'name' => 'agent.name', 'title' => 'Agent'],
['data' => 'treatment', 'name' => 'treatment', 'title' => 'Treatment'],
['data' => 'find_us', 'name' => 'find_us', 'title' => 'Find Us'],
['data' => 'form_type', 'name' => 'form_type', 'title' => 'Form','visible' => false],
['data' => 'social_account', 'name' => 'social_account', 'title' => 'Sosyal Medya'],
['data' => 'created_at', 'name' => 'created_at', 'title' => 'Created At'],
['data' => 'detail.conversion_notes', 'name' => 'detail.conversion_notes', 'title' => 'Primary Notes'],
];
$html = $builder->columns($tableColumn)->parameters([
"pageLength" => 25,
"lengthMenu" => [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
'dom' => 'Bfrtip',
'columnDefs' => [
['width' => '2%', 'targets' => 0],
['width' => '7%', 'targets' => 1],
'buttons' => [
'pageLength',
[
'extend' => 'colvis',
'collectionLayout' => 'fixed two-column',
'columns' => ':not(.noVis)'
]
]
]);
This is my Array Output
<pre>Array
(
[id] => 1
[leadservice] => Array
(
[minutes] => 34
)
)
<pre>Array
(
[id] => 2
[leadservice] => Array
(
[minutes] => 34
)
)
Your question is tagged with Laravel so if you turn your array into a Laravel collection you can start by grouping your items by their site_id then summing the minutes items in the leadservice items.
Something like:
collect($sitesArray)->groupBy('site_id')->map(function($site) {
return $site->map(function($siteData) {
return data_get($siteData, 'leadservice.minutes');
})->sum();
});
You can also add a third parameter to the data_get() to default any missing minutes data, presumably you'd want to default to 0
* ADD DETAILED EXAMPLE *
public function test()
{
return $this->setData()->groupBy('site_id')->map(function($site) {
return $site->map(function($siteData, $key) {
return data_get($siteData, 'leadservice.minutes');
})->sum();
});
}
public function setData()
{
return collect([
array(
'id' => 1,
'customer_lead_service_id' => 1,
'customer_id' => 94,
'site_id' => 1,
'branch_id' => null,
'start_date' => '2019-10-24',
'end_date' => '2019-10-24',
'price' => 24.00,
'status' => 'completed',
'confirmation_send_date' => null,
'confirmation_date' => null,
'assign_date' => '2019-11-21',
'assign_to' => 94,
'assign_by' => null,
'assign_note' => null,
'customer_remark' => null,
'reopen_date' => null,
'reopen_by' => null,
'reopen_note' => null,
'total_time' => 2,
'leadservice' => array(
'id' => 1,
'customer_lead_id' => 2,
'branch_id' => 10,
'service_id' => 9,
'service_title' => 'Sentinel Outlet Temperature Monitoring',
'minutes' => 34,
'quantity' => 1,
'type' => 'quarterly',
'contract_start_date' => null,
),
'documents' => array(
'0' => array(
'id' => 54,
'engineer_id' => 94,
'contract_id' => 1,
'original_name' => null,
'document' => '1574832562.mp4',
'document_type' => null,
),
),
'notes' => array(
'0' => array(
'id' => 28,
'customer_contract_id' => 1,
'engineer_id' => 94,
'note' => 'note',
),
),
),
array(
'id' => 2,
'customer_lead_service_id' => 2,
'customer_id' => 95,
'site_id' => 1,
'branch_id' => null,
'start_date' => '2019-10-24',
'end_date' => '2019-10-24',
'price' => 700.00,
'status' => 'running',
'confirmation_send_date' => null,
'confirmation_date' => null,
'assign_date' => '2019-11-21',
'assign_to' => 94,
'assign_by' => null,
'assign_note' => null,
'customer_remark' => null,
'reopen_date' => null,
'reopen_by' => null,
'reopen_note' => null,
'total_time' => null,
'leadservice' => array(
'id' => 2,
'customer_lead_id' => 3,
'branch_id' => 10,
'service_id' => 10,
'service_title' => 'HWS Vessel Temperature Monitoring Inspection',
'minutes' => 12,
'quantity' => 1,
'type' => 'monthly',
'contract_start_date' => null,
),
'documents' => array(
),
'notes' => array(
),
),
array(
'id' => 3,
'customer_lead_service_id' => 3,
'customer_id' => 96,
'site_id' => 1,
'branch_id' => null,
'start_date' => '2019-10-26',
'end_date' => '2019-10-24',
'price' => 550.00,
'status' => 'new',
'confirmation_send_date' => null,
'confirmation_date' => null,
'assign_date' => '2019-11-21',
'assign_to' => 94,
'assign_by' => null,
'assign_note' => null,
'customer_remark' => null,
'reopen_date' => null,
'reopen_by' => null,
'reopen_note' => null,
'total_time' => null,
'leadservice' => array(
'id' => 3,
'customer_lead_id' => 2,
'branch_id' => 10,
'service_id' => 11,
'service_title' => 'Hot Water Heater Temperature Monitoring',
'minutes' => 34,
'quantity' => 1,
'type' => 'six_monthly',
'contract_start_date' => null,
),
'documents' => array(
),
'notes' => array(
),
),
array(
'id' => 4,
'customer_lead_service_id' => 4,
'customer_id' => 100,
'site_id' => 2,
'branch_id' => null,
'start_date' => '2019-10-27',
'end_date' => '2019-10-24',
'price' => 24.00,
'status' => 'new',
'confirmation_send_date' => null,
'confirmation_date' => null,
'assign_date' => '2019-11-21',
'assign_to' => 94,
'assign_by' => null,
'assign_note' => null,
'customer_remark' => null,
'reopen_date' => null,
'reopen_by' => null,
'reopen_note' => null,
'total_time' => null,
'leadservice' => array(
'id' => 4,
'customer_lead_id' => 3,
'branch_id' => 10,
'service_id' => 12,
'service_title' => 'Tank Lid Installation',
'minutes' => 12,
'quantity' => 1,
'type' => 'annually',
'contract_start_date' => null,
),
'documents' => array(
),
'notes' => array(
),
),
array(
'id' => 5,
'customer_lead_service_id' => 5,
'customer_id' => 102,
'site_id' => 2,
'branch_id' => null,
'start_date' => '2019-10-24',
'end_date' => '2019-10-24',
'price' => 45.00,
'status' => 'new',
'confirmation_send_date' => null,
'confirmation_date' => null,
'assign_date' => '2019-11-21',
'assign_to' => 94,
'assign_by' => null,
'assign_note' => null,
'customer_remark' => null,
'reopen_date' => null,
'reopen_by' => null,
'reopen_note' => null,
'total_time' => null,
'leadservice' => array(
'id' => 5,
'customer_lead_id' => 3,
'branch_id' => 10,
'service_id' => 13,
'service_title' => 'Fitting of Tank Insulation',
'minutes' => 14,
'quantity' => 1,
'type' => 'annually',
'contract_start_date' => null,
),
'documents' => array(
),
'notes' => array(
),
),
]);
}
Considering your array name is $data, try below code
$arr = [];
foreach ($data as $value) {
foreach ($value['leadservice'] as $leadservice) {
if(isset($arr[$value['site_id']]['minutes']) && $arr[$value['site_id']]['minutes']!=null){
$arr[$value['site_id']]['minutes'] += $leadservice['minutes'];
}
else {
$arr[$value['site_id']]['site_id'] = $value['site_id'];
$arr[$value['site_id']]['minutes'] = $leadservice['minutes'];
}
}
}
I have magento 1.8. and i want to add product attribute in product. I can do all of that but when I want to add specific attribute to be located on top of others (sorting order in back office) in group. How can i do this?
$this->addAttribute('catalog_product', 'my_subtitle', array(
'attribute_model' => NULL,
'backend' => NULL,
'type' => 'text',
'table' => NULL,
'frontend' => NULL,
'input' => 'text',
'label' => 'Subtitle',
'frontend_class' => NULL,
'source' => NULL,
'required' => '0',
'user_defined' => true,
'default' => NULL,
'unique' => '0',
'note' => NULL,
'input_renderer' => NULL,
'visible' => '1',
'searchable' => '1',
'filterable' => '1',
'comparable' => '1',
'visible_on_front' => '1',
'is_html_allowed_on_front' => '1',
'is_used_for_price_rules' => '0',
'filterable_in_search' => '0',
'used_in_product_listing' => '1',
'used_for_sort_by' => '0',
'is_configurable' => '0',
'visible_in_advanced_search' => '0',
'position' => '0',
'wysiwyg_enabled' => '0',
'used_for_promo_rules' => '0',
'apply_to' => Mage_Catalog_Model_Product_Type::TYPE_SIMPLE,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,));
Try adding:
'sort_order' => -1, // Or 0
Ie.
$this->addAttribute('catalog_product', 'my_subtitle', array(
'sort_order' => -1, // Or 0
...
)
);
This way, your new attribute should be displayed before the rest, as per the sort_order.
I have problem with this format and I don't know does magento support it.
In code I have:
$this->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'release_date', array(
'input' => 'date',
'type' => 'datetime',
'label' => 'Release date',
'backend' => 'eav/entity_attribute_backend_datetime',
'visible' => true,
'required' => false,
'user_defined' => true,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
));
How to change type to date? And how to make it dependable from Magento locale?
Here solution....
$data = array(
"attribute_code" => "date_attribute_3",
"frontend_input" => "date",
"default_value" => "06/19/2014",
"is_unique" => 0,
"is_global"=>"1",
"is_required" => 0,
"apply_to" => array("simple"),
"is_configurable" => 0,
"is_searchable" => 0,
"is_visible_in_advanced_search" => 0,
"is_comparable" => 0,
//"is_global" =>1,
"is_used_for_promo_rules" => 0,
"is_visible_on_front" => 0,
"used_in_product_listing" => 0,
"additional_fields" => array(),
"frontend_label" => array(array("store_id" => "0", "label" => "my date"))
);
$Attribute=Mage::getModel('catalog/product_attribute_api')->create($data);
This is code that I'm using to add new product attribute with frontend settings set to yes:
<?php
$installer = Mage::getResourceModel('catalog/setup','catalog_setup');
$installer->startSetup();
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, $specCode, array(
'group' => $profileGroupName,
'sort_order' => 1,
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => $specLabel,
'note' => $specNote,
'input' => 'text',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'required' => true,
'user_defined' => true,
'default' => '',
'unique' => false,
'used_for_promo_rules' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'visible' => true,
'visible_on_front' => true,
'visible_in_advanced_search' => true,
'is_configurable' => false
));
...
Almost all the frontend settings are set to true but after installing them in backend I can see that this settings are set to no.
Regards,
Fixed. Code that is working bellow. Just keep to the $attr array key names for this method _prepareValues in this two classes Mage_Eav_Model_Entity_Setup, Mage_Catalog_Model_Resource_Setup. Second class inherits form the first one so if you want to add frontend setting your installer needs to be an object from the second class.
$installer = new Mage_Catalog_Model_Resource_Setup();
$installer->startSetup();
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, $tradeCode, array(
'group' => $profileGroupName,
'sort_order' => 1,
'type' => 'varchar',
'input' => 'text',
'label' => $tradeLabel,
'note' => $tradeNote,
'required' => 1,
'unique' => 0,
'user_defined' => 1,
'default' => '',
# Additional attribute data - forntend
'frontend_input_renderer' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => 1,
'searchable' => 1,
'filterable' => 1,
'comparable' => 1,
'visible_on_front' => 1,
'wysiwyg_enabled' => 0,
'is_html_allowed_on_front' => 0,
'visible_in_advanced_search' => 1,
'filterable_in_search' => 1,
'used_in_product_listing' => 1,
'used_for_sort_by' => 1,
'apply_to' => '',
'position' => '',
'is_configurable' => 0,
'used_for_promo_rules' => 0,
));
...