0

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!

newbie
  • 43
  • 5

1 Answers1

0

In my case, global $post page was declared. so

$cus_id = get_post_field('custom_id', $product_id);

did the trick!

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
newbie
  • 43
  • 5