Forum replies created

  1. gagandeep
    Participant
    Posts: 8
    November 11, 2019 at 11:10 am #4133
    gagandeep

    Hi Manik,

    Here is the snippet of the code

    $product = new WC_Product($product_id);
    $upsells = $product->get_upsells();
    if (!$upsells)
    return;
    
    $meta_query = WC()->query->get_meta_query();
    
    $args = array(
    'post_type' => 'product',
    'ignore_sticky_posts' => 1,
    'no_found_rows' => 1,
    'posts_per_page' => $posts_per_page,
    'orderby' => $orderby,
    'post__in' => $upsells,
    'post__not_in' => array($product->id),
    'meta_query' => $meta_query
    );
    
    $products = new WP_Query($args);
    if ($products->have_posts()) :
    // Iterate over the each product
    
    endif;

    Thanks

    • This reply was modified 3 weeks, 5 days ago by gagandeep gagandeep.
  2. gagandeep
    Participant
    Posts: 8
    October 18, 2019 at 11:42 am #3547
    gagandeep

    Hi Aditi,

    The following will change the order status from processing to your custom status service-booked, if a bookable product is in the order:

    add_action( 'woocommerce_order_status_changed', 'bookable_order_custom_status_change', 10, 4 );
    function bookable_order_custom_status_change( $order_id, $from, $to, $order ) {
    // For orders with status "pending"
    if( $from == 'pending' || in_array( $to, array('pending', 'on-hold') ) ) :
    
    // Get an instance of the product Object
    $product = $item->get_product();
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
    // If a bookable product is in the order
    if( $product->is_type('booking') ) {
    // Change the order status
    $order->update_status('service-booked');
    break; // end the loop
    }
    }
    endif;
    }

    Code goes on function.php file of your active child theme (or active theme).

    I hope this will work.

    Thanks

  3. gagandeep
    Participant
    Posts: 8
    October 8, 2019 at 7:39 am #3368
    gagandeep

    Try this, it’s working with me

    add_filter( 'gettext', 'wps_translate_words_array' );
    add_filter( 'ngettext', 'wps_translate_words_array' );
    function wps_translate_words_array( $translated ) {
    $words = array(
    // 'word to translate' = > 'translation'
    'Related Products' => 'Check out these related products',
    );
    $translated = str_ireplace( array_keys($words), $words, $translated );
    return $translated;
    }
  4. gagandeep
    Participant
    Posts: 8
    October 4, 2019 at 11:56 am #3341
    gagandeep

    The following code enables COD payments for specifically defined cities.

    You need to use an array of allowed cities with in_array() PHP conditional function like:

    add_filter( 'woocommerce_available_payment_gateways', 'cities_based_payment_gateway_cod' );
    function cities_based_payment_gateway_cod( $available_gateways ) {
    // HERE define the allowed cities in this array
    $cities = array( 'New York', 'San Francisco' );
    
    if ( isset( $available_gateways['cod'] ) && ! in_array( WC()->customer->get_shipping_city(), $cities ) ) {
    unset( $available_gateways['cod'] );
    }
    return $available_gateways;
    }

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

  5. gagandeep
    Participant
    Posts: 8
    October 3, 2019 at 11:53 am #3303
    gagandeep

    Here is a custom hooked function that will apply to cart a discount, if there is 5 or more items in cart and no products on sale:

    add_action('woocommerce_cart_calculate_fees' , 'custom_discount', 10, 1);
    function custom_discount( $cart ){
    
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;
    
    // Only when there is 5 or more items in cart
    if( $cart->get_cart_contents_count() >= 5):
    
    $is_on_sale = false;
    
    // Iterating through item in cart
    foreach( $cart->get_cart() as $cart_item ){
    // Getting an instance of the product object
    $product = $cart_item['data'];
    
    // If a cart item is in sale, $is_on_sale is true and we stop the loop
    if($product->is_on_sale()){
    $is_on_sale = true;
    break;
    }
    }
    
    ## Discount calculation ##
    $discount = $cart->subtotal * -0.1;
    
    ## Applied discount (no products on sale) ##
    if(!$is_on_sale )
    $cart->add_fee( '10% discount', $discount);
    
    endif;
    }

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

    This code is tested.

  6. gagandeep
    Participant
    Posts: 8
    October 3, 2019 at 11:42 am #3299
    gagandeep

    You have to call the global variable to ensure that it gets the correct values.

    If you add

    global $woocommerce;
    just before

    $amount2 = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
    that will work.

  7. gagandeep
    Participant
    Posts: 8
    September 25, 2019 at 4:39 am #3187
    gagandeep

    If you want to check for a specific coupon, to check if ANY coupon has been applied the following code will get the job done.

    if( WC()->cart->get_coupons() ) echo "Coupon applied";

  8. gagandeep
    Participant
    Posts: 8
    September 5, 2019 at 9:53 am #2909
    gagandeep

    You can keep:

    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
    To add the single product meta inside the description tab you can not use hooks…

    You need first to override single-product/tabs/description.php template via your theme as explained in this official documentation.

    Once you have copied the file to the woocommerce folder inside your active theme, open edit single-product/tabs/description.php file and add the following line inside it:

    wc_get_template( 'single-product/meta.php' );

    It will displays the product meta information inside the product description tab.