Jump to content

Wordpress cross-plugin refering


vortegast
Go to solution Solved by gizmola,

Recommended Posts

Hey.
I have no knowledge of PHP, but am trying to add some features to my Woocommerce website by fiddling with the .php files.
What I’m trying to accomplish is catching a ‘shop-orders’ variable from one plugin, which adds delivery time and date, and giving it to another plugin, which sends the order data to an email address.

Plugin 1

 /**
     * Display service details in Order Receipt from order meta
     *
     * @author FoodStore
     * @since 1.1
     * @return void
     */
    public function receipt_services_meta( $order ) {

        if( ! apply_filters( 'wfs_receipt_show_services', true ) ) 
            return;

        $order_id       = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
        $service_type   = get_post_meta( $order_id, '_wfs_service_type', true );
        $service_date   = get_post_meta( $order_id, '_wfs_service_date', true );
        $service_time   = get_post_meta( $order_id, '_wfs_service_time', true );

        $date_format    = get_option('date_format');

        echo '<tr>';
        echo '<td><strong>' . __( 'Service Type:', 'food-store' ) .'</strong></td>';
        echo '<td><strong>' . wfs_get_service_label( $service_type ) .'</strong></td>';
        echo '</tr>';
        echo '<tr>';
        echo '<td><strong>' . __( 'Date:', 'food-store') . '</strong></td>';
        echo '<td><strong>' . date_i18n( $date_format, strtotime( $service_date ) ) . '</strong></td>';
        echo '</tr>';
        echo '<tr>';
        echo '<td><strong>' . __( 'Time:', 'food-store' ) . '</strong></td>';
        echo '<td><strong>' . $service_time . '</strong></td>';
        echo '</tr>';
    }

Plugin 2

		function convert_template() {



			$defaults = array(
      'show_sku'      => false,
      'show_image'    => false,
      'image_size'    => array( 32, 32 ),
      'plain_text'    => true,
      'sent_to_admin' => false,
    );

		//	$this->placeholders['{woocommerce_email_order_meta}']    = $this->woocommerce_email_order_meta();
			$this->placeholders['{order_billing_name}']    = $this->object->get_billing_first_name() . ' ' . $this->object->get_billing_last_name();
			$this->placeholders['{email_order_items_table}']    = wc_get_email_order_items( $this->object , $defaults );
			$this->placeholders['{email_order_total_footer}']    = $this->email_order_total_footer();
			$this->placeholders['{order_billing_email}']    = $this->object->get_billing_email();
			$this->placeholders['{order_billing_phone}']    = $this->object->get_billing_phone();
			$this->placeholders['{email_addresses}']    = $this->get_email_addresses();
			$this->placeholders['{site_title}']    = get_bloginfo('name');
			$this->placeholders['{order_shipping_address_1}'] = $this->object->shipping_address_1;
			$this->placeholders['{order_shipping_city}'] = $this->object->shipping_city;
			$this->placeholders['{order_shipping_state}'] = WC()->countries->states[$this->object->billing_country][$this->object->billing_state];
 			$this->placeholders['{order_shipping_postcode}'] = $this->object->shipping_postcode;
 			$this->placeholders['{order_shipping_country}'] = WC()->countries->countries[ $this->object->get_shipping_country() ];
			// For old woocommerce use find and replace methods
			foreach ( $this->placeholders as $find => $replace ) {
				$this->find[]    = $find;
				$this->replace[] = $replace;
			}

			$this->placeholders = apply_filters( 'wcemails_find_placeholders', $this->placeholders, $this->object );

			// Legacy filters
			$this->find      = apply_filters( 'wcemails_find_placeholders', $this->find, $this->object );
			$this->replace   = apply_filters( 'wcemails_replace_placeholders', $this->replace, $this->object );

		}

I would like $service_time to be one of the placeholders in plugin 2.
What I’ve tried is this:

Quote

$this->placeholders[’{order_service_time}’] = receipt_services_meta( $service_time );

This failed.
I was hoping any one of you could quickly help me out here, since this may be just a 5 second job for someone who knows what they’re doing.

Thanks in advance!

Link to comment
Share on other sites

receipt_services_meta() is intended to display some HTML table of data. If you just want one value out of it, without a table, then you can't actually use the function.

But you can look at the function and mirror how parts of it work. Specifically, the bits that involve the $service_time.

$service_time is defined as

$service_time   = get_post_meta( $order_id, '_wfs_service_time', true );

so the first question is: how do you get $order_id?

That came from

$order_id       = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();

so next is: how do you get $order? Because that was provided to the receipt_services_meta() function by something else.

If you're lucky, the $order from the first plugin may be the same as the $this->object from the second plugin. I'm skeptical, but without knowing more you might as well try it out.

$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $this->object->id : $this->object->get_id();
$service_time = get_post_meta( $order_id, '_wfs_service_time', true );

 

  • Thanks 1
Link to comment
Share on other sites

It's been a while since I've dealt with WooCommerce and even in my current job my interaction with WP core has been limited so ymmv, but given you're talking about WordPress plugins I assume the global $post variable is available. If I remember correctly (and a quick SO search didn't lead my brain astray) you should be able to add `$order = new WC_Order($post->id);` before the code requinix suggested if you get an error with just that code alone. WordPress relies heavily on global variables and this unfortunately makes debugging difficult at times...

  • Thanks 1
Link to comment
Share on other sites

  • Solution

My educated guess is that requinix is right.   I also think the code snippets reflect that the order object should be available in plugin2 via $this->object.

So I would suggest you try adding this code to plugin2

$this->placeholders['{order_service_time}'] = get_post_meta( $this->object->get_id(), '_wfs_service_time', true);
  • Thanks 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.