Skip to content

woocommerce recurring wip

wankimmy edited this page Oct 8, 2024 · 1 revision

To update your function to handle and store the new $extraP parameter, follow these steps. This modification will extract the array, decode the JSON-encoded extraP data, and store it as metadata in the WooCommerce order.

Modified Code:

public function update_Cart_by_Status($orderid, $MOLPay_status, $tranID, $referer, $channel, $extraP) {
    global $woocommerce;

    $order = new WC_Order($orderid);
    switch ($MOLPay_status) {
        case '00':
            $M_status = 'SUCCESSFUL';
            break;
        case '22':
            $M_status = 'PENDING';
            $W_status = 'pending';
            break;
        case '11':
            $M_status = 'FAILED';
            $W_status = 'failed';
            break;
        default:
            $M_status = 'Invalid Transaction';
            $W_status = 'on-hold';
            break;
    }

    $channel_mappings = array(
        'maybank2u' => 'fpx_mb2u',
        'cimb' => 'fpx_cimbclicks',
        'hlb' => 'fpx_hlb',
        'rhb' => 'fpx_rhb',
        'amb' => 'fpx_amb',
        'publicbank' => 'fpx_pbb',
        'abb' => 'fpx_abb',
        'bankislam' => 'fpx_bimb',
        'alliancebank' => 'fpx_abmb',
        'bkrm' => 'fpx_bkrm',
        'bsn' => 'fpx_bsn',
        'hsbc' => 'fpx_hsbc',
        'kuwait-finace' => 'fpx_kfh',
        'ocbc' => 'fpx_ocbc',
        'scb' => 'fpx_scb',
        'uob' => 'fpx_uob',
        'agrobank' => 'fpx_agrobank',
        'muamalat' => 'fpx_bmmb',
        'TNG-EWALLET' => 'TNG_EWALLET',
        'RPP_DuitNowQR' => 'RPP_DuitNowQR',
        'CIMB_eBPG' => 'cimb-ebpg',
        'PBB-CYBS' => 'pbb-cybs',
    );

    if (isset($channel_mappings[$channel])) {
        $channel = $channel_mappings[$channel];
    }

    $getStatus = $order->get_status();
    if (!in_array($getStatus, array('processing', 'completed'))) {
        // Add order note with payment status and transaction details
        $order->add_order_note('Fiuu Payment Status: ' . $M_status . '<br>Transaction ID: ' . $tranID . $referer);

        // Handle successful payments
        if ($MOLPay_status == "00") {
            $order->payment_complete();
        } else {
            $order->update_status($W_status, sprintf(__('Payment %s via Fiuu.', 'woocommerce'), $tranID));
        }

        // Set payment method title based on channel if applicable
        if ($this->payment_title == 'yes') {
            $paytitle = $this->payment_titles[strtolower($channel)];
            $order->set_payment_method_title($paytitle);
        }

        // Store the extraP data as order metadata
        if (!empty($extraP)) {
            $extra_data = json_decode($extraP, true);
            foreach ($extra_data as $key => $value) {
                $order->update_meta_data('_' . $key, $value);
            }
        }

        // Save changes to the order
        $order->save();
    } else {
        wp_die("Order completed");
    }
}

Changes Explained:

  1. New Parameter: Added $extraP as a new function parameter to handle the extraP array from the input.
  2. JSON Decoding: The extraP string is decoded using json_decode($extraP, true), converting it into a PHP associative array.
  3. Store Metadata: For each key-value pair in the $extra_data array, store it in the WooCommerce order using $order->update_meta_data('_' . $key, $value). The prefix _ is added to ensure the keys are stored as hidden meta fields.
  4. Order Saving: After updating metadata, the order is saved using $order->save().

Sample Usage:

update_Cart_by_Status(24100135361, '00', '2421280836', 'PaymentGateway', 'credit', '{"token":"599639XXXXXX9963","ccbrand":"MASTERCARD","cctype":"CREDIT","cclast4":"9963"}');

Additional Tips:

  1. Validation: Consider adding validation for $extraP to ensure it’s in the correct JSON format before attempting to decode.
  2. Metadata Prefix: Use _ before metadata keys to ensure they remain hidden in WooCommerce’s admin interface, or omit it if you want them visible.

Let me know if this solution fits your requirement or if you need further assistance!

To view the custom order metadata in the WooCommerce dashboard, you can follow these steps:

1. Accessing the Order Details in WooCommerce

  1. Go to your WordPress Admin Panel.
  2. Navigate to WooCommerceOrders.
  3. Find the specific order that you want to check and click on it to open the Order Details page.

2. Viewing Custom Metadata in the Order Details

  1. Scroll down to the Custom Fields (Order Meta) section on the Order Details page.

  2. Look for fields with names starting with an underscore (_) followed by the keys you added, such as:

    • _token
    • _ccbrand
    • _cctype
    • _cclast4

    These fields are the custom metadata values stored using the code:

    $order->update_meta_data('_' . $key, $value);

    Note: WooCommerce hides fields with the _ prefix by default.

3. Making Metadata Visible in the Admin Interface

To make these custom fields visible on the order details page (if they’re not displayed), you can:

Option 1: Using a Plugin

Use a plugin like WooCommerce Admin Custom Order Fields or Advanced Custom Fields (ACF) to view and manage custom fields in the WooCommerce order details page.

Option 2: Custom Code in functions.php

If you want to display these fields without using plugins, add this custom code to your theme’s functions.php file:

