Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 针对商业中特定产品的本地提货固定折扣(按产品)_Php_Wordpress_Woocommerce_Discount_Shipping Method - Fatal编程技术网

Php 针对商业中特定产品的本地提货固定折扣(按产品)

Php 针对商业中特定产品的本地提货固定折扣(按产品),php,wordpress,woocommerce,discount,shipping-method,Php,Wordpress,Woocommerce,Discount,Shipping Method,在我们的商店里,我们想为客户提供挑选产品的选择。对于X类产品,如果客户选择提货选项,我们希望为他们提供每种产品6欧元的折扣。更复杂的是,如果他们不选择提货选项。交付费为5.95,不适用于X类产品 我试图使用这段代码,但无法理解。如果我们能够通过其id而不是类别来识别这些产品,那将是最好的 /** * Discount for Local Pickup */ add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pi

在我们的商店里,我们想为客户提供挑选产品的选择。对于X类产品,如果客户选择提货选项,我们希望为他们提供每种产品6欧元的折扣。更复杂的是,如果他们不选择提货选项。交付费为5.95,不适用于X类产品

我试图使用这段代码,但无法理解。如果我们能够通过其id而不是类别来识别这些产品,那将是最好的

/**
* Discount for Local Pickup
*/
add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_method', 10, 1 );
function custom_discount_for_pickup_shipping_method( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $discount_amount = 6; // Discount

    $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping_method    = explode(':', $chosen_shipping_method_id)[0];
    

    // Only for Local pickup chosen shipping method
    if ( strpos( $chosen_shipping_method_id, 'local_pickup' ) !== false) {

        // Set variable
        $new_subtotal = 0;
        
        // Set discount excluded categories list
        $arr_discount_excluded_category = ['merch', 'rum'];

        // Set variable for matched excluded category from the cart list
        $arr_discount_excluded_category_matched = [];

        // Loop though each cart items and set prices in an array
        foreach ( $cart->get_cart() as $cart_item ) {

            // Get product
            $product = wc_get_product( $cart_item['product_id'] );

            // Get product category
            $category_list = wp_get_post_terms($cart_item['product_id'],'product_cat',array('fields'=>'names'));
            
            // Product has no discount
            $arr_discount_excluded_category_matched_by_item = array_intersect($category_list, $arr_discount_excluded_category);
            if ( ! $product->is_on_sale() && empty($arr_discount_excluded_category_matched_by_item)) {
                // line_subtotal
                $line_subtotal = $cart_item['line_subtotal'];

                // Add to new subtotal
                $new_subtotal += $line_subtotal;
            }
            else{
                $arr_discount_excluded_category_matched = array_merge($arr_discount_excluded_category_matched, $arr_discount_excluded_category_matched_by_item);
            }
        }
        
        // Calculate the discount
        $discount = 0;
        if($new_subtotal > 0){
            $discount = $new_subtotal - $discount_amount;
        }
        
        //Add notification
        if(!empty($arr_discount_excluded_category_matched)){
            $str_message = 'Pickup discount does not apply to products from the Category "' . implode('", "', array_unique($arr_discount_excluded_category_matched)) . '"';
            wc_add_notice($str_message, 'notice');
        }

        // Add the discount
        $cart->add_fee( __('Discount') . ' (' . $discount_amount . '€)', -$discount );
    }
}

当选择本地提货运输方式时,以下内容将仅对特定定义的产品按产品添加固定折扣

在下面的代码中定义所需的产品ID和按产品列出的折扣金额:

add_action( 'woocommerce_cart_calculate_fees', 'fixed_discount_by_product_for_pickup_shipping_method', 10, 1 );
function fixed_discount_by_product_for_pickup_shipping_method( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $discount_by_product  = 6; // Here set the discount amount by product
    $targeted_product_ids = array(37,40); // Here set your excluded product ids

    $chosen_shipping_id = WC()->session->get('chosen_shipping_methods')[0];
    $total_discount     = 0; // Initializing

    // Only for "Local pickup" shipping method
    if ( strpos( $chosen_shipping_id, 'local_pickup' ) !== false ) {
        // Loop though each cart items and set prices in an array
        foreach ( $cart->get_cart() as $item ) {
            // Get matching targeted product ids from the current cart item
            $matched_product_ids = array_intersect( array($item['product_id'], $item['variation_id']), $targeted_product_ids);  
            
            // If product matches with targeted product Ids
            if ( ! empty($matched_product_ids) ) {
                // Add the discount by product
                $total_discount += $discount_by_product; 
            }
        }
        if ( $total_discount > 0 ) {
            $cart->add_fee( __('Local pickup discount'), -$total_discount ); // Add the discount
        }
    }
}
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作


要在折扣计算中处理产品数量,请替换:

$total_discount += $discount_by_product;
与:


在这里,我们非常感谢您对下面答案的反馈。
$total_discount += $discount_by_product * $item['quantity'];