Advertisement
Guest User

Untitled

a guest
May 27th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. /**
  2. * Add the custom attribute label "College" (custom2) to a product.
  3. *
  4. * @param array $attributes An array attributes.
  5. * @param array $post An array of post data including ID, post_title, post_status, etc...
  6. * @param array $product An array of product data returned from the Datafeedr API.
  7. * @param array $set A post array for this Product Set with an array key of postmeta containing all post meta data.
  8. * @param string $action The action the Product Set is performing. Value are either "insert" or "update".
  9. *
  10. * @return array Updated $attributes array.
  11. */
  12. add_filter( 'dfrpswc_product_attributes', 'mycode_set_promo_attribute_label', 20, 5 );
  13. function mycode_set_promo_attribute_label( $attributes, $post, $product, $set, $action ) {
  14. // The label for the product field.
  15. $label = 'College';
  16. // The product field to import.
  17. $field = 'custom2';
  18. /** if ( 'update' == $action ) {
  19. return $attributes;
  20. } */
  21. $sanitized_label = sanitize_title( $label );
  22. if ( ! isset( $product[ $field ] ) ) {
  23. return $attributes;
  24. }
  25. if ( isset( $attributes[ $sanitized_label ] ) ) {
  26. return $attributes;
  27. }
  28. if ( ! is_array( $attributes ) && empty( $attributes ) ) {
  29. $attributes = [];
  30. }
  31. $attributes[ $sanitized_label ]['name'] = $label;
  32. return $attributes;
  33. }
  34. /**
  35. * Set value for "Special Promotion" attribute.
  36. *
  37. * @param array|string $value The current value of the $attribute for this $post.
  38. * @param string $attribute The slug of the attribute. Examples: pa_color or pa_shoe-size
  39. * @param array $post An array of post data including ID, post_title, post_status, etc...
  40. * @param array $product An array of product data returned from the Datafeedr API.
  41. * @param array $set A post array for this Product Set with an array key of postmeta containing all post meta data.
  42. * @param string $action The action the Product Set is performing. Value are either "insert" or "update".
  43. *
  44. * @return array|string The updated attribute's value.
  45. */
  46. add_filter( 'dfrpswc_filter_attribute_value', 'mycode_set_promo_attribute_value', 30, 6 );
  47. function mycode_set_promo_attribute_value( $value, $attribute, $post, $product, $set, $action ) {
  48. // The label for the product field.
  49. $label = 'College';
  50. // The product field to import.
  51. $field = 'custom2';
  52. /** if ( 'update' == $action ) {
  53. return $value;
  54. } */
  55. if ( ! isset( $product[ $field ] ) ) {
  56. return $value;
  57. }
  58. if ( $attribute == $label ) {
  59. return $product[ $field ];
  60. }
  61. return $value;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement