Forum replies created

  1. Santosh Kumar
    Keymaster
    Posts: 67
    March 20, 2020 at 7:14 am #5461
    Santosh Kumar

    Hi Gagandeep,

    Products and quantities are located in the following tables

    1. wp_posts
    2. wp_postmeta

    Thanks

  2. Santosh Kumar
    Keymaster
    Posts: 67
    March 18, 2020 at 9:32 am #5457
    Santosh Kumar

    Hi Gagandeep,

    Filter hook: woocommerce_paypal_args So what I did is:

    add_action( ‘woocommerce_paypal_args’, ‘paypal_args’ );
    function paypal_args( $order ) {
    $partial_payment = isset( $_REQUEST[ ‘partial_payment’ ] ) ? $_REQUEST[ ‘partial_payment’ ] : false;

    if( $partial_payment ) {
    $current_price = $order[‘amount_1’];
    // Some calculation process here..
    $order[‘amount_1’] = $new_price;
    }

    return $order;
    }

    Thanks

  3. Santosh Kumar
    Keymaster
    Posts: 67
    March 16, 2020 at 5:14 am #5445
    Santosh Kumar

    Hi Gagandeep,

    You need to use wc_get_price_to_display($product) instead of $product->get_price_html() that will output the raw numerical price.

    The code:

    if ($hide_price !== '0') :
    $output_price .= '<div class="dhvc-woo-price dhvc-woo-span6">';
    $output_price .= $product->get_price_html (); //this is where the price is called
    
    // Here your correct price divided by 10 and formatted for the output
    $price_d10 = wc_price(wc_get_price_to_display($product) / 10);
    $output_price .= '<p><br>Em até 10x de '. $price_d10 .'</p>';
    $output_price .= '</div>';
    endif;
    
    $output .= apply_filters('dhvc_woo_price', $output_price, $product, $display);

    Tested and works.

    Thanks

  4. Santosh Kumar
    Keymaster
    Posts: 67
    February 28, 2020 at 11:38 am #5401
    Santosh Kumar

    Hi Gagandeep,

    This custom hooked function should do what you expect. You can set in it your progressive discount limit based on individual item quantity.

    Here is the code

    ## Tested and works on WooCommerce 2.6.x and 3.0+

    add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_by_item_quantity', 10, 1 );
    function progressive_discount_by_item_quantity( $cart_obj ) {
    
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;
    
    # Progressive quantity until quantity 3 is reached (here)
    # After this quantity limit, the discount by item is fixed
    # No discount is applied when item quantity is equal to 1
    
    // Set HERE the progressive limit quantity discount
    $progressive_limit_qty = 3; // <== <== <== <== <== <== <== <== <== <== <==
    
    $discount = 0;
    
    foreach( $cart_obj->get_cart() as $cart_item_key => $item_values ){
    
    $qty = $item_values['quantity'];
    
    if( $qty <= $progressive_limit_qty )
    $param = $qty; // Progressive
    else
    $param = $progressive_limit_qty; // Fixed
    
    ## Calculation ##
    $discount -= 5 * $qty * ($param - 1);
    }
    
    if( $discount < 0 )
    $cart_obj->add_fee( __( 'Quantity discount' ), $discount); // Discount
    
    }

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works.

  5. Santosh Kumar
    Keymaster
    Posts: 67
    February 28, 2020 at 11:37 am #5400
    Santosh Kumar

    Hi Gagandeep,

    Here is the complete code to Store product custom field in cart object and display that in Cart and Checkout pages:

    // Output the Custom field in Product pages
    add_action("woocommerce_before_add_to_cart_button", "options_on_single_product", 1);
    function options_on_single_product(){
    ?>
    <label for="custom_field">
    <input type="radio" name="custom_field" checked="checked" value="option1"> option 1 <br />
    <input type="radio" name="custom_field" value="option2"> option 2
    </label> <br />
    <?php
    }
    
    // Stores the custom field value in Cart object
    add_filter( 'woocommerce_add_cart_item_data', 'save_custom_product_field_data', 10, 2 );
    function save_custom_product_field_data( $cart_item_data, $product_id ) {
    if( isset( $_REQUEST['custom_field'] ) ) {
    $cart_item_data[ 'custom_field' ] = $_REQUEST['custom_field'];
    // below statement make sure every add to cart action as unique line item
    $cart_item_data['unique_key'] = md5( microtime().rand() );
    WC()->session->set( 'my_order_data', $_REQUEST['custom_field'] );
    }
    return $cart_item_data;
    }
    
    // Outuput custom Item value in Cart and Checkout pages
    add_filter( 'woocommerce_get_item_data', 'output_custom_product_field_data', 10, 2 );
    function output_custom_product_field_data( $cart_data, $cart_item ) {
    
    if( !empty( $cart_data ) )
    $custom_items = $cart_data;
    
    if( isset( $cart_item['custom_field'] ) ) {
    $custom_items[] = array(
    'key' => __('Custom Item', 'woocommerce'),
    'value' => $cart_item['custom_field'],
    'display' => $cart_item['custom_field'],
    );
    }
    return $custom_items;
    }

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested and works.

    Thanks

  6. Santosh Kumar
    Keymaster
    Posts: 67
    February 25, 2020 at 7:54 am #5379
    Santosh Kumar

    Hi Gagandeep,

    Try this,

    $result = array_shift(woocommerce_get_product_terms($product->id, 'pa_koostis', 'names'));

    Thanks

  7. Santosh Kumar
    Keymaster
    Posts: 67
    February 25, 2020 at 7:52 am #5378
    Santosh Kumar

    Hi Gagandeep,

    Add a tax_query to your $args…

    $args = array(
    'tax_query' => array(
    array(
    'taxonomy' => 'product_cat',
    'field' => 'slug', //possible values are term_id, name, slug or term_taxonomy_id
    'terms' => 'tshirt' //can be single string or array of slugs, names, term_ids or taxonomy_ids
    )
    )
    );
    
    $results = get_posts( $args );

    Thanks

  8. Santosh Kumar
    Keymaster
    Posts: 67
    February 11, 2020 at 12:36 pm #5104
    Santosh Kumar

    Hi Gagandeep,

    On that condition

    add_filter('woocommerce_product_needs_shipping', function(){return false;});

    This will stop the calculate button. Otherwise, there is no direct hook. You may end up editing your woocommerce theme and editing ‘cart\shipping-calculator.php’ and applying a logic there.

    Say you can place your own hook/filter there and set true/false from your outside code.

    Thanks

  9. Santosh Kumar
    Keymaster
    Posts: 67
    February 11, 2020 at 12:35 pm #5103
    Santosh Kumar

    Hi Gagandeep,

    The post meta key for the payment method ID is simply _payment_method

    So if $order->payment_method doesn’t have the magic methods in place to get that automatically, you could retrieve the post meta using traditional WordPress

    
    get_post_meta( $order->id, '_payment_method', true );

    Update for WooCommerce 3.0

    $order->get_payment_method();

    Thanks

  10. Santosh Kumar
    Keymaster
    Posts: 67
    February 11, 2020 at 12:26 pm #5100
    Santosh Kumar

    Hi Gagandeep,

    I couldn’t find an appropriate WooCommerce hook for the order-pay page. What I decided to do is add an action for the wp_enqueue_scripts and only load my scripts if the applicable route is active.

    In my plugin constructor:

    function __contstruct {
    // other stuff
    add_action( 'wp_enqueue_scripts', array( $this, 'load_my_order_pay_scripts' ) );
    }

    The function to load my scripts:

    public function load_my_order_pay_scripts() {
    if( !is_wc_endpoint_url( 'order-pay' ) ) return;
    
    global $wp;
    $order_id = $wp->query_vars['order-pay'];
    $order = wc_get_order( $order_id );
    
    wp_enqueue_script( 'my-js-sdk', 'http://my.cdn.com/my-js-sdk.js', true );
    wp_register_script( 'my-plugin-script', plugin_dir_url( __FILE__ ) . '/js/my-plugin-script.js', true );
    wp_localize_script( 'my-plugin-script', 'params', $this->script_parameters, true ); // I need to pass my script some data
    wp_enqueue_script( 'my-plugin-script', array('my-js-sdk'), true ); // my plugin's JS is dependent on my SDK
    }

    Thanks

  11. Santosh Kumar
    Keymaster
    Posts: 67
    February 11, 2020 at 12:26 pm #5099
    Santosh Kumar

    Hi Gagandeep,

    Before placing order you can not get the order id, so you have to write your function after order is being placed, so for that you can add your action on any one of these hooks-

    do_action( ‘woocommerce_thankyou’, $order->id );
    do_action( ‘woocommerce_thankyou_’ . $order->payment_method, $order->id );

    Thanks

  12. Santosh Kumar
    Keymaster
    Posts: 67
    February 11, 2020 at 11:41 am #5096
    Santosh Kumar

    Hi Gagandeep,

    If you want an order id as parameter to your action hook callback function you can use woocommerce_thankyou hook.

    Thanks

  13. Santosh Kumar
    Keymaster
    Posts: 67
    February 11, 2020 at 11:40 am #5095
    Santosh Kumar

    Hi Gagandeep,

    You forgot to change the Checkout Page option.

    Please follow the following steps.

    Go to Dashboard -> WooCommerce -> Settings -> Checkout -> Checkout Options
    Under Checkout Pages section you will see Checkout Page option. Select page in which you have posted [woocommerce_checkout].
    Click on Save Changes.
    Now go to the page in which you have been selected in step 2.
    I hope you will see the expected changes on the selected page.

    Thanks

  14. Santosh Kumar
    Keymaster
    Posts: 67
    February 11, 2020 at 11:39 am #5094
    Santosh Kumar

    Hi Gagandeep,

    Try using the following code in your plugin

    add_action( 'woocommerce_payment_complete', 'my_change_status_function' );
    
    function my_change_status_function( $order_id ) {
    
    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );
    
    }

    Thanks

  15. Santosh Kumar
    Keymaster
    Posts: 67
    February 11, 2020 at 9:24 am #5090
    Santosh Kumar

    Hi Gagandeep,

    With latest version of WooCommerce is possible try this as something like

    $address = array(
    ‘first_name’ => ‘Fresher’,
    ‘last_name’ => ‘StAcK OvErFloW’,
    ‘company’ => ‘stackoverflow’,
    ’email’ => ‘test@test.com’,
    ‘phone’ => ‘777-777-777-777’,
    ‘address_1′ => ’31 Main Street’,
    ‘address_2’ => ”,
    ‘city’ => ‘Chennai’,
    ‘state’ => ‘TN’,
    ‘postcode’ => ‘12345’,
    ‘country’ => ‘IN’
    );

    $order = wc_create_order();
    $order->add_product( get_product( ’12’ ), 2 ); //(get_product with id and next is for quantity)
    $order->set_address( $address, ‘billing’ );
    $order->set_address( $address, ‘shipping’ );
    $order->add_coupon(‘Fresher’,’10’,’2′); // accepted param $couponcode, $couponamount,$coupon_tax
    $order->calculate_totals();
    Call this above code with your function then it will work accordingly.

    Thanks