Magento custom options: success.. almost! - php

Alright, so I'm working on my own script that reads specific xml files, and imports products. Now, each of these products may have a set of attributes, (i.e. color for example).
So, what I'm doing is importing each custom attribute, and generating a specific string, "Yellow, black finish wood" for example, and that would be a radio button you could select from when you go to order the product. Now, when I come to a product that has the same "parentid" but a different unique id, I need to generate a new row for the custom option radio button, so if there is another product where the color is red, I would have the following to select from:
Red, black finish wood
Yellow, black finish wood
This is working great for the first product with the same parent id, in other words, if I'm creating a brand new product, and not just adding custom options to the same product.
My question is, how do I generate a new custom option value, for the specific option? In other words, how do I add a new row to a certain option that is already created?
Here is basically what I'm doing to generate the first option on a new product.
$options = array();
$options[$ParentID] = array(
'title' => 'Option Title',
'type' => 'radio',
'is_require' => 1,
'values' => array()
);
$options[$ParentID]['values'][] = array(
'title' => 'Option Value Title',
'price' => 0.00,
'price_type' => 'fixed',
'sku' => $UniqueID,
);
foreach($options as $ParentID => $option) {
$id = Mage::getModel('catalog/product')->getIdBySku($ParentID);
$product = Mage::getModel('catalog/product')->load($id);
if(!$product->getOptionsReadonly()) {
$product->setProductOptions(array($option));
$product->setCanSaveCustomOptions(true);
$product->save();
}
}
this works out great, because magento generates an sku when they order a certain product in the format of $ParentID_$UniqueID, and then I can send the $UniqueID off to the vendor for the order.
So again, I'm open to suggestions as to how to add a Option Value to an already created Custom Option

load all options in and create array map with unique and already existing options to get their id -s in place

Related

Add composite product to order programmatically in Woocommerce

I am struggling to add a composite product to to WC orders programatically. I have been going through the source code to try and figure this out, but keep running into errors.
What I am doing right now is creating at instance of WC_CP_Order to get access to the method add_composite_to_order($composite, $order, $quantity, $args)
$wc_cp = new WC_CP_Order(); // Create instance of WC_CP_Order
$composite_product = wc_get_product('39082'); // Get the composite product
$order = wc_get_order(39250); // Order that I want to add the composite product to
$args = array(
'configuration' => array(
1620281459 => array( // ID of the component.
'quantity' => 1, // Qty of composited product, will fall back to min.
'discount' => 0, // Composited product discount, defaults to the defined value.
'attributes' => array(), // Array of selected variation attribute names, sanitized.
'variation_id' => 39114, // ID of chosen variation, if applicable.
'args' => array() // Custom composited item args to pass into 'WC_Order::add_product()'.
)
)
)
$wc_cp->add_composite_to_order($composite_product, $order, 1, $args);
This configuration returns a 500 error when I trigger this function (this is just all of the code inside the function).
I am probably just overlooking something very simple, but so far am unable to find it. There are the docs that I have been using: https://docs.woocommerce.com/document/composite-products/. Any help or suggestions would be greatly appreciated!!

How to create a custom order to display posts and taxonomies

