Resolvedget lafkaa-addons from code
- This topic has 7 replies, 2 voices, and was last updated 3 years, 3 months ago by
movidita.
- AuthorPosts
- May 27, 2020 at 6:35 pm #23190
movidita
Participanthi,
I have made a custom options with lafka add-ons in some products.
How can I get the options that customers have choosen from $the_order in functions.php?
thank you.
May 29, 2020 at 12:14 pm #23248Alex
KeymasterHello movidita,
You can get the addons on a product level, using this function:
lafka_get_product_addons($product_id)
So, when you have $the_order, you can loop through $the_order->get_items() (this will give you all the products in the order) and run lafka_get_product_addons on each product.
Something like this:$addons = array(); /** @var WC_Product $product */ foreach($the_order->get_items() as $product) { $addons[]= array($product => lafka_get_product_addons($product->get_id())); }
Regards,
AlexJune 1, 2020 at 10:08 am #23276movidita
ParticipantHi, I have this but no works:
global $the_order, $post;
$addons = [];
// Loop through order items
foreach ( $the_order->get_items() as $item ) {
$addons[] = $item->get_id();
}foreach ( $addons as $add ) {
//var_dump($add);
//var_dump(lafka_get_product_addons($add));
}i get an error and wordpress chash.
Could you help me?
Thankyou.
June 1, 2020 at 12:15 pm #23292Alex
KeymasterHi,
Ok, what error you are getting?
Regards,
AlexJune 1, 2020 at 12:25 pm #23293movidita
Participant$addons = [];
foreach ( $the_order->get_items() as $item ) {$addons[] = $item->get_id();
}
foreach ( $addons as $add ) {
var_dump($add);
var_dump(lafka_get_product_addons($add));
}int(227)
Fatal error: Uncaught Error: Call to a member function get_id() on bool in /var/www/vhosts/pepeentucasa.es/httpdocs/wp-content/plugins/lafka-plugin/incl/addons/lafka-product-addons.php:97 Stack trace: #0 /var/www/vhosts/pepeentucasa.es/httpdocs/wp-content/themes/lafka-child/lafka-child/functions.php(173): lafka_get_product_addons(227) #1 /var/www/vhosts/pepeentucasa.es/httpdocs/wp-includes/class-wp-hook.php(287): custom_orders_list_column_content(‘my-column1’, 4965) #2 /var/www/vhosts/pepeentucasa.es/httpdocs/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array) #3 /var/www/vhosts/pepeentucasa.es/httpdocs/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #4 /var/www/vhosts/pepeentucasa.es/httpdocs/wp-admin/includes/class-wp-posts-list-table.php(1265): do_action(‘manage_shop_ord…’, ‘my-column1’, 4965) #5 /var/www/vhosts/pepeentucasa.es/httpdocs/wp-admin/includes/class-wp-list-table.php(1361): WP_Posts_List_Table->column_default(Object(WP_Post), ‘my-column1’) #6 /var/www/vhosts/pepeentucasa.es/httpd in /var/www/vhosts/pepeentucasa.es/httpdocs/wp-content/plugins/lafka-plugin/incl/addons/lafka-product-addons.php on line 97June 1, 2020 at 12:41 pm #23294movidita
Participantif I do: lafka_get_product_addons($item->get_product_id())
I get all options of products but no what options a customer has choosen of the products in the order.
Thank you.
June 3, 2020 at 11:14 am #23334Alex
KeymasterHi,
The error means that get_items() returns nothing, you need to check it before usage.
On the other point you are right. So in order to get the ordered addons for specific item, within the loop call:
$item->get_formatted_meta_data()
And look at the get_formatted_meta_data() definition in WooCommerce.
Regards,
AlexJune 9, 2020 at 2:32 pm #23493movidita
ParticipantHi, I have it working.
thankyou.
- AuthorPosts
You must be logged in and have valid license to reply to this topic.