![](/img/trans.png)
[英]Add custom "Add to cart" button based on ACF field to WooCommerce single product page
[英]Ajax add to cart button on single product page with custom field
我正在嘗試編輯單個產品的頁面添加到購物車按鈕。 使用 LoicTheAztec 的解決方案成功添加了自定義 ajax 調用。
代碼運行良好。 但是,我需要使用名為“custom_id”的產品自定義字段值更改解決方案代碼中的 $product_id。
試圖通過 $custom_data 變量解析它,例如:
$cart_item_key = WC()->cart->add_to_cart( $custom_data['custom_id'], $quantity, $variation_id, $variation, $cart_item_data );
還嘗試用另一個 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;
}
可悲的是我沒有運氣。 單擊該按鈕未執行 ajax 請求。 如果我將 $product_id 替換為 static 自定義字段值,則效果很好。
不幸的是,我沒有想法。 如果你想分享一個,我至少會感激不盡!
就我而言,聲明了全局 $post 頁面。 所以$cus_id = get_post_field('custom_id', $product_id);
成功了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.