// Display custom order meta in the Order Details page
add_action('woocommerce_admin_order_data_after_order_details', 'display_custom_order_meta_fields', 10, 1);

function display_custom_order_meta_fields($order) {
    // Retrieve custom metadata from the order
    $custom_meta = $order->get_meta_data();

    echo '<h4>' . __('Custom Payment Information') . '</h4>';
    echo '<ul>';
    
    foreach ($custom_meta as $meta) {
        $key = $meta->key;
        $value = $meta->value;
        
        // Display only your custom fields with the underscore (_)
        if (strpos($key, '_') === 0) {
            echo '<li><strong>' . esc_html($key) . ':</strong> ' . esc_html($value) . '</li>';
        }
    }

    echo '</ul>';
}

4. Checking the Order Note

Since the code also adds an order note with the payment status and transaction details, you can also see this in the Order Notes section:

  1. Scroll down to the Order Notes on the Order Details page.
  2. You’ll see a note similar to:
    Fiuu Payment Status: SUCCESSFUL
    Transaction ID: 2421280836
    
    This note will appear regardless of the visibility of the custom metadata.

5. Direct Database Check (Advanced Users)

If you want to verify that the metadata was stored correctly, you can check the wp_postmeta table in your database:

  • Look for entries where the post_id corresponds to the WooCommerce order ID.
  • Search for meta keys such as _token, _ccbrand, _cctype, etc.

6. Using WooCommerce Admin Panel Plugins

If you need an easier way to view or manipulate metadata without modifying code:

  • Use plugins like WooCommerce Admin Custom Order Fields or Show All Meta plugins to expose hidden fields.

This should allow you to view and manage all the custom order data directly from the WooCommerce dashboard! Let me know if this helps or if you'd like more customizations!

To allow customers to see the last 4 digits of their credit card in the Order History section on the customer side, follow these steps. We’ll customize the order history display by adding the necessary metadata to show the last 4 digits of the credit card using WooCommerce hooks and shortcodes.

Step 1: Store the Last 4 Digits of Credit Card During Checkout

In your code, you are already storing custom metadata in the WooCommerce order with keys like _cclast4. This metadata is crucial as it contains the last 4 digits of the credit card.

Example:

$order->update_meta_data('_cclast4', $extra_data['cclast4']);

Step 2: Display the Last 4 Digits in the Customer Order History

To display this data in the customer’s order history page, we need to hook into WooCommerce’s woocommerce_my_account_my_orders_columns and woocommerce_my_account_my_orders_column_ hooks. Add the following code to your theme’s functions.php file or a custom plugin:

// Add a new column to the order history table in My Account
add_filter('woocommerce_my_account_my_orders_columns', 'add_cc_last4_to_order_history');

function add_cc_last4_to_order_history($columns) {
    $new_columns = array();

    foreach ($columns as $key => $column) {
        $new_columns[$key] = $column;

        // Add custom column after the order date
        if ('order-date' === $key) {
            $new_columns['cc_last4'] = __('Last 4 Digits of Card', 'woocommerce');
        }
    }

    return $new_columns;
}

// Populate the new column with the last 4 digits of the credit card
add_action('woocommerce_my_account_my_orders_column_cc_last4', 'show_cc_last4_in_order_history');

function show_cc_last4_in_order_history($order) {
    $last4 = $order->get_meta('_cclast4');

    if ($last4) {
        echo esc_html($last4);
    } else {
        echo __('N/A', 'woocommerce');
    }
}

How the Code Works:

  1. woocommerce_my_account_my_orders_columns: Adds a new column called "Last 4 Digits of Card" to the order history table.
  2. woocommerce_my_account_my_orders_column_cc_last4: Populates this new column with the last 4 digits of the credit card (_cclast4) stored in the order metadata.

Step 3: Testing and Verifying

  1. Log in as a customer on your WordPress WooCommerce site.
  2. Navigate to My AccountOrders.
  3. You should see a new column labeled Last 4 Digits of Card with values corresponding to the stored metadata.

Step 4: Customizing the Display in Order Details Page

If you want to show the last 4 digits of the credit card on the order details page as well:

  1. Hook into the woocommerce_order_details_after_order_table action to add custom content to the order details view.
  2. Use the following code:
// Show last 4 digits of the credit card in the Order Details page
add_action('woocommerce_order_details_after_order_table', 'display_last4_in_order_details');

function display_last4_in_order_details($order) {
    $last4 = $order->get_meta('_cclast4');

    if ($last4) {
        echo '<p><strong>' . __('Last 4 Digits of Credit Card: ', 'woocommerce') . '</strong>' . esc_html($last4) . '</p>';
    }
}

Step 5: Security Considerations

  1. Ensure that sensitive data (like full credit card numbers or other personal information) is never exposed. Display only non-sensitive data such as the last 4 digits.
  2. Test thoroughly to confirm that only the order owner can view the custom metadata.

Final Result

After implementing the above code:

  • The Order History page will have a new column showing the last 4 digits of the credit card.
  • The Order Details page will show a summary of the last 4 digits under the order information.

Optional: Displaying More Information

If you want to display more fields from the $extraP array, simply modify the functions to retrieve and display other keys like ccbrand or cctype. Adjust the get_meta() call accordingly:

$ccbrand = $order->get_meta('_ccbrand');
$cctype = $order->get_meta('_cctype');

Let me know if you'd like to customize this further or need any additional help!