• Resolved Beats247

    (@beats247)


    Hello there,
    I have added this code to the functions.php of my child theme to add related products to customer emails with a shortcode in the template. The code used to work until the latest WC / WP update. Do you know why it isn’t working anymore?

    This is the code:

    // product suggestion in order mail
    function order_mail_product_suggestion($atts) {
    $atts=shortcode_atts(
        array(
            'id' => '',
        ), $atts, 'order_mail_product_suggestion');
    $orderId = esc_attr($atts['id']);
    $order = wc_get_order( (int)$orderId );
    $items = $order->get_items();
    $cat = array();
    $pid = array();
    foreach ( $items as $item ) {
        $pid[] = $item->get_product_id();
        $terms = wp_get_post_terms($item->get_product_id(),'product_cat',array('fields'=>'ids'));
        foreach ( $terms as $term ) {
            $cat[] = $term;
        }
    }
    $uniuqcat = array_unique($cat);
    $uniuqpid = array_unique($pid);
    $html = '';
    $args = array(
        'post_type'             => 'product',
        'stock'                 => 1,
        'post_status'           => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page'        => '20',
        'tax_query'             => array(
            array(
                'taxonomy'      => 'product_cat',
                'field' => 'term_id',
                'terms'         => implode(',', $uniuqcat),
                'operator'      => 'IN'
            ),
            array(
                'taxonomy'      => 'product_visibility',
                'field'         => 'slug',
                'terms'         => 'exclude-from-catalog',
                'operator'      => 'NOT IN'
            )
        )
    );
    $loop = new WC_Product_Query($args);
    $products = $loop->get_products();
    if ($products) {
        $html .= '<div id="suggestion" style="padding: 20px 0;border-top: 1px solid #eee;">';
            $html .= '<h2 style="text-align: center;font-weight: 400;color: #000;">PLUS, MORE THINGS YOU MAY LIKE</h2>';
            $html .= '<table style="width: 100%;table-layout: fixed;border-collapse: collapse;">';
                $html .= '<tbody>';
                    $html .= '<tr>';
                            $i=0;
                            foreach ( $products as $product ) {
                                if (in_array($product->get_id(), $pid)) {
                                    continue;
                                }
                                $html .= '<td align="center" style="padding: 5px;border: 1px solid #eee;">';
                                if (has_post_thumbnail( $product->get_id() )) {
                                    $html .= get_the_post_thumbnail($product->get_id(), 'shop_catalog');
                                } else {
                                    $html .= '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />';
                                }
                                $html .= '<h3><a style="color: #000;font-weight: normal;text-decoration: none;font-size: 13px;text-align: center;display: block;line-height: 16px;" href="'.get_permalink( $product->get_id() ).'">'.esc_attr($product->get_title() ? $product->get_title() : $product->get_id()).'</a></h3>';
                                $html .= '<p><a style="font-weight: normal;text-decoration: none;display: block;margin: 0 auto;max-width: 72px;padding: 4px 9px;text-align: center;background-color: #000;color: #fff;font-size: 13px;" href="'.get_permalink( $product->get_id() ).'" class="shop-now">Shop Now</a></p>';
                                $i++;
                                if($i==4) break;
                            }
                    $html .= '</tr>';
                $html .= '</tbody>';
            $html .= '</table>';
        $html .= '</div>';
    }
    // now return the preapred html
    return $html;
    }
    // register shortcode add_shortcode('email_product_suggestion', 'order_mail_product_suggestion');
    

    However, now I get a fatal error with the same code
    Fatal error: Uncaught Error: Call to a member function get_items() on boolean in /home/beats247/public_html/2020/wp-content/themes/martfury-child/functions.php:186 Stack trace: #0 /home/beats247/public_html/2020/wp-includes/shortcodes.php(343): order_mail_product_suggestion(Array, ”, ’email_product_s…’) #1 [internal function]: do_shortcode_tag(Array) #2 /home/beats247/public_html/2020/wp-includes/shortcodes.php(218): preg_replace_callback(‘/\\[(\\[?)(email_…’, ‘do_shortcode_ta…’, ‘[email_product_…’) #3 /home/beats247/public_html/2020/wp-content/plugins/kadence-woocommerce-email-designer/templates/woo/emails/customer-completed-order.php(69): do_shortcode(‘[email_product_…’) #4 /home/beats247/public_html/2020/wp-content/plugins/woocommerce/includes/wc-core-functions.php(344): include(‘/home/beats247/…’) #5 /home/beats247/public_html/2020/wp-content/plugins/woocommerce/includes/wc-core-functions.php(363): wc_get_template(’emails/customer…’, Array, ”, ”) #6 /home/beats247/public_html/2020/wp-content/plugins/w in /home/beats247/public_html/2020/wp-content/themes/martfury-child/functions.php on line 186

    This is line 186:
    $items = $order->get_items();

    What needs to be changed in the code so it’s working again? Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Beats247

    (@beats247)

    This is the shortcode in the corresponding email template:

    // Product suggestions
    $order = $email->object;
    if ( $order ) {
       $id = $order->get_id();
       echo do_shortcode( '[email_product_suggestion id="'.$id.'" ]' );
    }
    Plugin Support Hannah S.L.

    (@fernashes)

    Automattic Happiness Engineer

    Hey there!

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Thread Starter Beats247

    (@beats247)

    Hey Hannah,
    thanks for the reply. It’s a code I found online on stackoverflow and it has been widely used. Unfortunately, with the last WordPress 5.5 and WooCommerce 4.4 update it has stopped working. I really hope someone is able to answer this. It shouldn’t be hard to fix it I guess but I am not a developer, unfortunately.

    • This reply was modified 4 years, 1 month ago by Beats247.
    Plugin Support Hannah S.L.

    (@fernashes)

    Automattic Happiness Engineer

    Hey there! As no one has been able to chime in to help, I’m going to wrap up this topic. If you do still need help, I recommend the resources mentioned above.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add Related Products to Customer EMail’ is closed to new replies.