1
我無法凌駕於第二次購買價格。設置WooCommerce車項目價格爲零,如果該產品已經買
使用情況是:如果用戶已經購買了產品的「944」,那麼價格會爲下一次訂單爲0。
含義,客戶將僅支付特定產品的一階,這將是免費爲明年的訂單。
這裏我的代碼:
// Enter the ID of the product that shouldn't be purchased again
$no_repeats_id = 944;
$no_repeats_product = wc_get_product($no_repeats_id);
// Get the current product to check if purchasing should be disabled
global $product;
if ($no_repeats_product->is_type('variation')) {
// Bail if we're not looking at the product page for the non-purchasable product
if (! $no_repeats_product->parent->id === $product->id) {
return;
}
// Render the purchase restricted message if we are
if (wc_customer_bought_product(wp_get_current_user()->user_email, get_current_user_id(), $no_repeats_id)) {
sv_render_variation_non_purchasable_message($product, $no_repeats_id);
}
} elseif ($no_repeats_id === $product->id) {
if (wc_customer_bought_product(wp_get_current_user()->user_email, get_current_user_id(), $no_repeats_id)) {
// Create your message for the customer here
add_action('woocommerce_before_calculate_totals', 'add_custom_price');
add_action('woocommerce_before_calculate_totals', 'add_custom_price');
function add_custom_price($cart_object) {
$custom_price = 10; // This will be your custome price
$target_product_id = 944;
foreach ($cart_object->cart_contents as $value) {
if ($value['product_id'] == $target_product_id) {
$value['data']->price = $custom_price;
}
/*
// If your target product is a variation
if ($value['variation_id'] == $target_product_id) {
$value['data']->price = $custom_price;
}
*/
}
}
}
}
}
add_action('woocommerce_single_product_summary', 'sv_purchase_disabled_message', 31);
這不起作用。如果(!wc_customer_bought_product($ CUSTOMER_EMAIL,$ CUSTOMER_ID,$ targeted_product_id))需要,如果(wc_customer_bought_product($ CUSTOMER_EMAIL,$ CUSTOMER_ID,$ targeted_product_id)) 沒有! –
@MujtabaK ** **更新對不起,我忘了刪除了'!'當我正在testings,但此代碼的工作... – LoicTheAztec
同意代碼的偉大工程增加。非常感謝。 –