Skip to content

Instantly share code, notes, and snippets.

@jessepearson
Last active February 10, 2025 22:56
Show Gist options
  • Select an option

  • Save jessepearson/66a0e72706b99c15b52dee7ce59e1d31 to your computer and use it in GitHub Desktop.

Select an option

Save jessepearson/66a0e72706b99c15b52dee7ce59e1d31 to your computer and use it in GitHub Desktop.

Revisions

  1. jessepearson renamed this gist Aug 22, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. jessepearson created this gist Aug 22, 2017.
    83 changes: 83 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    <?php // do not copy this line


    /**
    * add_new_topic_hooks will add a new webhook topic hook.
    * @param array $topic_hooks Esxisting topic hooks.
    */
    function add_new_topic_hooks( $topic_hooks ) {

    // Array that has the topic as resource.event with arrays of actions that call that topic.
    $new_hooks = array(
    'order.custom_filter' => array(
    'custom_order_filter',
    ),
    );

    return array_merge( $topic_hooks, $new_hooks );
    }
    add_filter( 'woocommerce_webhook_topic_hooks', 'add_new_topic_hooks' );

    /**
    * add_new_topic_events will add new events for topic resources.
    * @param array $topic_events Existing valid events for resources.
    */
    function add_new_topic_events( $topic_events ) {

    // New events to be used for resources.
    $new_events = array(
    'custom_filter',
    );

    return array_merge( $topic_events, $new_events );
    }
    add_filter( 'woocommerce_valid_webhook_events', 'add_new_topic_events' );

    /**
    * add_new_webhook_topics adds the new webhook to the dropdown list on the Webhook page.
    * @param array $topics Array of topics with the i18n proper name.
    */
    function add_new_webhook_topics( $topics ) {

    // New topic array to add to the list, must match hooks being created.
    $new_topics = array(
    'order.custom_filter' => __( 'Order Custom Filter', 'woocommerce' ),
    );

    return array_merge( $topics, $new_topics );
    }
    add_filter( 'woocommerce_webhook_topics', 'add_new_webhook_topics' );

    /**
    * my_order_item_check will check an order when it is created through the checkout form,
    * if it has product ID 10603 as one of the items, it will fire off the action `custom_order_filter`
    *
    * @param int $order_id The ID of the order that was just created.
    * @param array $posted_data Array of all of the data that was posted through checkout form.
    * @param object $order The order object.
    * @return null
    */
    function my_order_item_check( $order_id, $posted_data, $order ) {

    $order = wc_get_order( $order_id );
    $items = $order->get_items();

    foreach ( $items as $item ) {

    if ( is_a( $item, 'WC_Order_Item_Product' ) ) {

    if ( 10603 === $item->get_product_id() ) {

    do_action( 'custom_order_filter', $order_id, $posted_data, $order );
    return;
    }
    }
    }
    }
    add_action( 'woocommerce_checkout_order_processed', 'my_order_item_check', 10, 3 );

    /**
    * The two below actions are what the order.created webhook is tied into, it is up to you to use these if you wish.
    */
    // add_action( 'woocommerce_process_shop_order_meta', 'my_order_item_check' );
    // add_action( 'woocommerce_api_create_order', 'my_order_item_check' );