skip to Main Content

my code working fine in functions file

how i can create a custom plugin for working this code ?

add_action( 'woocommerce_cart_calculate_fees' , 'wpdesk_checkout_fee' );
add_action( 'woocommerce_after_cart_item_quantity_update', 'wpdesk_checkout_fee' );
function wpdesk_checkout_fee() {
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    $list = array();
    foreach($items as $item=> $values ){
        array_push($list, $values['product_id']);   
    }
    foreach($list as $productid){
        if($productid == 2361){
            $fee = (int)-5000;
            WC()->cart->add_fee( "fee: ", $fee, false, '' );

        }
    }

}

i need correct snippets for use in custom plugin

2

Answers


  1. You can easily convert your working code into a plugin.

    Step1: Create a folder for your plugin name (Example: custom-plugin)

    Step2: Create a new file with the same name as your plugin

    Step3: Add your code to this file

    Step4: Make sure you have added the headers for the plugin file.

    Finally, your plugin code will be like this.

    <?php
    
    /*
    Plugin Name: Customize Other Plugin Output
    Plugin URI: http://brianhogg.com/
    Description: Changes the output from The Other Plugin using a bit of code
    Author: Brian Hogg
    Version: 1.0.0
    Author URI: http://brianhogg.com
    
    
    add_action( 'woocommerce_cart_calculate_fees' , 'wpdesk_checkout_fee' );
    add_action( 'woocommerce_after_cart_item_quantity_update', 'wpdesk_checkout_fee' );
    function wpdesk_checkout_fee() {
        global $woocommerce;
        $items = $woocommerce->cart->get_cart();
        $list = array();
        foreach($items as $item=> $values ){
            array_push($list, $values['product_id']);   
        }
        foreach($list as $productid){
            if($productid == 2361){
                $fee = (int)-5000;
                WC()->cart->add_fee( "fee: ", $fee, false, '' );
    
            }
        }
    
    }
    

    Refer to this link for more details https://brianhogg.com/functional-wordpress-plugins/

    Login or Signup to reply.
    1. Create folder of your plugin name (Dont use space)
    2. Create file with the same name of plugin (Dont use space)
    3. Add code to your plugin file using below details for plugin

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search