简体   繁体   中英

Ajax add to cart button on single product page with custom field

I am trying to edit the single product's page add to cart button. Successfully added a custom ajax call using LoicTheAztec's solution .

Code is working great. However I need to change $product_id in solution's code with product's custom field value named "custom_id".

Tried to parse it via $custom_data variable, something like:

$cart_item_key = WC()->cart->add_to_cart( $custom_data['custom_id'], $quantity, $variation_id, $variation, $cart_item_data );

Also tried to call it with another function:

function single_product_text_field() {
    global $product;

   // $cus_id = get_post_meta( $product->get_id(), 'custom_id', true );
      $cus_id = $product->get_meta( 'custom_id' );
    
    if ( ! empty($cus_id) )
        return $cus_id;
}

Sadly I had no luck. Clicking the button did not proceed the ajax request. If i replace $product_id with static custom field value, it's working great.

Unfortunately, I am out of ideas. In case you want to share one I would be at least grateful!

In my case, global $post page was declared. so $cus_id = get_post_field('custom_id', $product_id); did the trick!

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