admin管理员组

文章数量:1134597

I'm working on a website that is using woocommerce and yith to add product variations. When the customers wants to buy something, he will have to choose between some product attributes, like the size, etc...

When the customer has finished his purchases, he must fill out a form which will be sent by email to the owner of the site, who will then contact him.

the problem is that, in this said email, there are no product variations (size, etc.). Simply the image, product name and quantity.

We are using the "[yith-request-a-quote-list]" to show in the email, the product list.

The following is my product variations and the email the owner receive.

I'm working on a website that is using woocommerce and yith to add product variations. When the customers wants to buy something, he will have to choose between some product attributes, like the size, etc...

When the customer has finished his purchases, he must fill out a form which will be sent by email to the owner of the site, who will then contact him.

the problem is that, in this said email, there are no product variations (size, etc.). Simply the image, product name and quantity.

We are using the "[yith-request-a-quote-list]" to show in the email, the product list.

The following is my product variations and the email the owner receive.

Share Improve this question asked Sep 25, 2023 at 15:11 sorrowsorrow 31 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can try this. First, make sure to start a session. Just add this to your theme's functions.php:

function start_session() {
    if(!session_id()) {
        session_start();
    }
}
add_action('init', 'start_session', 1);

Next, hook into WooCommerce to capture the selected attributes:

function capture_selected_attributes($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    if($variation_id && is_array($variation)) {
        $_SESSION['product_attributes'] = $variation;
    }
}
add_action('woocommerce_add_to_cart', 'capture_selected_attributes', 10, 6);

Then, in your contact form, add a hidden field:

[hidden product-attributes default:shortcode-attr]

Now, use a custom shortcode to retrieve the attributes:

function retrieve_product_attributes() {
    return isset($_SESSION['product_attributes']) ? implode(", ", $_SESSION['product_attributes']) : '';
}
add_shortcode('shortcode-attr', 'retrieve_product_attributes');

Lastly, in the email template of your contact form, make sure you add the new field:

Product Attributes: [product-attributes]

This way, whenever the form is sent, the product attributes should be included in the email.

本文标签: woocommerce offtopicHow to implement my product variationsattributes in my contact form 7 email