Resolvedget lafkaa-addons from code

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #23190
    movidita
    Participant

    hi,

    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.

    #23248
    Alex
    Keymaster

    Hello 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,
    Alex

    #23276
    movidita
    Participant

    Hi, 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.

    #23292
    Alex
    Keymaster

    Hi,

    Ok, what error you are getting?

    Regards,
    Alex

    #23293
    movidita
    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 97

    #23294
    movidita
    Participant

    if 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.

    #23334
    Alex
    Keymaster

    Hi,

    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,
    Alex

    #23493
    movidita
    Participant

    Hi, I have it working.

    thankyou.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in and have valid license to reply to this topic.

License required for the following item
Login and Registration Log in · Register