<p>You will need to write a plugin with three responsibilities:</p>

<ol>
<li>Keep track of which entries have been purchased</li>
<li>Set an entry to enabled when it has been purchased</li>
<li>Ensure that entries always have the correct status (enabled/disabled) whenever they are saved</li>
</ol>

<p>For the first one, you will need to create a new DB table that can store records of which entries have been purchased.</p>

<p>In your plugin, create a <code>records/</code> table with a <code>MyPlugin_EntryPurchaseRecord.php</code> file:</p>



<pre class="lang-php prettyprint-override"><code>&lt;?php
namespace Craft;

class MyPlugin_EntryPurchaseRecord extends BaseRecord
{
    public function getTableName()
    {
        return 'myplugin_entrypurchases';
    }

    public function defineRelations()
    {
        return array(
            'entry' =&gt; array(static::BELONGS_TO, 'EntryRecord'),
            'order' =&gt; array(static::BELONGS_TO, 'Commerce_OrderRecord'),
        );
    }
}
</code></pre>

<p>With that in place, the table will automatically get created when you install your plugin.</p>

<p>Next, give your primary plugin class an <code>init()</code> method where you can set up some event listeners:</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php
namespace Craft;

class MyPluginPlugin extends BasePlugin
{
    // ...

    public function init()
    {
        craft()-&gt;on('commerce_orders.orderComplete', array($this, 'onOrderComplete'));
        craft()-&gt;on('entries.beforeSaveEntry', array($this, 'onBeforeSaveEntry'));
    }
}
</code></pre>

<p>Your <code>onOrderComplete</code> function will be responsible for recording the purchase in your entrypurchases table, and updating the entry’s status to enabled:</p>

<pre class="lang-php prettyprint-override"><code>public function onOrderComplete(Event $event)
{
    /* @var Commerce_OrderModel $order */
    $order = $event-&gt;params['order'];

    // Look for a line item with an entryId option
    foreach ($order-&gt;lineItems as $lineItem)
    {
        if (!empty($lineItem-&gt;options['entryId']))
        {
            // Validate the entry ID
            $entryId = $lineItem-&gt;options['entryId'];
            $entry = craft()-&gt;entries-&gt;getEntryById($entryId);
            if (!$entry) continue;

            // Record the purchase
            $purchaseRecord = new MyPlugin_EntryPurchaseRecord();
            $purchaseRecord-&gt;entryId = $entryId;
            $purchaseRecord-&gt;orderId = $order-&gt;id;
            $purchaseRecord-&gt;save();

            // Enable the entry
            $entry-&gt;enabled = true;
            craft()-&gt;entries-&gt;saveEntry($entry);
        }
    }
}
</code></pre>

<p>And your <code>onBeforeSaveEntry</code> function will be responsible for making sure entries’ statuses are set correctly depending on whether they’ve been purchased:</p>

<pre class="lang-php prettyprint-override"><code>public function onBeforeSaveEntry(Event $event)
{
    // Ignore for new entries
    if (!empty($event-&gt;params['isNewEntry']))
    {
        return;
    }

    /* @var EntryModel $entry */
    $entry = $event-&gt;params['entry'];

    // Ignore entries in other sections
    if ($entry-&gt;section-&gt;handle != 'mySection')
    {
        return;
    }

    // Has the entry been purchased?
    $purchaseCount = MyPlugin_EntryPurchaseRecord::model()-&gt;countByAttributes(array(
        'entryId' =&gt; $entry-&gt;id
    ));

    $entry-&gt;enabled = ($purchaseCount != 0);
}
</code></pre>

<p>Last but not least, you will need to update your entry purchase form to record the entry ID being purchased. We’ll do that as an <a href="https://craftcommerce.com/docs/add-to-cart#purchasable-options-and-notes">option</a> on the line item.</p>

<p>(If you look at the <code>onOrderComplete</code> function, we’re actually combing through the purchased order’s line items to look for these <code>entryId</code> options. This is where it gets set.)</p>

<pre class="lang-html prettyprint-override"><code>&lt;form method="POST"&gt;
    &lt;input type="hidden" name="action" value="commerce/cart/updateCart"&gt;
    &lt;input type="hidden" name="redirect" value="commerce/cart"&gt;
    &lt;input type="hidden" name="qty" value="1"&gt;
    &lt;input type="hidden" name="purchasableId" value="{{ variant.purchasableId }}"&gt;

    &lt;!-- Associate the line item with this entry ID --&gt;
    &lt;input type="hidden" name="options[entryId]" value="{{ entry.id }}"&gt;

    &lt;input type="submit" value="Buy Now"&gt;
&lt;/form&gt;
</code></pre>

<p>Baring any typos, that should do it!</p>

CC BY-SA 3.0
2016-01-18T23:48:28.700Z
I just got a chance to check out this plugin today. It didn't work at first because I had missed the underscore when I was renaming the class in the records file. Once I searched through the Craft plugin documentation and saw that it was needed I made the correction and everything worked excellent. Thanks again for the help!
Cavell Blood
CC BY-SA 3.0
2016-01-13T00:50:17.200Z
I'll check this out this evening or tomorrow. Thanks! Really appreciate it.
Cavell Blood
CC BY-SA 3.0
2016-01-13T00:45:50.223Z
2016-01-13T00:45:50.223Z
Brandon Kelly
<p>I'm trying to figure out how to do the following:</p>

