soflyy / oxygen-bugs-and-features

Bug Reports & Feature Requests for Oxygen
https://oxygenbuilder.com/
314 stars 29 forks source link

CSS is not Regenerated if Page/Template uses PHP Function Containing $global Product Classes #3103

Closed Ulprus closed 2 years ago

Ulprus commented 2 years ago

Describe the bug When setting up a PHP function in a snippet program (IE, Advanced Scripts) which echoes or returns a Product Class of WooCommerce (example: $product->get_sku();), whichever page or template uses this PHP Function will cause an AJAX error when regenerating that page's/template's CSS cache.

A link to a Sandbox site where the bug has been reproduced https://oxygenbuilder.com/try? (REQUIRED) Link: Admin Login Username: TestUser Password: TestUserPass123!@#

IF YOU CAN'T PROVIDE A SANDBOX REPRODUCTION: Steps required to reproduce the bug: Steps to reproduce the behavior:

  1. Install WooCommerce and add a product along with an SKU.
  2. Set up a PHP function in snippet software (in this case, I used Advanced Scripts):
<?php

/* This function breaks Oxygen's 'Regenerate CSS' for some reason */

function u_product_sku()
{

    global $product;
    $productSKU = $product->get_sku();

    echo $productSKU;
}
  1. Set up a template for post_type product
  2. Add a Repeater with a default query
  3. In the repeater, convert a text block to dynamic data
  4. Choose Return PHP Function
  5. Use u_product_sku
  6. The function works well and as intended in both the builder and the front.
  7. Now in Oxygen's settings, click on Regenerate CSS Cache.
  8. There will be an AJAX error for this template.
Kpudlo commented 2 years ago

Hello @Ulprus,

This was an issue that should have been fixed in Oxygen 4.0.4 when PHP such as global $product is used in a Code Block element.

I wasn't able to see the issue on the Sandbox site, but when using an external script, you may want to wrap the code within an if statement to see if that fixes the issue:

<?php

/* This function breaks Oxygen's 'Regenerate CSS' for some reason */

function u_product_sku()
{

    global $product;
    if($product) {
         $productSKU = $product->get_sku();

         echo $productSKU;
    }
}
Ulprus commented 2 years ago

Hi @Kpudlo - apologies for the delayed response; email never went into my primary inbox.

That worked perfectly! Thanks so much. Really appreciate this.