3 回答

TA貢獻1820條經驗 獲得超3個贊
你可以這樣試試:
add_action('woocommerce_add_to_cart', 'custom_action_add_to_cart', 20, 6);
function custom_action_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
$cart = WC()->cart->get_cart();
$product = wc_get_product($product_id);
$referer = $_SERVER['HTTP_REFERER'];
$route = parse_url( $referer );
$path = $route['path'] ?? 'home' ;
$args = array_filter( ( explode('/', $path) ) );
if (in_array( 'product', $args) ) {
// Product Page
} elseif (in_array('product-category', $args)) {
// Product Category
} else {
// Default
}
}
但是你需要檢查你的設置。Settings > Permalinks.

TA貢獻1895條經驗 獲得超7個贊
您可以使用wp_get_referer或check_ajax_referer()例如:
function custom_action_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data){ $cart = WC()->cart->get_cart(); $product = wc_get_product($product_id); $referer = wp_get_referer(); // HOMEPAGE if (strpos($referer ,'http://yourwebsite.com/') !== false) { $new_quantity = $quantity_from_cart + 1; } //from some product page like http://yourwebsite.com/product/my-product-page else if (strpos($referer ,'http://yourwebsite.com/product/') !== false) { $new_quantity = $quantity_from_cart * 3; } // Need to send the $new_quantity along with the $cart_item_key to the custom function so that the data can be saved using $cart_item_key my_custom_function($new_quantity, $cart_item_key);}

TA貢獻1853條經驗 獲得超9個贊
你可以使用is_product(),is_product_category()功能
function custom_action_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data)
{
$cart = WC()->cart->get_cart();
$product = wc_get_product($product_id);
if ( is_product() ) {
global $product;
$id = $product->get_id();
foreach ( WC()->cart->get_cart() as $cart_item ) {
if($cart_item['product_id'] == $id ){
$quantity_from_cart = $cart_item['quantity'];
break; // stop the loop if product is found
}
}
$new_quantity = $quantity_from_cart * 3;
}
else if (is_product_category()) {
$new_quantity = $quantity_from_cart + 1;
}
my_custom_function($new_quantity, $cart_item_key);
}
- 3 回答
- 0 關注
- 206 瀏覽
添加回答
舉報