I'm receiving a php warning in the error when I run the below script in the footer.php file. The code works but is there is a way to write it better?


 global $product;

    $mpn = array_shift( wc_get_product_terms( $product->id, 'pa_mpn', array( 'fields' => 'names' ) ) );
    $brand = array_shift( wc_get_product_terms( $product->id, 'pa_brand', array( 'fields' => 'names' ) ) );

    <script type='text/javascript'>
      var ccs_cc_args = ccs_cc_args || [];
      // Main Product Page
      ccs_cc_args.push(['cpn', 'CPN']);
      ccs_cc_args.push(['mf', '<?php echo $brand; ?> ']);
      ccs_cc_args.push(['pn', '<?php echo $mpn; ?>']);
      ccs_cc_args.push(['upcean', 'UPC_EAN_CODE']);
      ccs_cc_args.push(['ccid', 'CATALOG_CODE']);
      ccs_cc_args.push(['lang', 'LANGUAGE']);
      ccs_cc_args.push(['market', 'MARKET']);
      (function () {
         var o = ccs_cc_args; o.push(['_SKey', '']); o.push(['_ZoneId', '6ce9a32538']);
         var sc = document.createElement('script'); sc.type = 'text/javascript'; sc.async = true;
         sc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.cs.1worldsync.com/jsc/h1ws.js';
         var n = document.getElementsByTagName('script')[0]; n.parentNode.insertBefore(sc, n);
  • On which line, the $mpn and $brand lines? Then I guess you're running this even when $product is null i.e. even when this isn't a product page.
    – Rup
    Commented Oct 24, 2023 at 12:51
  • 1
    You could also use wp_add_inline_script to pass the mpn and brand into an enqueued script block rather than inlining your complete tracking code. That would also avoid any problems escaping your strings for inclusion in JavaScript strings, which you're not doing. And I guess you don't really want to pass dummy values into upcean etc.?
    – Rup
    Commented Oct 24, 2023 at 12:53
  • 2
    you've written $product->id and assumed that there will always be a $product in global $product but this is incorrect! E.g. if you were on a login page which product would $product be? You need to check assumptions using if conditions before doing things
    – Tom J Nowell
    Commented Oct 24, 2023 at 13:12

1 Answer 1


$product->id only works if $product has a value, and sometimes it does not! E.g. on a login page which product would it be?

So you need to check if it's valid/exists/safe with an if check:

global $product;
if ( ! empty( $product ) ) {
    // code that uses $product goes here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.