i'm a music producer and new to coding. I'm working on my website and i have created a custom taxonomy called "genre" which is linked to a custom post type called "my-music". below is the code i used for the options when adding genres to display on a page. please see the attached image for reference. athis image (https://drive.google.com/file/d/1liovGGOnjgx-IxjMklk1ZVNyyCaBpqXF/view?usp=sharing).
<?php if (!defined('FW')) die('Forbidden');
`$options = array(
'genre_heading' => array(
'label' => __('Title', 'miraculous'),
'type' => 'text'
),
'number_limits' => array(
'label' => __('Number of genre', 'miraculous'),
'type' => 'text'
),
);
The above code and attached image then display the created values under the genre taxonomy. please this image for reference. ( https://drive.google.com/file/d/191ArcB0y1sh9vuKynkknHrhP7GOCJ7Sv/view?usp=sharing ). by Default it display the genres by alphabetical order, i would like to add an option to the above codes called display order which should include 3 select options 1. Most Played 2.Alphabetical Order 3.Top Rated.
The "my-music' custom post type already has a function called (miraculous_song_view_counter) which counts how many times a song has been played. this is the code i used to create it ( https://drive.google.com/file/d/1kbcHW476CvFH5xIsjsXT0PIDtUIgLOVE/view?usp=sharing ). and i'm using Unyson feedback extension for rating posts. Is there a way to use the data collected from this rating submissions and music count function to create those display order options?

WooCommerce custom child products to variable products

I have some variable products (different 3-4 variations) with different prices. I want to add some simple products as child products (show as optional checkbox in front). I don't know how to manage that for adding to the cart.
For example let's assume I have a cellphone as main product and when user added it to it's cart it should see 3 sub product (child product) :
Handsfree (in three type of color)
Glass guard (in two type of color )
cell phone shield case ( just black)
as you can understand from above example I wanna to:
add all of this items as one product (includes handsfree, glass guard , cellphone case and cellphone itself ) and calculate it's price .I've read Add a checkbox on single product pages that adds an additional cost in Woocommerce and after that I have codes below to adding options in product edit page.
//custom product tab to woocommerce edit
function sm_wccpd_custom_product_tabs($tabs)
{
$tabs['sm_wcpa_extra_products'] = array(
'label' => __('Samanik Extra Products', 'sm-wcpa'),
'target' => 'sm_wc_extra_products_options',
'class' => array('show_if_variable'),
);
return $tabs;
}
add_filter('woocommerce_product_data_tabs', 'sm_wccpd_custom_product_tabs');
and here is tab content
function extra_products_options_product_tab_content()
{
global $post;
// Note the 'id' attribute needs to match the 'target' parameter set above
?>
<div id='sm_wc_extra_products_options' class='panel woocommerce_options_panel'><?php
?>
<div class='options_group'><?php
woocommerce_wp_checkbox(
[
'id' => '_has_extra_products',
'label' => __('has extra products', 'sm-wcpa'),
]);
woocommerce_wp_select(
[
'class' => 'multiselect attribute_values wc-enhanced-select',
'custom_attributes' => ['multiple' => 'multiple', 'style' => 'width:100% !importan;'],
'id' => '_sm_wcpa_product[]',
'label' => __('Extra Products to be add here', 'sm-wcpa'),
'value' => array_values(json_decode(get_post_meta($post->ID, '_sm_wcpa_product'), true)),
'options' => sm_wcpa_get_products_as_options(),//this is function that I pass Products as array like [product_id => product name]
]);
?></div>
</div><?php
}
// add_filter('woocommerce_product_data_tabs', 'extra_products_options_product_tab_content'); // WC 2.5 and below
add_filter('woocommerce_product_data_panels', 'extra_products_options_product_tab_content'); // WC 2.6 and up
also I can save the data and I think they are not necessary here.
now what I need to do is how to manage this in front. I mean show child products as checkbox, add price to parent price if checked, what about changing variables(those have different prices).
thanks for your patient to read my question.

Show product on main page by id opencart

I need to show one certain product on front page. I didn't find a module for this. What do I need to do? I need to show product with countdown timer but I can do this, I only do not know how to get product from database by ID or SKU. I guess the code should be like this:
echo $product_option_data[] = array(
'product_option_id' => $product_option['product_option_id'],
'option_id' => $product_option['option_id'],
'name' => $product_option['name'],
'type' => $product_option['type'],
'option_value' => $product_option['option_value'],
'required' => $product_option['required']
);
In OpenCart controller you can call
$this->load->model('catalog/product'); // only if not yet 'loaded' within the controller
$product = $this->model_catalog_product->getProduct($productId);
print_r($product);
which will return you the product by it's ID. Do with it whatever you need afterwards.

Add selection to bundle programmatically

on import I would like to add imported products as bundled products to a list of bundle products.
I extended Mage_Catalog_Model_Convert_Adapter_Product and used this class in a custom Dataflow, before the imported row is saved the following code is executed:
//Load product model collection filtered by attribute set id
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('attribute_set_id', 12);
// loop through products
foreach($products as $p){
// get product options
$options = $p->getTypeInstance(true)->getOptionsCollection($p);
}
What I need to do next is determine what is the right option (by title), and add the product as a selection to it.
$option->addSelection($selection);
But how do I get the option title and how do i create the selection from my product?
To get the option title i did:
$option->getData('default_title');
To create the selection i did:
$selection = new Mage_Bundle_Model_Selection();
$selection->addData(array(
'entity_id' => //bundle id,
'option_id' => $option->getId(),
'product_id' => //bundled item id,
'selection_price_value' => 0.00,
'selection_qty' => 1,
'selection_can_change_qty' => 0,
));
$selection->save();
to add the selection to the option i did:
$option->addSelection($selection);
$option->addData(array(
'store_id' => 1,
'title' => 'Abonnement'
));
$option->save();
I don't know why i have to set the title and store_id, because the option already exists so i think it shouldn't be necessary, but if i don't do this i get a "missing store_id" or "missing title" error.
I like Magento but it's huge, documentation should be better.

Categories