<ol>
<li>Person registers as new user</li>
<li>User creates a new entry with a front-end entry form</li>
<li>Entry is saved as disabled so it's not publicly visible</li>
<li>User has the option to make entry public by making a payment with Craft Commerce.</li>
<li>When the Craft order is complete it updates the entry in step 3 to enabled so that is will be publicly visible.</li>
</ol>

<p>I think I should be able to use this event <a href="https://craftcommerce.com/docs/events-reference#commerce_orders.onordercomplete" rel="noreferrer">commerce_orders.onOrderComplete</a> but I'm not sure how to pass the entry ID into this event and also how to check if the order that has just been completed is the order that applies to this event checkout.</p>

<p>Any thoughts on how to accomplish this?</p>

<plugin-development><entries><frontend><plugin-craftcommerce>
Using Commerce to pay for posting entry
# Using Commerce to pay for posting entry

(Asked by: username_0 on 2016-01-12)

I'm trying to figure out how to do the following:

1. Person registers as new user
2. User creates a new entry with a front-end entry form
3. Entry is saved as disabled so it's not publicly visible
4. User has the option to make entry public by making a payment with Craft Commerce.
5. When the Craft order is complete it updates the entry in step 3 to enabled so that is will be publicly visible.

I think I should be able to use this event [commerce\_orders.onOrderComplete](https://craftcommerce.com/docs/events-reference#commerce_orders.onordercomplete) but I'm not sure how to pass the entry ID into this event and also how to check if the order that has just been completed is the order that applies to this event checkout.

Any thoughts on how to accomplish this?

**Tags:** plugin-development, entries, frontend, plugin-craftcommerce


---

## 1 answers

### Answer by username_1 on 2016-01-13. Score: 11 | Accepted answer

You will need to write a plugin with three responsibilities:

Keep track of which entries have been purchased
Set an entry to enabled when it has been purchased
Ensure that entries always have the correct status (enabled/disabled) whenever they are saved

For the first one, you will need to create a new DB table that can store records of which entries have been purchased.
In your plugin, create a records/ table with a MyPlugin_EntryPurchaseRecord.php file:
 array(static::BELONGS_TO, 'EntryRecord'),
            'order' => array(static::BELONGS_TO, 'Commerce_OrderRecord'),
        );
    }
}

With that in place, the table will automatically get created when you install your plugin.
Next, give your primary plugin class an init() method where you can set up some event listeners:
on('commerce_orders.orderComplete', array($this, 'onOrderComplete'));
        craft()->on('entries.beforeSaveEntry', array($this, 'onBeforeSaveEntry'));
    }
}

Your onOrderComplete function will be responsible for recording the purchase in your entrypurchases table, and updating the entry’s status to enabled:
public function onOrderComplete(Event $event)
{
    /* @var Commerce_OrderModel $order */
    $order = $event->params['order'];

    // Look for a line item with an entryId option
    foreach ($order->lineItems as $lineItem)
    {
        if (!empty($lineItem->options['entryId']))
        {
            // Validate the entry ID
            $entryId = $lineItem->options['entryId'];
            $entry = craft()->entries->getEntryById($entryId);
            if (!$entry) continue;

            // Record the purchase
            $purchaseRecord = new MyPlugin_EntryPurchaseRecord();
            $purchaseRecord->entryId = $entryId;
            $purchaseRecord->orderId = $order->id;
            $purchaseRecord->save();

            // Enable the entry
            $entry->enabled = true;
            craft()->entries->saveEntry($entry);
        }
    }
}

And your onBeforeSaveEntry function will be responsible for making sure entries’ statuses are set correctly depending on whether they’ve been purchased:
public function onBeforeSaveEntry(Event $event)
{
    // Ignore for new entries
    if (!empty($event->params['isNewEntry']))
    {
        return;
    }

    /* @var EntryModel $entry */
    $entry = $event->params['entry'];

    // Ignore entries in other sections
    if ($entry->section->handle != 'mySection')
    {
        return;
    }

    // Has the entry been purchased?
    $purchaseCount = MyPlugin_EntryPurchaseRecord::model()->countByAttributes(array(
        'entryId' => $entry->id
    ));

    $entry->enabled = ($purchaseCount != 0);
}

Last but not least, you will need to update your entry purchase form to record the entry ID being purchased. We’ll do that as an option on the line item.
(If you look at the onOrderComplete function, we’re actually combing through the purchased order’s line items to look for these entryId options. This is where it gets set.)

    
    
    
    

    
    

    


Baring any typos, that should do it!

> **Comments:**
> **username_0**: I'll check this out this evening or tomorrow. Thanks! Really appreciate it.  
> **username_0**: I just got a chance to check out this plugin today. It didn't work at first because I had missed the underscore when I was renaming the class in the records file. Once I searched through the Craft plugin documentation and saw that it was needed I made the correction and everything worked excellent. Thanks again for the help!  

---

