wordpress 在WooCommerce上设置简单和可变产品的最低订购量

eeq64g8w  于 2023-11-17  发布在  WordPress
关注(0)|答案(1)|浏览(281)

我是WooCommerce和PHP的新手,我计划在WooCommerce中为简单和可变产品添加一个字段,以便我可以控制最小订单量。
基于此,例如,当它设置为10时,WooCommerce购物车计数器应自动更改为该数字,并且用户不能减少订单的值,因为最小订单数量已设置为10。
所以我想要最低最小订货量(因为只有一个最小订货量可以用于购物车)
下面是我的代码:

/*Minimum Order - Simple & Variable Product*/
// Add minimum order field to product options
function add_minimum_order_field() {
    global $product_object;

    woocommerce_wp_text_input(
        array(
            'id' => 'minimum_order',
            'class' => 'wc_input_price short',
            'label' => 'Minimim Order',
            'desc_tip' => true,
            'description' => 'Set mimimum order for this product',
            'value' => get_post_meta($product_object->get_id(), 'minimum_order', true), // Populate the field with the saved value
        )
    );
}
add_action('woocommerce_product_options_pricing', 'add_minimum_order_field');

// Save minimum order field value
function save_minimum_order_field($product) {
    $minimum_order = isset($_POST['minimum_order']) ? wc_clean($_POST['minimum_order']) : '';
    $product->update_meta_data('minimum_order', $minimum_order);
}
add_action('woocommerce_admin_process_product_object', 'save_minimum_order_field');

// Add minimum order field to product variations
function add_minimum_order_field_to_variations($loop, $variation_data, $variation) {
    woocommerce_wp_text_input(
        array(
            'id' => 'minimum_order[' . $variation->ID . ']',
            'class' => 'wc_input_price short',
            'label' => 'Minimim Order',
            'desc_tip' => true,
            'description' => 'Set mimimum order for this variation',
            'value' => get_post_meta($variation->ID, 'minimum_order', true), // Populate the field with the saved value
        )
    );
}
add_action('woocommerce_variation_options_pricing', 'add_minimum_order_field_to_variations', 10, 3);

// Save minimum order field value for product variations
function save_minimum_order_field_for_variations($variation_id, $i) {
    $minimum_order = isset($_POST['minimum_order'][$variation_id]) ? wc_clean($_POST['minimum_order'][$variation_id]) : '';
    update_post_meta($variation_id, 'minimum_order', $minimum_order);
}
add_action('woocommerce_save_product_variation', 'save_minimum_order_field_for_variations', 10, 2);

// Validate minimum order on add to cart
function validate_minimum_order($passed, $product_id, $quantity) {
    $minimum_order = get_post_meta($product_id, 'minimum_order', true);

    if ($quantity < $minimum_order) {
        wc_add_notice(sprintf('the minimum order must be %s.', $minimum_order), 'error');
        $passed = false;
    }

    return $passed;
}
add_filter('woocommerce_add_to_cart_validation', 'validate_minimum_order', 10, 3);

字符串
但使它的工作,因为我需要得到最低最低订单数量将适用于购物车。
如何根据购物车商品的最低最小订货量设置简单产品和可变产品的最小订货量?

kmpatx3s

kmpatx3s1#

我重新检查了你的代码,因为有一些错误。
对于全局最低订单总商品数量(基于最低最低订单),需要在购物车上进行验证,而不是在添加到购物车的单个产品上进行验证。因此,如果未满足所需的(最低)最低订单,购物车中将显示错误消息,阻止结账访问。


的数据
尝试以下操作:

// Display a minimum order input field to product options pricing
add_action('woocommerce_product_options_pricing', 'add_minimum_order_field');
function add_minimum_order_field() {

    woocommerce_wp_text_input( array(
        'id'            => 'minimum_order',
        'type'          => 'number',
        'class'         => 'short',
        'label'         => 'Minimum order',
        'desc_tip'      => true,
        'description'   => 'Set minimum order for this product',
    ) );
}

// Display a minimum order input field to product variation options pricing
add_action('woocommerce_variation_options_pricing', 'add_minimum_order_field_to_variations', 10, 3);
function add_minimum_order_field_to_variations($loop, $variation_data, $variation) {

    woocommerce_wp_text_input( array(
        'id'            => 'minimum_order['.$loop.']',
        'type'          => 'number',
        'wrapper_class' => 'form-row form-row-wide',
        'class'         => 'short',
        'label'         => 'Minimum Order',
        'desc_tip'      => true,
        'description'   => 'Set minimum order for this variation',
        'value'         => get_post_meta($variation->ID, 'minimum_qty', true), 
    ) );
}
// Save minimum order field value for products
add_action('woocommerce_admin_process_product_object', 'save_minimum_order_field');
function save_minimum_order_field($product) {
    $minimum_order = isset($_POST['minimum_order']) ? esc_attr($_POST['minimum_order']) : '';
    $product->update_meta_data('minimum_order', $minimum_order);
}

// Save minimum order field value for product variations
add_action('woocommerce_admin_process_variation_object', 'save_minimum_order_field_for_variations', 10, 2);
function save_minimum_order_field_for_variations($variation, $i) {
    $minimum_order = isset($_POST['minimum_order'][$i]) ? esc_attr($_POST['minimum_order'][$i]) : '';
    $variation->update_meta_data('minimum_order', $minimum_order);
}

// Utility function: Get the lowest min order
function get_lowest_minimum_order() {
    $data_array = []; // Initializing

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $item ) {
        $min_order = $item['data']->get_meta('minimum_order');

        if ( empty($min_order) && $item['variation_id'] > 0 ) {
            // if variation minimum order is not defined take the parent product minimum order  
            $min_order = get_post_meta($item['product_id'], 'minimum_order', true);
        }

        if ( ! empty($min_order) ) {
            $data_array[] = $min_order;
        }
    }
    return min($data_array);
}

// Cart items validation
add_action( 'woocommerce_check_cart_items', 'minimum_order_cart_items_validation' );
function minimum_order_cart_items_validation() {
    $minimum_order    = get_lowest_minimum_order();
    $cart_total_count = WC()->cart->get_cart_contents_count();

    if ( ! empty($minimum_order) && $cart_total_count < $minimum_order ) {
        wc_add_notice(sprintf('The minimum order total items quantity must be %s.', $minimum_order), 'error');
    }
}

字符串
代码放在你的子主题的functions.php文件中(或者插件中)。它应该可以工作。
如果您想根据**最高最低订单设置最低订单总物料数量,只需更改:

return min($data_array);


致:

return max($data_array);

相关问题