• Resolved smams

    (@smams)


    Hi. I am trying to conditionally run some code but need to be able to check what configuration option the customer has selected. Is there a direct way to access this data from the cart object?

    foreach( WC()->cart->get_cart() as $cart_item ) {

    ? ? ? ? $product_id = $cart_item['product_id'];

    ? ? ? ? if ( is_configurable_product( $product_id ) ) {

    ? ? ? ? ? ? // Get configuration option here

    ? ? ? ? }

    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Marc Lacroix

    (@marcusig)

    Hi there,

    The configuration is stored in the configurator_data attribute.
    You can access it like this:

    foreach( WC()->cart->get_cart() as $cart_item ) {

    $product_id = $cart_item['product_id'];

    if ( mkl_pc_is_configurable( $product_id ) && isset( ( $cart_item['configurator_data'] ) ) ) {

    foreach( $cart_item['configurator_data'] as $selected_choice ) {
    // dump the layer name
    var_dump( $selected_choice->get_layer( 'name' ) );
    // dump the choice name
    var_dump( $selected_choice->get_choice( 'name' ) );
    }
    // Alternatively, you can use the simpler configurator_data_raw data
    var_dump( $cart_item['configurator_data_raw'] );
    }

    }

    Marc

    • This reply was modified 1 month ago by Marc Lacroix. Reason: Fixed code
    Thread Starter smams

    (@smams)

    That’s brilliant. Thank you for your quick response.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.