• Resolved vincej

    (@vincej)


    Want to add a custom field or attribute (have info both ways)
    simple product.
    on category page want add to cart button to include info like “genre:Rock” and add it to cart page so that it carries over to checkout/order pages.

    I have been able to do it with the single product page ok, but cannot figure it out for the category page.

    any suggestions? I would guess I need to somehow modify the woocommerce/loop/add-to-cart.php file. When I add the field there, it does show it in the HREF for the button… but does not carry over into the cart nor order pages.

    is there any way to get this to change so that it accepts the custom field info and carries it forwards?

    
    

    echo apply_filters(
    ‘woocommerce_loop_add_to_cart_link’, // WPCS: XSS ok.
    sprintf(
    %s‘,
    esc_url( $product->add_to_cart_url() ),
    esc_attr( isset( $args[‘quantity’] ) ? $args[‘quantity’] : 1 ),
    esc_attr( isset( $args[‘class’] ) ? $args[‘class’] : ‘button’ ),
    isset( $args[‘attributes’] ) ? wc_implode_html_attributes( $args[‘attributes’] ) : ”,
    esc_html( $product->add_to_cart_text() )
    ),
    $product,
    $args
    );`
    `

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there ??

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers!

    laceyrod

    (@laceyrod)

    Automattic Happiness Engineer

    Hi @vincej

    This thread has been inactive for a bit, so I’m going to mark it as Resolved now for the overall health of the forums. Please check out the resources we referenced above for additional assistance. If you aren’t able to get the help you need, I can also recommend the following paid support for help: https://woocommerce.com/customizations/

    Cheers!

    Thread Starter vincej

    (@vincej)

    actually. found my solution. a simple add to my functions.php file
    thanks.

    // Changing the cart item name
    add_action( ‘woocommerce_before_calculate_totals’, ‘customizing_cart_items_name’, 20, 1 );
    function customizing_cart_items_name( $cart ) {

    if ( is_admin() && ! defined( ‘DOING_AJAX’ ) )
    return;

    if ( did_action( ‘woocommerce_before_calculate_totals’ ) >= 2 )
    return;

    // Loop through each cart items
    foreach ( $cart->get_cart() as $cart_item ) {
    // Continue if we get the custom ‘sample_name’ for the current cart item
    if( empty( $cart_item[‘sample_name’] ) ){
    // Get an instance of the WC_Product Object
    $product = $cart_item[‘data’];
    // Get the product name (Added compatibility with Woocommerce 3+)
    $product_name = method_exists( $product, ‘get_name’ ) ? $product->get_name() . “<br> – ” .get_post_meta( $product->get_id(), ‘genre’, true ) : $product->post->post_title . “<br> – ” . get_post_meta( $product->get_id(), ‘genre’, true );
    // The new string composite name
    $product_name .= ‘ (‘ . $cart_item[‘sample_name’] . ‘)’;

    // Set the new composite name (WooCommerce versions 2.5.x to 3+)
    if( method_exists( $product, ‘set_name’ ) )
    $product->set_name( $product_name );
    else
    $product->post->post_title = $product_name;
    }
    }
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘add attribute or custom field info to add to cart buttonon category page’ is closed to new replies.