chore: update
This commit is contained in:
parent
d6238c49ee
commit
ea1011b73b
|
|
@ -44,18 +44,18 @@ class Yoone_Product_Bundles_Frontend {
|
|||
}
|
||||
|
||||
/**
|
||||
* Hijack the add-to-cart process for bundle products.
|
||||
* Validates the submission, then adds the container and all child products to the cart.
|
||||
* 劫持混装产品的“添加到购物车”流程。
|
||||
* 验证提交,然后将容器和所有子产品添加到购物车。
|
||||
*
|
||||
* @return bool False to prevent the default add-to-cart action.
|
||||
* @return bool False 以阻止默认的“添加到购物车”操作。
|
||||
*/
|
||||
public function process_bundle_add_to_cart($passed, $product_id, $quantity) {
|
||||
$product = wc_get_product($product_id);
|
||||
if (!$product || $product->get_type() !== Yoone_Product_Bundles::TYPE) {
|
||||
return $passed; // Not our product, pass through.
|
||||
return $passed; // 不是我们的产品,直接通过。
|
||||
}
|
||||
|
||||
// 1. Validation
|
||||
// 1. 验证
|
||||
$config = Yoone_Product_Bundles::get_bundle_config($product);
|
||||
$min_qty = max(1, absint($config['min_qty']));
|
||||
$components = !empty($_POST['yoone_bundle_components']) ? (array) $_POST['yoone_bundle_components'] : array();
|
||||
|
|
@ -75,14 +75,14 @@ class Yoone_Product_Bundles_Frontend {
|
|||
return false;
|
||||
}
|
||||
|
||||
// 2. Add items to cart
|
||||
// 2. 添加商品到购物车
|
||||
try {
|
||||
$bundle_container_id = uniqid('bundle_');
|
||||
|
||||
// Add the main bundle product (as a 0-price container)
|
||||
// 添加主混装产品(作为价格为0的容器)
|
||||
WC()->cart->add_to_cart($product_id, 1, 0, array(), array('yoone_bundle_container_id' => $bundle_container_id));
|
||||
|
||||
// Add child components
|
||||
// 添加子组件
|
||||
foreach ($clean_components as $comp_id => $qty) {
|
||||
WC()->cart->add_to_cart($comp_id, $qty, 0, array(), array(
|
||||
'yoone_bundle_parent_id' => $bundle_container_id,
|
||||
|
|
@ -90,7 +90,7 @@ class Yoone_Product_Bundles_Frontend {
|
|||
));
|
||||
}
|
||||
|
||||
// Set success message and redirect
|
||||
// 设置成功消息并重定向
|
||||
wc_add_to_cart_message(array($product_id => 1), true);
|
||||
add_filter('woocommerce_add_to_cart_redirect', function() { return wc_get_cart_url(); });
|
||||
|
||||
|
|
@ -99,30 +99,44 @@ class Yoone_Product_Bundles_Frontend {
|
|||
return false;
|
||||
}
|
||||
|
||||
// Prevent default add-to-cart for the main product
|
||||
// 阻止主产品的默认“添加到购物车”行为
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* When a cart item is removed, check if it's a bundle container.
|
||||
* If so, find and remove all its child items.
|
||||
* 当一个购物车项目被移除时,检查它是否是一个混装容器。
|
||||
* 如果是,则找到并移除其所有的子项目。
|
||||
*/
|
||||
public function remove_bundle_children($removed_cart_item_key, $cart) {
|
||||
$removed_item = $cart->get_removed_cart_items()[$removed_cart_item_key];
|
||||
// 此钩子在项目已从购物车会话中移除后触发,所以我们在 `removed_cart_contents` 中查找它。
|
||||
if (!isset($cart->removed_cart_contents[$removed_cart_item_key])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$removed_item = $cart->removed_cart_contents[$removed_cart_item_key];
|
||||
|
||||
// 检查被移除的商品是否是我们的混装容器。
|
||||
if (isset($removed_item['yoone_bundle_container_id'])) {
|
||||
$bundle_container_id = $removed_item['yoone_bundle_container_id'];
|
||||
$child_item_keys_to_remove = [];
|
||||
|
||||
// 遍历购物车,找到所有属于此容器的子项目。
|
||||
// 我们不能在遍历时直接修改购物车,所以我们先收集要移除的项目的key。
|
||||
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
|
||||
if (isset($cart_item['yoone_bundle_parent_id']) && $cart_item['yoone_bundle_parent_id'] === $bundle_container_id) {
|
||||
$cart->remove_cart_item($cart_item_key);
|
||||
$child_item_keys_to_remove[] = $cart_item_key;
|
||||
}
|
||||
}
|
||||
|
||||
// 现在,遍历收集到的key并从购物车中移除子项目。
|
||||
foreach ($child_item_keys_to_remove as $key_to_remove) {
|
||||
$cart->remove_cart_item($key_to_remove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide the remove link for child items of a bundle.
|
||||
* 隐藏混装产品子项目的移除链接。
|
||||
*/
|
||||
public function hide_child_remove_link($link, $cart_item_key) {
|
||||
$cart_item = WC()->cart->get_cart_item($cart_item_key);
|
||||
|
|
@ -133,7 +147,7 @@ class Yoone_Product_Bundles_Frontend {
|
|||
}
|
||||
|
||||
/**
|
||||
* For child items, add a meta line in the cart to link back to the parent bundle.
|
||||
* 对于子项目,在购物车中添加一个元信息行,以链接回父混装产品。
|
||||
*/
|
||||
public function display_child_bundle_link($item_data, $cart_item) {
|
||||
if (isset($cart_item['yoone_bundle_parent_product_id'])) {
|
||||
|
|
@ -149,7 +163,7 @@ class Yoone_Product_Bundles_Frontend {
|
|||
}
|
||||
|
||||
/**
|
||||
* Remove the default "Add to Cart" button for bundle products only.
|
||||
* 仅为混装产品移除默认的“添加到购物车”按钮。
|
||||
*/
|
||||
public function remove_default_add_to_cart_for_bundle() {
|
||||
global $product;
|
||||
|
|
@ -159,7 +173,7 @@ class Yoone_Product_Bundles_Frontend {
|
|||
}
|
||||
|
||||
/**
|
||||
* Render the custom "Add to Cart" form for bundle products.
|
||||
* 为混装产品渲染自定义的“添加到购物车”表单。
|
||||
*/
|
||||
public function render_bundle_add_to_cart_form() {
|
||||
global $product;
|
||||
|
|
@ -169,7 +183,7 @@ class Yoone_Product_Bundles_Frontend {
|
|||
}
|
||||
|
||||
/**
|
||||
* Add a body class for bundle product pages.
|
||||
* 为混装产品页面添加一个 body class。
|
||||
*/
|
||||
public function filter_body_class($classes) {
|
||||
global $post;
|
||||
|
|
|
|||
Loading…
Reference in New Issue