简体   繁体   中英

WooCommerce Custom Fields through add_to_cart

I have a site where a user can spell out their name and we create a framed picture using photos of each letter. I created a variation product in woocommerce and added a custom field "Description" with value "variable"

I'd like to have the user click a link after spelling out their name and use

$woocommerce->cart->add_to_cart( $product_id );

to add it to the cart. The thing is, I need to attach the word they spelled out to the custom field "Description" that I made so we know what word they want spelled out. Is their a way to do this dyanmically with the add_to_cart function? I've googled like crazy and can't figure it out, not even sure if custom fields work this way. I don't even see a field labeled "Description" on the product page even though I created that custom field.

Thanks for any and all suggestions.

Further to your query on how to register custom fields, Gerhard Potgieter has a useful post on creating Custom Product Fields at:

http://gerhardpotgieter.com/2013/09/17/woocommerce-custom-product-fields/

I have amended his code to have a Expected Delivery field in the Shipping tab.

I'm not sure if i understood you well, but here's something you can try.

First, you must edit the add_to_cart function signature so it can ALSO receive the new parameter.

public function add_to_cart( $product_id, $new_parameter,$quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() )

Here i put the $new_parameter as mandatory, but it's up to you.

Second, create the code that inputs the new value to the custom field. Normally you can use the update_post_meta function

update_post_meta($product_id,"description",$new_parameter); /* Considering that your custom field slug is "description" */

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM